Grafana Cloud 监控设置 (Apache + PHP-FPM + Alloy)
发布: (2025年12月10日 GMT+8 12:42)
3 min read
原文: Dev.to
Source: Dev.to
第一步:在 EC2 上安装 Grafana Alloy
- 前往 Grafana Cloud → Collector → Configure。
- 创建一个新的 API token。
- 将 Grafana 提供的安装命令复制粘贴到你的 EC2 实例中。
安装完成后,运行:
cd /etc/alloy
打开配置文件:
sudo nano config.alloy
- 粘贴 Alloy 配置(
config.alloy)。(不要更改 Prometheus、Loki 和 remotecfg 的凭证。) - 如需接收
config.alloy文件,可在注释中写上你的 GitHub 账户。
重启 Alloy:
sudo systemctl restart alloy.service
确认 Alloy 已在 Grafana Cloud 中连接。
第二步:在 Grafana 中验证系统指标
- 前往 Grafana Cloud → Fleet Management。
- 选择 EC2 实例(通过 IP 识别)。
- 使用 Explore 验证 CPU、内存、磁盘等系统指标是否已收到。
第三步:启用 Apache mod_status
编辑 status.conf:
sudo nano /etc/apache2/mods-available/status.conf
添加(如果不存在):
SetHandler server-status
Require local
重启 Apache:
sudo systemctl restart apache2.service
第四步:启用 PHP‑FPM 状态与 Ping 页面
4.1 更新 www.conf
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
添加或修改以下指令:
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
ping.path = /ping
pm.status_path = /status
listen = /run/php/php8.1-fpm.sock
重启 PHP‑FPM:
sudo systemctl restart php8.1-fpm
确认套接字文件 /run/php/php8.1-fpm.sock 已存在。
4.2 创建 php-fpm-status.conf
sudo nano /etc/apache2/conf-available/php-fpm-status.conf
粘贴:
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
重启服务:
sudo systemctl restart apache2
sudo systemctl restart php8.1-fpm
第五步:启用所需的 Apache 模块
sudo a2enmod proxy proxy_fcgi setenvif
sudo a2enconf php-fpm-status
sudo systemctl restart apache2
第六步:安装 PHP‑FPM Exporter
6.1 下载并安装 Exporter
wget https://github.com/hipages/php-fpm_exporter/releases/download/v2.2.0/php-fpm_exporter_2.2.0_linux_amd64.tar.gz
tar -xvzf php-fpm_exporter_2.2.0_linux_amd64.tar.gz
sudo mv php-fpm_exporter /usr/local/bin/
6.2 创建 Exporter 用户
sudo useradd --system --no-create-home --shell /usr/sbin/nologin phpfpm_exporter
sudo usermod -aG www-data phpfpm_exporter
6.3 创建 Systemd 服务
sudo nano /etc/systemd/system/php-fpm-exporter.service
粘贴:
[Unit]
Description=PHP-FPM Prometheus Exporter
After=network.target
[Service]
User=phpfpm_exporter
Group=www-data
ExecStart=/usr/local/bin/php-fpm_exporter server \
--phpfpm.scrape-uri='unix:///run/php/php8.1-fpm.sock;/status' \
--web.listen-address=':9253'
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
6.4 启动服务
sudo systemctl daemon-reload
sudo systemctl enable --now php-fpm-exporter
sudo systemctl status php-fpm-exporter
6.5 测试指标
curl http://127.0.0.1/status
curl http://127.0.0.1:9253/metrics
重启 Alloy 以加载新 Exporter:
sudo systemctl restart alloy
第七步:在 Grafana 中验证 PHP‑FPM 指标
- 在 Grafana Cloud → Explore 中搜索 PHP‑FPM 指标,例如:
phpfpm_active_processes
phpfpm_requests_total
phpfpm_slow_requests
- 确认 Apache、系统以及 PHP‑FPM 指标已全部显示。