天天看點

為什麼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;

}

}