8

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

 3 years ago
source link: https://segmentfault.com/a/1190000040494081
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK