温馨提示×

如何监控ubuntu apache2

小樊
38
2026-09-11 04:49:43
栏目: 智能运维

在 Ubuntu 上监控 Apache2 可以从系统资源、Apache 运行状态、访问日志、性能指标、告警几个层面来做。下面按“从简单到专业”的方式介绍。


一、基础监控(命令行)

1. 查看 Apache 是否在运行

systemctl status apache2

开机自启检查:

systemctl is-enabled apache2

2. 查看 Apache 进程

ps aux | grep apache2

或:

top -p $(pgrep apache2 | tr '\n' ',')

3. 查看端口监听

ss -lntp | grep :80
ss -lntp | grep :443

4. 实时连接数

netstat -an | grep :80 | wc -l

或(推荐):

ss -ant | grep :80 | wc -l

二、Apache 自带状态监控(最推荐)

1. 启用 status 模块

sudo a2enmod status
sudo systemctl restart apache2

2. 配置 status 页面

编辑:

sudo nano /etc/apache2/mods-enabled/status.conf

示例(允许内网访问):

<Location /server-status>
    SetHandler server-status
    Require ip 192.168.1.0/24
    # Require local
</Location>

重启:

sudo systemctl restart apache2

3. 访问状态页

http://服务器IP/server-status

开启扩展状态:

http://服务器IP/server-status?auto

可以看到:

  • 当前请求数
  • 空闲 / 忙碌 worker
  • 每个进程在干什么

三、日志监控

1. 实时访问日志

tail -f /var/log/apache2/access.log

2. 错误日志

tail -f /var/log/apache2/error.log

3. 统计访问 IP

awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head

四、系统资源监控

1. CPU / 内存

htop

安装:

sudo apt install htop

2. 磁盘 IO

iotop

3. 网络流量

iftop

五、专业监控方案(生产环境推荐)

方案 1:Prometheus + Node Exporter + Apache Exporter

适合服务器集群

  • node_exporter:系统监控
  • apache_exporter:Apache 指标
  • Prometheus + Grafana 可视化

Apache Exporter:

https://github.com/Lusitaniae/apache_exporter

方案 2:Zabbix

  • 自带 Apache 模板
  • 支持告警(微信 / 邮件 / 钉钉)

方案 3:Netdata(最简单)

bash <(curl -Ss https://my-netdata.io/kickstart.sh)

打开:

http://服务器IP:19999

六、常见监控指标(重点)

✅ 必须关注:

  • Apache 进程数
  • 请求失败率(5xx)
  • 响应时间
  • CPU / 内存占用
  • 磁盘空间

✅ 告警建议:

  • 5xx 错误突然增加
  • 连接数接近 MaxRequestWorkers
  • 内存使用 > 80%

七、简单健康检查脚本(示例)

#!/bin/bash
if ! systemctl is-active --quiet apache2; then
    echo "Apache is down!" | mail -s "Apache Alert" admin@example.com
    systemctl restart apache2
fi

如果你愿意,我可以:

  • 帮你 配置 Grafana 监控面板
  • 给你 Zabbix Apache 模板
  • 针对 高并发场景给优化建议

你现在是个人服务器、公司生产环境,还是学习用

0