`laravel`框架很优秀,但是也有点缺点,就是它的访问速度有点慢,因为加载了太多的模块
今天我们介绍下用`swoole`来加速`laravel`
首先在网站`http://pecl.php.net/package/swoole`下载安装包,安装php的swoole的扩展
```
[root@dev ~]# cd /usr/local/src
[root@dev src]# wget http://pecl.php.net/get/swoole-4.3.2.tgz
[root@dev src]# tar -zvxf swoole-4.3.2.tgz
[root@dev src]# cd swoole-4.3.2[root@dev swoole-1.9.3]# phpize
[root@dev swoole-4.3.2]# ./configure
[root@dev swoole-4.3.2]# make && make install
如果出错看时否缺少pcre-devel,执行yum install pcre-devel
然后在php.ini里配置
[root@dev swoole-4.3.2]# vim php.ini
extension=swoole.so
如果出现问题
can not found php-config
说明缺少php-devel
执行该命令:yum install php-devel
```
接着在`laravel`项目中安装扩展
`composer require swooletw/laravel-swoole`
执行`php artisan vendor:publish` 选择`laravel-swoole`
将以下代码加入config/swoole_http.php
```
'server' => [
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
'port' => env('SWOOLE_HTTP_PORT', '1215'),
'options' => [
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', 1),
],
],
```
启动swoole `php artisan swoole:http start`
然后在`nginx`中配置代理即可
```
server {
listen 80;
server_name your.domain.com;
root /path/to/laravel/public;
index index.php;
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix "/";
}
proxy_set_header Host $host;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# IF https
# proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$suffix;
}
}
```
然后可以像平时一样访问自己的网站了