php网站监控告警可通过四种路径实现:一、php-fpm状态页+prometheus;二、promphp/client_php库主动暴露/metrics;三、frankenphp内置metrics模块;四、grafana可视化+alertmanager分级告警。

如果您已部署PHP网站,但缺乏对响应延迟、错误率、资源占用等关键健康指标的实时感知能力,则可能在故障发生时无法及时响应。以下是实现PHP网站监控告警的多种集成路径:
一、通过PHP-FPM状态页暴露指标并接入Prometheus
该方法无需修改应用代码,直接利用PHP-FPM内置的status接口作为数据源,由Prometheus定期抓取解析,适用于传统CGI/FPM架构。
1、编辑PHP-FPM池配置文件(如www.conf),启用状态页:
pm.status_path = /status
2、在Nginx配置中暴露该路径,确保可被Prometheus访问:
location ~ ^/status$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
3、重启PHP-FPM与Nginx服务,验证接口可用性:
curl http://your-domain.com/status?json
4、在prometheus.yml中添加scrape_configs:
- job_name: 'php-fpm'
static_configs:
- targets: ['your-domain.com:80']
metrics_path: /status
params:
format: [json]
5、启动Prometheus后,在Targets页面确认状态为UP,且Last Scrape时间持续更新。
二、使用promphp/client_php库在PHP应用中主动暴露/metrics
该方法将监控逻辑下沉至应用层,支持自定义业务指标(如订单创建数、登录失败次数),适用于微服务或Swoole常驻进程场景,指标精度更高。
1、通过Composer安装客户端:
composer require promphp/prometheus_client_php
2、在入口脚本(如index.php)顶部加载自动加载器并初始化单例Registry:
require __DIR__ . '/vendor/autoload.php';
use Prometheus\CollectorRegistry;
$registry = CollectorRegistry::getDefault();
3、注册HTTP请求计数器与响应时间直方图:
$counter = $registry->getOrRegisterCounter('php_app_http_requests_total', 'Total HTTP Requests', ['method', 'code']);
$histogram = $registry->getOrRegisterHistogram('php_app_http_request_duration_seconds', 'HTTP Request Duration', ['method']);
4、在请求处理完成前记录指标:
$counter->inc(['GET', '200']);
$histogram->observe(microtime(true) - $start_time, ['GET']);
5、添加路由响应/metrics端点,设置正确Content-Type头:
header('Content-Type: text/plain; version=0.0.4');
echo $registry->getMetricFamilySamples();
6、确保Web服务器未拦截/metrics路径,Nginx中需显式放行:
location = /metrics {
try_files $uri @php;
}
7、验证输出格式符合Prometheus协议,关键检查项为:首行必须为# HELP,且无HTML或JSON混杂内容。
三、基于FrankenPHP内置Metrics模块快速启用监控
FrankenPHP作为现代PHP应用服务器,原生支持Caddy metrics模块,无需额外编码即可导出线程、worker、请求队列等12项核心指标,适合新项目快速落地。
1、在Caddyfile根配置块中启用metrics模块:
{
metrics
}
2、为FrankenPHP服务块添加metrics路径暴露:
localhost:8080 {
php_server
}
3、启动FrankenPHP后,执行curl命令验证指标导出:
curl http://localhost:8080/metrics
4、确认返回中包含frankenphp_busy_threads、frankenphp_queue_length等指标,且类型声明正确(# TYPE ... gauge/counter)。
5、在prometheus.yml中配置目标:
- job_name: 'frankenphp'
static_configs:
- targets: ['localhost:8080']
6、Prometheus Targets页面中,该job的State必须显示UP,且Scrape Pool中无重复target。
四、配置Grafana仪表盘展示PHP网站健康度核心视图
该步骤将Prometheus采集的原始指标转化为直观可视化界面,聚焦网站可用性、性能、稳定性三大维度,支持运营人员实时掌握健康度。
1、在Grafana中添加Prometheus为数据源,URL指向Prometheus服务地址(如http://localhost:9090)。
2、新建Dashboard,添加第一个Panel,查询PHP请求总量变化趋势:
sum(rate(php_app_http_requests_total[5m])) by (job)
3、添加第二个Panel,展示HTTP错误率(5xx占比):
rate(php_app_http_requests_total{code=~"5.."}[5m]) / rate(php_app_http_requests_total[5m])
4、添加第三个Panel,监控响应时间P95分位值:
php_app_http_request_duration_seconds_bucket{le="1.5"} / ignoring(le) group_left() sum by (job) (php_app_http_request_duration_seconds_count)
5、添加第四个Panel,显示当前活跃PHP线程或worker数量(适配FrankenPHP或FPM):
frankenphp_busy_threads 或 php_fpm_active_processes
6、为每个Panel设置阈值着色规则:当P95 > 1.5s 或 错误率 > 1% 时,图表背景变为红色闪烁提示。
五、配置Alertmanager实现精准告警触发与分级通知
该方法将监控从“可观测”升级为“可响应”,依据预设规则自动判断异常,并通过多通道推送告警,避免人工盯屏遗漏。
1、在Prometheus主配置文件prometheus.yml中启用Alertmanager:
alerting:
alert_relabel_configs:
- source_labels: [severity]
regex: critical
action: keep
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
2、创建alert.rules.yml文件,定义PHP网站核心告警规则:
groups:
- name: php-website-alerts
rules:
- alert: PHPHighErrorRate
expr: rate(php_app_http_requests_total{code=~"5.."}[5m]) / rate(php_app_http_requests_total[5m]) > 0.01
for: 2m
labels:
severity: warning
annotations:
summary: "PHP网站5xx错误率过高"
description: "过去5分钟内5xx错误率超过1%,当前值为 {{ $value }}"
3、在prometheus.yml中引入该规则文件:
rule_files:
- "alert.rules.yml"
4、启动Alertmanager容器,挂载配置文件并映射端口:
docker run -d -p 9093:9093 \
-v $(pwd)/alertmanager.yml:/etc/alertmanager/alertmanager.yml \
prom/alertmanager
5、配置Alertmanager通知渠道(如邮件或钉钉),确保receiver字段中webhook URL为已验证可写入的有效地址。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











