Docker中安装 laravel 与 Swoole扩展

谢阳成
2023-12-01

laravel与Swoole

参考文档

https://gitee.com/hhxsv5/laravel-s

php的docker容器中配置swoole

1.获取swoole安装包

https://pecl.php.net/package/swoole

2.解压swoole安装包

tar –zxvf swoole-4.6.6.tar

3.将解压出来的安装包copy到php容器

docker cp /home/swoole php:/usr/src/php/ext/swoole

4.安装swoole

docker-php-ext-install swoole

5.完成安装后,查看swoole信息

php --ri swoole

laravel配置swoole

  1. 在 Laravel 应用中使用 Swoole 之前,先通过 Composer 安装 LaravelS 扩展包
composer require hhxsv5/laravel-s

2.运行如下 Artisan 命令相应脚本和配置文件发布到根目录下:

php artisan laravels publish

3.在.env 修改工作进程数并重启服务

LARAVELS_LISTEN_IP=0.0.0.0
LARAVELS_LISTEN_PORT=5200
LARAVELS_WORKER_NUM=4

4.启动 LaravelS

php bin/laravels start

5.通过 Supervisor 管理 LaravelS

[program:laravel-s-test]
command=php /www/wwwroot/lms/blog/bin/laravels start -i
numprocs=1
autostart=true
autorestart=true
startretries=3
user=root
redirect_stderr=true
stdout_logfile=/www/wwwroot/lms/blog/storage/logs/supervisord-stdout.log

nginx反向代理swoole使用

1.nginx.conf配置

upstream swoole {
   server 172.17.0.3:5200 weight=5 max_fails=3 fail_timeout=30s;
   keepalive 16;
}

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root /docker/www/lmrs-2008/public;
    index index.php index.html;

    location / {
        try_files $uri @laravels;
    }

    location @laravels {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        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://swoole;
    }
}

swoole定时器携程问题

新创建一个php脚本.在bin/laravels启动文件中引入脚本

<?php
/**
 * 初始化Go携程参数
 */
if (! function_exists('go')) {
    /**
     * @return bool|int
     */
    function go(callable $callable)
    {
        $id = \Swoole\Coroutine::create($callable);

        return $id > 0 ? $id : false;
    }
}

if (! function_exists('co')) {
    /**
     * @return bool|int
     */
    function co(callable $callable)
    {
        $id = \Swoole\Coroutine::create($callable);

        return $id > 0 ? $id : false;
    }
}

if (! function_exists('defer')) {
    function defer(callable $callable): void
    {
        \Swoole\Coroutine::defer($callable);
    }
}

 类似资料: