使用Swoole组件Laravel-S来加速Laravel访问速度
Swoole是基于异步事件驱动和协程的并行网络通信引擎。
Laravel在启动的时候需要载入很多文件。于是我们可以通过Swoole来加速Laravel。
一:Swoole的安装
文档地址:https://wiki.swoole.com/#/
下载地址:https://github.com/swoole/swoole-src/releases,https://pecl.php.net/package/swoole,https://gitee.com/swoole/swoole/tags
// 上传压缩包 解压 tar xvf swoole-4.4.8.tgz // 进入到swoole文件 cd /www/server/swoole // 在swoole文件下调用phpize 来生成configure 文件 // ubuntu 没有安装 phpize 可执行命令:sudo apt-get install php-dev 来安装 phpize /www/server/php/74/bin/phpize //phpize 命令是用来准备 PHP 扩展库的编译环境的,可以先 ./configure --help 查看下编译所需参数 ./configure --with-php-config=/www/server/php/74/bin/php-config --enable-sockets --enable-swoole --enable-mysqlnd //编译安装 make && make install //编辑php.ini 配置文件,让 php 支持swoole extension=swoole.so //可以通过php -m 测试安装是否成功
二:原生Swoole加速Laravel
在根目录创建一个 server/http_server.php文件
<?php $http = new swoole_http_server('0.0.0.0',9501); $http->on('WorkerStart',function ($serv,$worker_id){ require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../bootstrap/app.php'; }); $http->on('request',function ($request,$response){ if (isset($request->server)) { foreach ($request->server as $k => $v) { $_SERVER[strtoupper($k)] = $v; } } if (isset($request->header)){ foreach ($request->header as $k => $v){ $_SERVER[strtoupper($k)] = $v; } } if (isset($request->get)){ foreach ($request->get as $k => $v){ $_GET[$k] = $v; } } if (isset($request->post)){ foreach ($request->post as $k => $v){ $_POST[$k] = $v; } } if (isset($request->files)){ foreach ($request->files as $k => $v){ $_FILES[$k] = $v; } } if (isset($request->cookie)){ foreach ($request->cookie as $k => $v){ $_COOKIE[$k] = $v; } } ob_start(); $kernel = app()->make(Illuminate\Contracts\Http\Kernel::class); $laravelRespose = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $laravelRespose->send(); $kernel->terminate($request,$laravelRespose); $res = ob_get_contents(); ob_end_clean(); $response->end($res); }); $http->start(); ?>
php server/http_server.php 启动服务。
前端可以用 127.0.0.1:9501 访问测试。
三:Swoole组件加速Laravel
1,Laravel-S加速。
安装Laravel-S组件
composer require hhxsv5/laravel-s --ignore-platform-reqs
发布资源文件
php artisan laravels publish
启动Laravel-S
php bin/laravels start -d //-d守护式运行
2,Swooletw加速
安装Swooletw组件
composer require swooletw/laravel-swoole
在config/app.config里添加服务提供者
<?php [ 'providers' => [ SwooleTW\Http\LaravelServiceProvider::class, ], ] ?>
在bootstrap/app.php里添加
<?php $app->register(SwooleTW\Http\LumenServiceProvider::class); ?>
启动服务
php artisan swoole:http start
四:Nginx添加反向代理
打开nginx.conf配置文件,加上反向代理。
upstream swoole { # 通过 IP:Port 连接 server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s; # 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能 #server unix:/xxxpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s; #server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s; #server 192.168.1.2:5200 backup; keepalive 16; } server { listen 80; # 别忘了绑Host哟 server_name t2.meilizhi.vip; root /www/wwwroot/xxx/public; access_log /www/wwwlogs/xxx.log; autoindex off; index index.html index.htm; # Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。 location / { try_files $uri @laravels; } # 当请求PHP文件时直接响应404,防止暴露public/*.php #location ~* \.php$ { # return 404; #} 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://swoole; } }