天天看点

为什么swoole会加速php,laravels -- Swoole加速php

LaravelS是一个胶水项目,用于快速集成Swoole到Laravel,然后赋予它们更好的性能、更多可能性.

环境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高性能php服务器。

依赖

说明

>= 5.5.9

>= 1.7.19 推荐最新的稳定版 从2.0.12开始不再支持PHP5

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

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;

}

}