LaravelS
是一個膠水項目,用於快速集成Swoole
到Laravel
,然后賦予它們更好的性能、更多可能性.
環境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高性能php服務器。
依賴 | 說明 |
---|---|
PHP | >= 5.5.9 |
Swoole | >= 1.7.19 推薦最新的穩定版 從2.0.12開始不再支持PHP5 |
Laravel/Lumen | >= 5.1 |
Gzip[可選的] | zlib,用於壓縮HTTP響應,檢查本機libz 是否可用 ldconfig -p|grep libz |
Inotify[可選的] | inotify,用於修改代碼后自動Reload Worker進程,檢查本機inotify 是否可用 php --ri inotify |
1:環境監測
php -v (檢查php)
cc -v (檢查 gcc)
openssl version (檢查openssl)
php --ri swoole (檢查 swoole)
2:安裝swoole
sudo wget https://github.com/swoole/swoole-src/archive/v4.0.4.zip
sudo unzip v4.0.4.zip
*** 如果沒有unzip,先安裝 sudo apt install unzip 再 sudo unzip v4.0.4.zip
cd swoole-src-4.0.4 (進入解壓包)
sudo phpize
***如果沒有phpize 先安裝 sudo apt-get install php7.1-dev 再 sudo phpize
sudo ./configure
sudo make
sudo make install
安裝成功后,在php中加入so
位置 /etc/php/7/1/cli/php.ini
添加配置 extension=swoole.so
檢查版本 php --ri swoole
3:安裝laravels
注:若還沒有 Laravel 環境請參考 https://www.cnblogs.com/cj8988/p/9454142.html
1、在你的Laravel/Lumen項目的根目錄下執行
composer require "hhxsv5/laravel-s:~3.0" -vvv
2、注冊Service Provider
Laravel: 修改文件config/app.php,Laravel 5.5+支持包自動發現,你應該跳過這步
'providers' => [
Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],
3、發布配置文件,每次升級LaravelS后,建議重新發布一次配置文件
php artisan laravels publish
4、.修改配置config/laravels.php:監聽的IP、端口等。
5、運行
php artisan laravels {start|stop|restart|reload|publish}
命令 | 說明 |
---|---|
start |
啟動LaravelS,展示已啟動的進程列表 ps -ef|grep laravels |
stop |
停止LaravelS |
restart |
重啟LaravelS |
reload |
平滑重啟所有worker進程,這些worker進程內包含你的業務代碼和框架(Laravel/Lumen)代碼,不會重啟master/manger進程 |
publish |
發布配置文件到你的項目中config/laravels.php |
4、配置nginx
在 nginx.conf 配置中添加
gzip on;
gzip_min_length 1024;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
upstream laravels { # By IP:Port server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s; }
在service配置中添加
server {
listen 80;
server_name laravels.com;
root /xxxpath/laravel-s-test/public;
access_log /yyypath/log/nginx/$server_name.access.log main;
autoindex off;
index index.html index.htm;
# Nginx處理靜態資源(建議開啟gzip),LaravelS處理動態資源。
location / {
try_files $uri @laravels;
}
location @laravels { proxy_http_version 1.1; proxy_set_header Connection "keep-alive"; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header Scheme $scheme; proxy_set_header Server-Protocol $server_protocol; proxy_set_header Server-Name $server_name; proxy_set_header Server-Addr $server_addr; proxy_set_header Server-Port $server_port; proxy_pass http://laravels; }
}
參考文檔 https://laravel-china.org/articles/8050/laravels-speed-up-laravellumen-through-swoole
https://github.com/hhxsv5/laravel-s