温馨提示×

Ubuntu PHP-FPM怎么监控

小樊
39
2026-07-28 01:32:20
栏目: 编程语言

在 Ubuntu 上监控 PHP-FPM,可以从 进程状态、性能、慢日志、系统资源、告警 几个层面来做。下面给你一套从基础到进阶的完整方案。


一、确认 PHP-FPM 状态(最基础)

1️⃣ 查看 PHP-FPM 是否运行

systemctl status php-fpm
# 或 Ubuntu 常见名称
systemctl status php8.1-fpm

启动 / 重启:

systemctl restart php8.1-fpm

2️⃣ 启用并访问 PHP-FPM 状态页(强烈推荐)

修改 www.conf

vim /etc/php/8.1/fpm/pool.d/www.conf

取消注释或添加:

pm.status_path = /status
ping.path = /ping

Nginx 配置(关键)

location = /status {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    allow 127.0.0.1;
    deny all;
}

location = /ping {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    include fastcgi_params;
    allow 127.0.0.1;
    deny all;
}

访问状态页

curl http://127.0.0.1/status

输出示例:

pool:                 www
process manager:      dynamic
start time:           01/Jan/2026:10:00:00 +0800
start since:          3600
accepted conn:        12345
listen queue:         0
active processes:     5
total processes:      10

✅ 这是最核心的监控指标


二、关键监控指标(重点)

✅ PHP-FPM 状态指标

指标 含义
active processes 正在处理请求的进程
listen queue 等待队列(高说明处理不过来)
accepted conn 总请求数
slow requests 慢请求数

⚠️ listen queue > 0 长期存在 = 危险


三、开启慢日志(排查性能问题)

slowlog = /var/log/php8.1-fpm.slow.log
request_slowlog_timeout = 5s

查看慢请求:

tail -f /var/log/php8.1-fpm.slow.log

四、系统级监控(推荐)

1️⃣ 进程 & CPU & 内存

top -p $(pgrep -d',' php-fpm)
htop

2️⃣ 查看 PHP-FPM 内存占用

ps -ylC php-fpm --sort:rss

五、使用 Prometheus + Grafana(专业方案)

1️⃣ 安装 php-fpm-exporter

wget https://github.com/hipages/php-fpm_exporter/releases/download/v2.2.0/php-fpm_exporter_2.2.0_linux_amd64.rpm

2️⃣ 配置 exporter

php-fpm_exporter --phpfpm.scrape-uri="tcp://127.0.0.1/status"

3️⃣ Prometheus 抓取

- job_name: php-fpm
  static_configs:
    - targets: ["localhost:9253"]

✅ 可监控:

  • 请求数
  • 进程数
  • 队列长度
  • 慢请求

✅ Grafana 有现成 Dashboard


六、Zabbix 监控 PHP-FPM(企业常用)

Zabbix 自带模板:

  • PHP-FPM by HTTP
  • PHP-FPM status

监控内容:

  • 进程数
  • 队列
  • 响应时间
  • 可用性

七、告警建议(非常重要)

场景 建议
listen queue > 10 立即告警
active processes ≈ max children 扩容
慢请求突增 查 SQL / 代码
PHP-FPM 重启频繁 内存泄漏

八、推荐组合(实战)

中小项目

  • PHP-FPM status 页
  • 慢日志
  • top / htop

生产环境

  • PHP-FPM status
  • Prometheus + Grafana
  • 告警系统(Zabbix / Alertmanager)

如果你愿意,我可以:

  • ✅ 按 Nginx / Apache 给你单独配置
  • ✅ 给你一个 Grafana Dashboard JSON
  • ✅ 帮你算 pm.max_children 合理值

你现在用的是 Ubuntu + Nginx 还是 Apache?PHP 几版本?

0