要让 Prometheus 采集 Apache 状态数据,需启用 mod_status 模块暴露 /server-status?auto 接口,再通过 apache_exporter 转换为 Prometheus 格式 /metrics,并在 prometheus.yml 中配置 scrape job 拉取 localhost:9117。

要让 Prometheus 采集 Apache 的状态数据,核心是把 Apache 的运行指标“翻译”成 Prometheus 能理解的文本格式(即 /metrics 接口)。Apache 自身不原生输出 Prometheus 格式,必须通过中间适配层实现。最常用、最稳定的方式是启用 mod_status 模块 + apache_exporter。
启用并配置 mod_status 模块
这是整个链路的前提。Apache 需先暴露结构化状态页(/server-status?auto),供 exporter 解析。
- 确认已加载模块:在 Apache 配置中确保存在
LoadModule status_module modules/mod_status.so - 启用状态页:添加或修改
<Location "/server-status">区块,设置SetHandler server-status - 控制访问权限:用
Require local限制本地访问,或明确允许 Prometheus 服务器的 IP,例如Require ip 192.168.1.100 - 重启 Apache 生效:
systemctl restart apache2或apachectl graceful - 验证是否可用:
curl http://localhost/server-status?auto应返回类似ScoreBoard:____W___...的纯文本内容
部署并运行 apache_exporter
它是一个轻量级服务,持续抓取 /server-status?auto 页面,再转换为 Prometheus 格式的 /metrics 接口。
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
- 下载对应平台的二进制文件(如
apache_exporter-0.12.0.linux-amd64.tar.gz) - 解压后直接运行:
./apache_exporter --scrape_uri http://localhost/server-status?auto - 默认监听端口
:9117,访问http://localhost:9117/metrics可看到apache_up 1、apache_workers_total 256等指标 - 建议用 systemd 托管,保证长期运行
在 Prometheus 中添加采集任务
告诉 Prometheus 去哪里拉取 exporter 暴露的指标。
- 编辑
prometheus.yml,在scrape_configs下新增 job:
- job_name: 'apache'
static_configs:
- targets: ['localhost:9117']
```
- 保存后重载配置:
curl -X POST http://localhost:9090/-/reload(需开启--web.enable-admin-api) - 进入 Prometheus Web UI 的 Status → Targets 页面,确认该 job 状态为 UP
可选增强:补充日志维度指标
mod_status 提供的是汇总型指标(如请求数、并发连接数),若需响应时间分布、URL 路径统计、错误码明细等,可结合访问日志解析:
- 用
mtail或promtail + loki实时解析access.log,提取延迟、状态码、路径等字段并转为指标 - 典型指标如:
apache_http_response_duration_seconds_bucket{le="0.1",code="200",path="/api/user"} - 注意日志格式需统一(推荐使用
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" combined,其中%D是微秒级响应时间)

















