大家都知道,性能是laravel的诟病,下面提供一种使用swoole扩展提高laravel响应速度的方法:
1、安装Swoole扩展
Swoole扩展到GitHub首页下载Swoole扩展源码,地址:https://github.com/swoole/swoole-src 下载后按照标准的PHP扩展编译方式进行编译和安装。
unzip swoole-master.zip
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make install
编译安装完后,修改 php.ini 加入 extension=swoole.so 开启swoole扩展
2、laravel 安装laravel-swoole组件
https://github.com/swooletw/laravel-swoole
composer require swooletw/laravel-swoole
然后,添加服务提供者:
如果你使用 Laravel ,在 config/app.php 服务提供者数组添加该服务提供者:
[
'providers' => [
SwooleTW\Http\LaravelServiceProvider::class,
],
]
3、启动
现在,你可以执行以下的命令来启动 Swoole HTTP 服务。
/usr/local/php/bin/php artisan swoole:http start
然后你可以看到以下信息:
Starting swoole http server...
Swoole http server started: <http://127.0.0.1:1215>
现在可以通过访问 http://127.0.0.1:1215 来进入 Laravel 应用
laravel-swoole 命令
# 启动
/usr/local/php/bin/php artisan swoole:http start
# 重启
/usr/local/php/bin/php artisan swoole:http restart
# 重载
/usr/local/php/bin/php artisan swoole:http reload
# 停止
/usr/local/php/bin/php artisan swoole:http stop
# 查看服务信息
/usr/local/php/bin/php artisan swoole:http infos
4、配合ngnix使用
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 labs {
# Connect IP:Port
server 127.0.0.1:1215 weight=5 max_fails=3 fail_timeout=30s;
keepalive 16;
}
server {
listen 80;
server_name www.xxx.com;
root /data/www/labs/public/;
access_log /var/log/nginx/xxx.cn.access.log main;
error_log /var/log/nginx/xxx.cn.error.log;
autoindex off;
index index.html index.htm;
location / {
try_files $uri @laravels;
}
location @laravels {
# proxy_connect_timeout 60s;
# proxy_send_timeout 60s;
# proxy_read_timeout 120s;
proxy_http_version 1.1;
proxy_set_header Connection "";
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://labs;
}
}
重启nginx