Laravel là gì?
Laravel là một PHP Framework mã nguồn mở và miễn phí chuyên dùng cho phát triển các ứng dụng web. Laravel được phát triển theo mô hình MVC (model–view–controller) và được phát hành theo giấy phép MIT, bạn có thể tìm và tải mã nguồn Laravel trên trang GitHub chính thức của Laravel tại đây.
Theo thống kê của SitePoint thì Laravel là PHP Framework được sử dụng nhiều nhất trong năm 2013 với 25.85% thị phần, tiếp theo sau là các PHP Framework nổi tiếng khác như Phalcon, Symfony2, CodeIgniter, …
Cài đặt Laravel trên CentOS 6 64bit
Đầu tiên, bạn cần cài đặt LAMP Stack hoặc LEMP Stack trên VPS của bạn. Sau đó bạn cài đặt Composer trên CentOS bằng cách thực hiện những lệnh sau :
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
Sau đó bạn tải về “Laravel installer” bằng cách dùng lệnh sau :
cd /root composer global require "laravel/installer=~1.1" export PATH=/root/.composer/vendor/bin:$PATH
Nếu bạn gặp thông báo tương tự như bên dưới thì có nghĩa là phiên bản PHP trên VPS của bạn thấp hơn 5.4.0 nên không cài đặt được Laravel.
Your requirements could not be resolved to an installable set of packages. Problem 1 - laravel/installer v1.1.1 requires guzzlehttp/guzzle ~4.0 -> satisfiable by guzzlehttp/guzzle[4.0.0, 4.0.1, 4.0.2, 4.1.0, 4.1.1, 4.1.2, 4.1.3, 4.1.4, 4.1.5, 4.1.6, 4.1.7, 4.1.8, 4.2.0, 4.2.1, 4.2.2, 4.2.3]. - laravel/installer v1.1.2 requires guzzlehttp/guzzle ~4.0 -> satisfiable by guzzlehttp/guzzle[4.0.0, 4.0.1, 4.0.2, 4.1.0, 4.1.1, 4.1.2, 4.1.3, 4.1.4, 4.1.5, 4.1.6, 4.1.7, 4.1.8, 4.2.0, 4.2.1, 4.2.2, 4.2.3]. - laravel/installer v1.1.3 requires guzzlehttp/guzzle ~4.0 -> satisfiable by guzzlehttp/guzzle[4.0.0, 4.0.1, 4.0.2, 4.1.0, 4.1.1, 4.1.2, 4.1.3, 4.1.4, 4.1.5, 4.1.6, 4.1.7, 4.1.8, 4.2.0, 4.2.1, 4.2.2, 4.2.3]. - laravel/installer v1.1.0 requires illuminate/support ~4.2 -> satisfiable by illuminate/support[v4.2.1, v4.2.12, v4.2.16, v4.2.17, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9]. - guzzlehttp/guzzle 4.2.3 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.2.2 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.2.1 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.2.0 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.8 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.7 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.6 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.5 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.4 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.3 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.2 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.1 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.1.0 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.0.2 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.0.1 requires php >=5.4.0 -> no matching package found. - guzzlehttp/guzzle 4.0.0 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.9 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.8 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.7 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.6 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.5 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.4 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.3 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.2 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.17 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.16 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.12 requires php >=5.4.0 -> no matching package found. - illuminate/support v4.2.1 requires php >=5.4.0 -> no matching package found. - Installation request for laravel/installer ~1.1 -> satisfiable by laravel/installer[v1.1.0, v1.1.1, v1.1.2, v1.1.3]. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details. Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems. Installation failed, deleting ./composer.json.
Bạn kiểm tra phiên bản PHP bạn đang sử dụng bằng lệnh sau :
php -v
[root@xtraffic]# php -v PHP 5.3.3 (cli) (built: Oct 30 2014 20:12:53) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies [root@xtraffic]#
Trong trường hợp này là phiên bản PHP đang sử dụng là 5.3.3, do đó ta cần nâng cấp lên php 5.4 trở lên thì mới cài đặt được Laravel
Nếu mọi chuyện thành công, thì bạn tạo và di chuyển đến thư mục “/home/laravel/” và tạo project mới có tên là “hello-world” bằng các lệnh sau :
sudo mkdir -p /home/laravel/ cd /home/laravel/ laravel new ./hello-world
[root@xtraffic laravel]# laravel new ./hello-world Crafting application... Application ready! Build something amazing. [root@xtraffic laravel]#
Trong thư mục “/home/laravel/hello-world/” sẽ có cấu trúc như sau :
Sau đó bạn tạo file “/etc/nginx/conf.d/hello-world.conf” để cấu hình Nginx với nội dung như sau :
server { listen *:80; server_name 23.88.236.66; index index.html index.php; root /home/laravel/hello-world/public; location / { try_files $uri $uri/ /index.php?$query_string; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Bạn thay địa chỉ IP “23.88.236.66” bằng địa chỉ IP của VPS của bạn
Khởi động lại Nginx :
service nginx restart
Sau đó mở trình duyệt ra và truy cập vào địa chỉ ip của host sẽ thấy giao diện tương tự như sau :
Vậy là bạn đã cài đặt thành công phiên bản Laravel mới nhất (tại thời điểm viết bài là Laravel 5.0)
Tạo trang “Hello World” cho Laravel
Bạn tạo file “/home/laravel/hello-world/app/Http/Controllers/HelloController.php” có nội dung như sau :
<?php namespace App\Http\Controllers; class HelloController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { } /** * Show the application dashboard to the user. * * @return Response */ public function index() { return view('hello'); } }
Bạn tạo file “/home/laravel/hello-world/resources/views/hello.blade.php” có nội dung như sau :
<div> <h1>Hello World</h1> <p>Laravel Framework</p> <p>Tutorial by <b><a href="http://blog-xtraffic.pep.vn/">http://blog-xtraffic.pep.vn/</a></b></p> </div>
Mở file “/home/laravel/hello-world/app/Http/routes.php” và thêm vào đoạn mã PHP sau :
Route::get('hello', 'HelloController@index');
Sau đó bạn mở trình duyệt lên và truy cập vào “http://23.88.236.66/hello” sẽ thấy như sau :
Benchmark Laravel
Tiếp theo chúng ta sẽ benchmark trang “http://23.88.236.66/hello” để kiểm tra hiệu năng của Laravel.
Thông tin VPS chạy Laravel :
- Nhà cung cấp VPS : BudgetVM
- Tên gói : XEN SSD CACHED VPS Servers
- 1024MB RAM
- 50GB SSD Storage
- 1 CPU
- Bandwidth : 3TB (3072 GB)
- Port Speed : 1Gbps
- DDOS Protected
- Price : $4.99/month (104.000 VNĐ/1 tháng)
- Link chi tiết : http://blog-xtraffic.pep.vn/danh-gia-chat-luong-vps-budgetvm/
- Link đăng ký : https://www.budgetvm.com/
Benchmark Laravel chạy trên Nginx + PHP-FPM + APC
Benchmark Laravel bằng công cụ Apache Benchmark (ab)
[[email protected] ~]# ab -c 20 -k -r -t 60 -v 3 http://23.88.236.66/hello Finished 268 requests Server Software: nginx/1.4.0 Server Hostname: 23.88.236.66 Server Port: 80 Document Path: /hello Document Length: 155 bytes Concurrency Level: 20 Time taken for tests: 60.207 seconds Complete requests: 268 Failed requests: 0 Write errors: 0 Keep-Alive requests: 0 Total transferred: 303916 bytes HTML transferred: 41540 bytes Requests per second: 4.45 [#/sec] (mean) Time per request: 4493.042 [ms] (mean) Time per request: 224.652 [ms] (mean, across all concurrent requests) Transfer rate: 4.93 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 4.5 0 19 Processing: 1318 4314 724.1 4276 7385 Waiting: 1318 4314 724.3 4276 7385 Total: 1337 4316 723.8 4276 7403 Percentage of the requests served within a certain time (ms) 50% 4276 66% 4355 75% 4389 80% 4429 90% 4784 95% 5821 98% 6489 99% 7236 100% 7403 (longest request)
Benchmark Laravel bằng công cụ Siege
[root@xtraffic ~]# siege -c20 -t60s http://23.88.236.66/hello Lifting the server siege.. done. Transactions: 269 hits Availability: 100.00 % Elapsed time: 59.04 secs Data transferred: 0.04 MB Response time: 3.75 secs Transaction rate: 4.56 trans/sec Throughput: 0.00 MB/sec Concurrency: 17.09 Successful transactions: 269 Failed transactions: 0 Longest transaction: 7.27 Shortest transaction: 1.32
Benchmark Laravel bằng công cụ wrk
[root@xtraffic]# wrk -t10 -c20 -d60s http://23.88.236.66/hello Running 1m test @ http://23.88.236.66/hello 10 threads and 20 connections Thread Stats Avg Stdev Max +/- Stdev Latency 5.24s 1.32s 7.76s 79.13% Req/Sec 0.00 0.00 0.00 100.00% 259 requests in 1.00m, 286.09KB read Socket errors: connect 0, read 0, write 0, timeout 340 Requests/sec: 4.31 Transfer/sec: 4.77KB
Benchmark Laravel bằng công cụ boom
[root@xtraffic]# boom -n 2000 -c 20 -cpus 1 -allow-insecure http://23.88.236.66/hello 2000 / 2000 Boooooooooooooooooooooooooo! 100.00 % Summary: Total: 400.1436 secs. Slowest: 15.4960 secs. Fastest: 1.2228 secs. Average: 3.9933 secs. Requests/sec: 4.9982 Status code distribution: [200] 2000 responses Response time histogram: 1.223 [1] | 2.650 [11] | 4.077 [1580] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 5.505 [351] |∎∎∎∎∎∎∎∎ 6.932 [15] | 8.359 [4] | 9.787 [9] | 11.214 [12] | 12.641 [9] | 14.069 [5] | 15.496 [3] | Latency distribution: 10% in 3.5637 secs. 25% in 3.6937 secs. 50% in 3.8013 secs. 75% in 3.9461 secs. 90% in 4.3431 secs. 95% in 4.4606 secs. 99% in 10.4491 secs.
Benchmark Laravel chạy trên Nginx + HHVM
Benchmark Laravel bằng công cụ Apache Benchmark (ab)
[[email protected] ~]# ab -c 20 -k -r -t 60 -v 3 http://23.88.236.66/hello Finished 1349 requests Server Software: nginx/1.4.0 Server Hostname: 23.88.236.66 Server Port: 80 Document Path: /hello Document Length: 155 bytes Concurrency Level: 20 Time taken for tests: 60.071 seconds Complete requests: 1349 Failed requests: 609 (Connect: 0, Receive: 0, Length: 609, Exceptions: 0) Write errors: 0 Non-2xx responses: 609 Keep-Alive requests: 609 Total transferred: 1039068 bytes HTML transferred: 219448 bytes Requests per second: 22.46 [#/sec] (mean) Time per request: 890.600 [ms] (mean) Time per request: 44.530 [ms] (mean, across all concurrent requests) Transfer rate: 16.89 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.9 0 14 Processing: 2 875 2049.0 355 16030 Waiting: 2 870 2046.8 355 16030 Total: 2 876 2049.3 356 16035 Percentage of the requests served within a certain time (ms) 50% 356 66% 415 75% 1073 80% 1111 90% 1991 95% 3026 98% 9421 99% 11193 100% 16035 (longest request)
Benchmark Laravel bằng công cụ Siege
[root@xtraffic ~]# siege -c20 -t60s http://23.88.236.66/hello Lifting the server siege... done. Transactions: 1247 hits Availability: 100.00 % Elapsed time: 59.77 secs Data transferred: 0.18 MB Response time: 0.44 secs Transaction rate: 20.86 trans/sec Throughput: 0.00 MB/sec Concurrency: 9.13 Successful transactions: 1247 Failed transactions: 0 Longest transaction: 2.20 Shortest transaction: 0.01
Benchmark Laravel bằng công cụ wrk
[root@xtraffic]# wrk -t10 -c20 -d60s http://23.88.236.66/hello Running 1m test @ http://23.88.236.66/hello 10 threads and 20 connections Thread Stats Avg Stdev Max +/- Stdev Latency 2.27s 1.29s 3.48s 73.67% Req/Sec 1.82 2.27 10.00 79.25% 1287 requests in 1.00m, 1.39MB read Socket errors: connect 0, read 0, write 0, timeout 20 Requests/sec: 21.44 Transfer/sec: 23.69KB
Benchmark Laravel bằng công cụ boom
[root@xtraffic]# boom -n 2000 -c 20 -cpus 1 -allow-insecure http://23.88.236.66/hello 2000 / 2000 Boooooooooooooooooooooooooo! 100.00 % Summary: Total: 80.9312 secs. Slowest: 3.1142 secs. Fastest: 0.0358 secs. Average: 0.7957 secs. Requests/sec: 24.7123 Status code distribution: [200] 2000 responses Response time histogram: 0.036 [1] | 0.344 [69] |∎∎ 0.651 [1292] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 0.959 [0] | 1.267 [21] | 1.575 [303] |∎∎∎∎∎∎∎∎∎ 1.883 [230] |∎∎∎∎∎∎∎ 2.191 [19] | 2.499 [3] | 2.806 [0] | 3.114 [62] |∎ Latency distribution: 10% in 0.3561 secs. 25% in 0.3706 secs. 50% in 0.3932 secs. 75% in 1.3797 secs. 90% in 1.6514 secs. 95% in 1.7999 secs. 99% in 3.0827 secs.
Nhìn chung khi chạy Laravel trên HHVM thì hiệu năng đã tăng đáng kể, tuy nhiên nếu so sánh với Phalcon PHP (xem chi tiết benchmark Phalcon tại đây) thì Laravel vẫn kém xa nhiều bậc về hiệu năng.
Tham khảo thêm tài liệu hướng dẫn Laravel tại đây
Trang chính thức của Laravel tại đây