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 一下:
- https://github.com/mix-php/mix
- https://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.phpPHP 7.3.24-(to be removed in future macOS) Development Server started at Tue Aug 10 17:00:55 2021Listening on http://localhost:8000Document root is /Users/***/mix/examples/api-skeletonPress Ctrl-C to quit.
测试一下默认的路由
% curl http://127.0.0.1:8000/hellohello, 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/phpfile=/data/project/bin/swoole.phpcmd=startnumprocs=1
启动治理
sh /data/project/shell/server.sh startsh /data/project/shell/server.sh stopsh /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