MixPHP V3 开发流程体验 Swoole, Workerman, FPM, CLI-Server 多种运行模式介绍 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
onanying
V2EX    PHP

MixPHP V3 开发流程体验 Swoole, Workerman, FPM, CLI-Server 多种运行模式介绍

  •  
  •   onanying 2021-08-11 15:53:23 +08:00 2151 次点击
    这是一个创建于 1522 天前的主题,其中的信息可能已经有所发展或是发生改变。

    MixPHP V3 发布后,由于本身支持超多的执行模式,用户可能无从下,这里先大体介绍一下:

    • CLI-Server: 适合本机开发,零扩展依赖,Windows/MacOS 等全平台支持
    • PHP-FPM: 适合共享开发环境部署,同时适合 admin 等管理后台项目
    • Swoole, Workerman: 适合线上部署,根据需要选择其一即可

    Swoole 的多种模式:

    • Swoole 多进程同步: 适合需要使用那些协程不支持的第三方库的项目,和 Workerman 一致
    • Swoole 多进程协程: 适合专注 mysql + redis 需要超高 io 性能的项目
    • Swoole 单进程协程: 单进程协程就是 V2.2 版本那种 golang 风格协程,适合开发 websocket

    几乎支持 PHP 流行的全部执行模式,并且以上执行模式代码是无缝切换的,真正做到效率与性能并存。

    请帮忙 Star 一下:

    首先创建一个骨架

    我们以开发一个 API 项目为例,打开 MixPHP 的 开发文档 里面有 cli api web websocket grpc 项目的开发教程,V3 开始仓库底下的 README 就是开发文档,如果有不明白的可以加我们的 官方 QQ 群 参与讨论。

    • 首先创建一个骨架

    如果提示缺少 redis 等扩展支持,可以使用 --ignore-platform-reqs 暂时忽略依赖检查

    composer create-project --prefer-dist --ignore-platform-reqs mix/api-skeleton api 

    安装后目录结构如下:

    • bin 目录是全部入口文件,不同文件对应的不同驱动模式
    • routes 是路由配置文件
    • public/index.php 是 FPM, CLI-Server 两种模式的入口文件
    • shell/server.sh 是部署是管理进程 start|stop|restart
    ├── README.md ├── bin │ ├── cli.php │ ├── swoole.php │ ├── swooleco.php │ └── workerman.php ├── composer.json ├── composer.lock ├── conf │ └── config.json ├── public │ └── index.php ├── routes │ └── index.php ├── runtime ├── shell │ └── server.sh ├── src │ ├── Command │ ├── Container │ ├── Controller │ ├── Error.php │ ├── Middleware │ ├── Vega.php │ └── functions.php └── vendor 

    使用 CLI-Server 零扩展依赖模式本机开发

    首先我们查看一下 composer.json,与其他框架不同的是我们推荐在本机开发阶段使用 composer run-script 启动程序,可以和 PhpStorm 的调试功能完美配合。

    • 这里定义了每个执行模式的命令入口文件
    • composer run-script --timeout=0 cliserver:start 就可以启动命令
     "scripts": { "cliserver:start": "php -S localhost:8000 public/index.php", "swoole:start": "php bin/swoole.php", "swooleco:start": "php bin/swooleco.php", "workerman:start": "php bin/workerman.php start", "cli:clearcache": "php bin/cli.php clearcache" } 

    由于现在是本机开发,我们使用 CLI-Server 模式启动,零扩展依赖,无需 pcntl, event, swoole 这些扩展,自带热更新。

    % composer run-script --timeout=0 cliserver:start > php -S localhost:8000 public/index.php PHP 7.3.24-(to be removed in future macOS) Development Server started at Tue Aug 10 17:00:55 2021 Listening on http://localhost:8000 Document root is /Users/***/mix/examples/api-skeleton Press Ctrl-C to quit. 

    测试一下默认的路由

    % curl http://127.0.0.1:8000/hello hello, world! 

    接下来就可以根据文档:

    使用 PHP-FPM 部署共享开发环境

    热更新是刚性需求,所以共享开发环境我们直接采用 PHP-FPM 部署,和 Laravel 、ThinkPHP 部署方法完全一致,将 public/index.phpnginx 配置 rewrite 重写即可。

    server { server_name www.domain.com; listen 80; root /data/project/public; index index.html index.php; location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } } location ~ ^(.+\.php)(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 

    使用 Swoole 多进程协程模式线上部署

    Swoole 、Workerman 你可以随意选择,这里我们采用 Swoole 举例。

    • 首先安装 Swoole 扩展
    • 修改 shell/server.sh 脚本中的绝对路径和参数

    这里我们选择的 Swoole 多进程协程模式,因此入口文件为 bin/swoole.php,其他模式参考 composer.json

    php=/usr/local/bin/php file=/data/project/bin/swoole.php cmd=start numprocs=1 

    启动管理

    sh /data/project/shell/server.sh start sh /data/project/shell/server.sh stop sh /data/project/shell/server.sh restart 

    接下来将启动命令加入 crontab 防止程序异常中断

    */1 * * * * sh /data/project/shell/server.sh start > /tmp/server.sh.log 2>&1 & 

    当修改代码时,使用 restart 让代码生效

    sh /data/project/shell/server.sh restart 
    2 条回复    2021-08-12 16:16:09 +08:00
    bihui
        1
    bihui  
       2021-08-12 11:40:47 +08:00
    虽然看不懂,帮顶
    onemillet
        2
    onemillet  
       2021-08-12 16:16:09 +08:00
    fork 了~
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5483 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 08:33 PVG 16:33 LAX 01:33 JFK 04:33
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86