laravel 使用 swoole

阅读 (183)
laravel 使用 swoole

在运营的过程中,Laravel框架由于过于笨重导致磁盘IO时不时爆满,导致服务卡顿,更无法承载大量用户使用,此时可以借助Swoole引擎来优化,将整个Laravel永久驻留在内存中,避免重复加载。

首先安装Swoole: 

$ pecl install swoole

如果使用宝塔面板,直接在软件管理中安装swoole4即可

安装后,使用php -m | grep swoole命令来检查是否安装成功

然后在Laravel项目中安装laravel-swoole

$ composer require swooletw/laravel-swoole

如果安装时提示以下错误:

PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 32 bytes)

将php.ini中修改一下配置

memory_limit = 128M
// 改为
memory_limit = -1

然后复制配置:命令行的php版本最好>=7.2

$ php artisan vendor:publish --tag=laravel-swoole


swoole_http_server只能在cli环境中运行,这个包提供了命令方便管理。默认情况下,您可以访问您的站点http://127.0.0.1:1215

$ php artisan swoole:http {start|stop|restart|reload|infos}

 

命令 描述
start 启动 Laravel Swoole, list the processes by ps aux|grep swoole
stop 停止 Laravel Swoole
restart 重启 Laravel Swoole
reload 重新加载 (包含你的业务和Laravel/Lumen代码),不包括master/manger进程
infos 显示 PHP 和 Swoole 基本信息(包括PHP版本、Swoole版本、Laravel版本、服务器状态和PID)

用以下命令启动swoole

$ php artisan swoole:http start

显示基本信息

$ php artisan swoole:http infos
+-----------------+----------------------------------------------------+
| Name            | Value                                              |
+-----------------+----------------------------------------------------+
| PHP Version     | 7.2.32                                             |
| Swoole Version  | 4.5.2                                              |
| Laravel Version | 7.22.4                                             |
| Listen IP       | 127.0.0.1                                          |
| Listen Port     | 1215                                               |
| Server Status   | Online                                             |
| Reactor Num     | 2                                                  |
| Worker Num      | 2                                                  |
| Task Worker Num | 0                                                  |
| Websocket Mode  | Off                                                |
| Master PID      | 17704                                              |
| Manager PID     | 17708                                              |
| Log Path        | /home/wwwroot/your-project/storage/logs/swoole_http.log |
+-----------------+----------------------------------------------------+

在正式环境中我们不可能让用户访问 http://127.0.0.1:1215 进到我们服务中,此时需要对nginx进行配置

nginx 配置:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
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;
    }
    # any php files must not be accessed
    #location ~* \.php$ {
    #    return 404;
    #}
    location / {
        try_files $uri $uri/ @swoole;
    }

    location @swoole {
        set $suffix "";

        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }

        proxy_http_version 1.1;
        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;
    }
}

 

更新于:2020-07-31 17:11:33
返回顶部