MixPHP V3 开发流程体验 Swoole, Workerman, FPM, CLI-Server 多种运行模式介绍
撸代码的乡下人 -MixPHP V3
发布后,由于本身支持超多的执行模式,用户可能无从下手,这里先大体介绍一下:
Swoole 的多种模式:
Swoole 多进程同步: 适合需要使用那些协程不支持的第三方库的项目,和 Workerman 一致Swoole 多进程协程: 适合专注 mysql + redis 需要超高 io 性能的项目Swoole 单进程协程: 单进程协程就是V2.2
版本那种 golang 风格协程,适合开发 websocket几乎支持 PHP 流行的全部执行模式,并且以上执行模式代码是无缝切换的,真正做到效率与性能并存。
请帮忙 Star 一下:
https://github.com/mix-php/mixhttps://gitee.com/mix-php/mix首先创建一个骨架我们以开发一个 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!
接下来就可以根据文档:
编写一个 API 接口使用 PHP-FPM 部署共享开发环境热更新是刚性需求,所以共享开发环境我们直接采用 PHP-FPM 部署,和 Laravel、ThinkPHP 部署方法完全一致,将 public/index.php
在 nginx
配置 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
php介绍
PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。