2019年1月21日 星期一

《Swoole加速Laravel 5》ubuntu環境配置Nginx+php-fpm7.2

請確保你的機器安裝了正確的Swoole 。使用下面的命令快速安裝

方法一 pecl install swoole
sudo apt-get php7.2-fpm php7.2-mysql php7.2-curl php7.2-gd php7.2-mbstring php7.2-xml php7.2-xmlrpc php7.2-zip php7.2-opcache php7.2-dev -y

方法二 git clone https://github.com/swoole/swoole-src.git

複製後,依序指令

cd swoole-src
phpize
./configure
make
make install

修改 php.ini 導入模組,使用的這版 php 跟以前那種直接去 php.ini 新增 extension=swoole.so 不太一樣,而是在安裝目錄下載入 module。

cd /etc/php/7.2/mods-available/
# 隨便複製一個 ini
cp mysqli.ini swoole.ini
vim swoole.ini
# 把 extension=mysqli.so 改成 extension=swoole.so
cd /etc/php/7.2/cli/conf.d/
ln -s /etc/php/7.2/mods-available/swoole.ini 20-swoole.ini
sudo service nginx restart
# 檢查是否正確安裝
php -m | grep 'swoole'

添加swoole擴展到php環境中

echo 'extension=swoole.so' >> /etc/php/7.2/mods-available/swoole.ini
cd /etc/php/7.2/cli/conf.d/ && ln -s ../../mods-available/swoole.ini 20-swoole.ini
cd /etc/php/7.2/fpm/conf.d/ && ln -s ../../mods-available/swoole.ini 20-swoole.ini

在Laravel中使用swoole来取代nginx作为http伺服器

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
server {
    listen 80;
    server_name your.domain.com;
    root /var/www/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 ?$query_string;
        }

        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        # IF https
        # proxy_set_header HTTPS "on";

        proxy_pass http://127.0.0.1:1215$suffix;
    }
}

使用Composer安裝:

composer require swooletw/laravel-swoole

如果你使用Laravel ,在  config/app.php服務提供者數組添加該服務提供者:
'providers' => [
    SwooleTW\Http\LaravelServiceProvider::class,
],

現在,你可以執行以下的命令來啟動Swoole HTTP 服務。
php artisan swoole:http start

swoole server 會搞亂Laravel 的Session

基於這個拓展包開發程序時,請注意務必應用中單例的使用,如果存在影響下一次請求的單例或變量,必須在當前請求結束時處理掉。因為swoole會使你的程序運行在命令行模式下,並使程序常駐內存。

舉例:如果你使用了Laravel提供的驗證,那麼你可以在當前請求結束時重新綁定一次權威性單例(這樣可以使保護類裡的$用戶變量釋放掉,就不會影響下一次請求了):

// File: app/Providers/AuthServiceProvider.php

namespace App\Providers;

use Illuminate\Auth\AuthManager;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{

    public function boot()
    {
        // 不相關的代碼已省略掉

        $this->terminateAuth();
    }

    protected function terminateAuth()
    {
        $this->app->terminating(function () {
            // 重新綁定auth單例
            $this->app->singleton('auth', function ($app) {
                $app['auth.loaded'] = true;

                return new AuthManager($app);
            });
        });
    }
}

Nginx壓測 ab -t 60 -n 500 -c 100 -k -r http://127.0.0.1/login

Document Path:          /login
Document Length:        5878 bytes

Concurrency Level:      100
Time taken for tests:   2.983 seconds
Complete requests:      500
Failed requests:        0
Keep-Alive requests:    0
Total transferred:      3391816 bytes
HTML transferred:       2939000 bytes
Requests per second:    167.61 [#/sec] (mean)
Time per request:       596.638 [ms] (mean)
Time per request:       5.966 [ms] (mean, across all concurrent requests)
Transfer rate:          1110.33 [Kbytes/sec] received

Document Path:          /phpinfo.php
Document Length:        91256 bytes

Concurrency Level:      100
Time taken for tests:   3.326 seconds
Complete requests:      500
Failed requests:        211
   (Connect: 0, Receive: 0, Length: 211, Exceptions: 0)
Keep-Alive requests:    0
Total transferred:      45722686 bytes
HTML transferred:       45646186 bytes
Requests per second:    150.35 [#/sec] (mean)
Time per request:       665.125 [ms] (mean)
Time per request:       6.651 [ms] (mean, across all concurrent requests)
Transfer rate:          13426.36 [Kbytes/sec] received

Swoole壓測 ab -t 60 -n 500 -c 100 -k -r http://127.0.0.1/login

Document Path:          /login
Document Length:        5878 bytes

Concurrency Level:      100
Time taken for tests:   1.371 seconds
Complete requests:      500
Failed requests:        0
Keep-Alive requests:    0
Total transferred:      3377826 bytes
HTML transferred:       2939000 bytes
Requests per second:    364.69 [#/sec] (mean)
Time per request:       274.204 [ms] (mean)
Time per request:       2.742 [ms] (mean, across all concurrent requests)
Transfer rate:          2405.99 [Kbytes/sec] received

Document Path:          /phpinfo.php
Document Length:        19 bytes

Concurrency Level:      100
Time taken for tests:   0.250 seconds
Complete requests:      500
Failed requests:        0
Keep-Alive requests:    500
Total transferred:      131500 bytes
HTML transferred:       9500 bytes
Requests per second:    2002.27 [#/sec] (mean)
Time per request:       49.943 [ms] (mean)
Time per request:       0.499 [ms] (mean, across all concurrent requests)
Transfer rate:          514.26 [Kbytes/sec] received

沒有留言:

張貼留言