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