Apache在CentOS上的性能监控方法
mod_status是Apache自带的内置模块,无需额外安装,可实时查看Apache的请求处理数、工作线程状态、CPU/内存占用等基础性能指标。
配置步骤:
/etc/httpd/conf/httpd.conf
或/etc/apache2/apache2.conf
),添加/修改以下内容:<IfModule mod_status.c>
ExtendedStatus On # 开启详细状态信息(可选,但建议开启)
<Location /server-status>
SetHandler server-status
Require local # 仅允许本地访问(生产环境可改为IP白名单)
</Location>
</IfModule>
sudo systemctl restart httpd
。http://your_server_ip/server-status
,即可查看实时性能数据(如“Total Accesses”表示总请求数、“BusyWorkers”表示繁忙工作线程数)。Netdata是轻量级实时监控工具,提供Web仪表盘,可监控Apache的请求速率、响应时间、错误数及系统资源(CPU、内存、磁盘、网络)等指标。
安装与使用:
sudo yum install netdata -y
。sudo systemctl start netdata
、sudo systemctl enable netdata
。http://your_server_ip:19999
,默认用户名/密码为空,进入后可查看Apache相关监控项。Glances是跨平台的实时监控工具,支持监控Apache的进程、资源占用及网络流量,适合快速排查性能瓶颈。
安装与使用:
sudo yum install glances -y
。sudo systemctl start glances
(或直接运行glances
命令进入交互界面)。Monit是进程监控工具,可监控Apache服务的运行状态,自动重启异常停止的服务,并发送邮件报警。
安装与配置:
sudo yum install monit -y
。/etc/monit.d/httpd
,添加以下内容:check process httpd with pidfile /var/run/httpd.pid
start program = "/usr/sbin/apachectl start"
stop program = "/usr/sbin/apachectl stop"
if failed host 127.0.0.1 port 80 protocol http then restart
if 5 restarts within 5 cycles then timeout
sudo systemctl start monit
、sudo systemctl enable monit
。适合大规模部署,通过prometheus_apache_exporter
收集Apache指标,Prometheus存储数据,Grafana可视化展示(如请求速率趋势、错误率分布)。
安装与配置:
wget https://github.com/Lusitaniae/apache_exporter/releases/download/v0.11.0/apache_exporter-0.11.0.linux-amd64.tar.gz
,解压后运行./apache_exporter
(默认监听9117端口)。prometheus.yml
中添加Apache Exporter的job:scrape_configs:
- job_name: 'apache'
static_configs:
- targets: ['localhost:9117']
sudo yum install grafana -y
,启动后导入Apache监控模板(如ID:1860)。通过Shell脚本定期检查Apache进程状态,若异常则自动重启,并通过Cron定时执行(如每5分钟一次)。
示例脚本(/path/to/apache_status.sh
):
#!/bin/bash
if ! pgrep -x httpd &>/dev/null; then
echo "$(date): Apache is not running. Starting..." >> /var/log/apache_monitor.log
systemctl start httpd
fi
添加Cron任务:
crontab -e
,添加以下内容:*/5 * * * * /bin/bash /path/to/apache_status.sh
作用:实现基础的进程守护,避免Apache意外停止。
Cacti是基于PHP的网络图表工具,可图形化展示Apache的性能指标(如请求数、带宽使用、工作线程数),适合需要历史数据对比的场景。
安装与配置:
sudo yum install cacti cacti-plugins -y
。http://your_server_ip/cacti
),添加Apache监控设备,选择对应的模板(如“Apache Status”)。dstat是命令行工具,可实时显示CPU、内存、磁盘、网络及Apache的请求处理速率等综合指标,适合快速查看系统整体性能。
安装与使用:
sudo yum install dstat -y
。dstat -ta 1
(每秒刷新一次,包含Apache的“requests”指标)。