FrankenPHP实践
目录
1. 说明
2. 程序修改
3. 性能测试
4. 配置
4.1 Docker化部署
4.2 泛域名和证书设置
4.3 相关命令
5. 要点:
6. 参考
1. 说明
Frankenphp是一个先进的,结合了高性能Caddy服务器的PHP环境框架,它允许用户只需要少量改动,就能让传统的php前端应用插上翅膀,享受到异步服务带来的量级提升体验,官网:https://frankenphp.dev。
它实际上是有两种运行模式:普通模式和worker模式,其中的普通模式类似于传统的LNMP容器,只是Nginx换成了Caddy Server,带来的提升并不特别明显。而worker模式就不同了,效果是原来性能的三倍左右。
2. 程序修改
以TP6为例,用worker模式的话,入口程序需改由frankenphp_handle_request包裹使用,参考官网docs: FrankenPHP: the modern PHP app server
<?php
ignore_user_abort(true);
require __DIR__ . '/../vendor/autoload.php';
$thinkApp = new \think\App();
$http = $thinkApp->http;
$handler = static function () use ($http) {
// 初始化并处理请求
$response = $http->run();
// 发送响应
$response->send();
// 返回响应后终止应用
$http->end($response);
};
$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0);
for ($nbRequests = 0; !$maxRequests || $nbRequests < $maxRequests; ++$nbRequests) {
// 处理请求
$keepRunning = \frankenphp_handle_request($handler);
// 执行一些在发送 HTTP 响应后的操作
gc_collect_cycles(); // 垃圾回收
if (!$keepRunning) break;
}
3. 性能测试
Performance benchmark of PHP runtimes - DEV Community
4. 配置
4.1 Docker化部署
docker-compose.yml,其中: 证书路径映射到 /data0/Server/Auths/certs
# compose.yaml
services:
php:
container_name: frankenphp
# image: dunglas/frankenphp
build:
dockerfile: frankenphp.Dockerfile
context: ./docker
restart: always
# restart: unless-stopped
# uncomment the following line if you want to use a custom Dockerfile
#build: .
environment:
SERVER_NAME: api-test001.xxx.com, api-test001a.xxx.com, php:80
MAX_REQUESTS: 600
FRANKENPHP_CONFIG: |
worker {
file /app/public/index.php
num 42
watch
}
#CADDY_SERVER_EXTRA_DIRECTIVES: try_files {path} {path}/ /index.php?s=/{path}&{query}
CADDY_SERVER_EXTRA_DIRECTIVES: |
try_files {path} {path}/ /index.php?s=/{path}&{query}
ports:
- "80:80" # HTTP
- "443:443" # HTTPS
- "443:443/udp" # HTTP/3
volumes:
# - /data0/Projects/Test/test001:/app/public
#- /home/website/ad_serving_backend:/app # App src
- /home/website/tp:/app # App src
- /data0/Server/Settings/caddy/Caddyfile:/etc/caddy/Caddyfile
- /data0/Server/Db/caddy:/data
- /data0/Server/Settings/caddy/config:/config
- /data0/Server/Auths/certs:/certs
# comment the following line in production, it allows to have nice human-readable logs in dev
tty: true
4.2 泛域名和证书设置
尽管Caddy自带无敌好感的自注册安全证书机制,但有时候我们还是需要手动部署一些已有的证书(nginx原有证书即可),这时我们要编辑Caddyfile做定制:
*.xxx.com {
#log {
# # Redact the authorization query parameter that can be set by Mercure
# format filter {
# request>uri query {
# replace authorization REDACTED
# }
# }
#}
root * public/
encode zstd br gzip
tls /certs/xxx.com/xxx.com.pem /certs/xxx.com/xxx.com.key
{$CADDY_SERVER_EXTRA_DIRECTIVES}
php_server
}
4.3 相关命令
docker compose down && docker compose up -d --wait
# 修改配置后重拉容器
docker compose down && docker compose -f /data0/Server/Settings/docker-compose/frankenphp/docker-compose.yml up -d --wait
# restart
docker compose -f /data0/Server/Settings/docker-compose/frankenphp/docker-compose.yml restart
# 日志
docker logs frankenphp
5. 要点:
如果自建镜像扩展插件,需三思是否需要加Opcache,开启后 frankenphp 的watch功能(监控文件修改自动热启)将不能正常使用,但开启后,性能将提高40%左右。
6. 参考
- FrankenPHP: the modern PHP app server
- Performance benchmark of PHP runtimes - DEV Community