温馨提示×

centos如何监控apache日志

小樊
61
2026-09-21 10:06:48
栏目: 智能运维

在 CentOS 上监控 Apache 日志,常见方式从简单实时查看到专业监控告警都有。下面按使用场景给你一个实用清单。


一、最基础:实时查看日志

1. 查看访问日志(实时)

tail -f /var/log/httpd/access_log

2. 查看错误日志(实时)

tail -f /var/log/httpd/error_log

CentOS 默认 Apache 日志路径:

  • 访问日志:/var/log/httpd/access_log
  • 错误日志:/var/log/httpd/error_log

二、按关键字监控(排错常用)

监控 500 错误

tail -f /var/log/httpd/error_log | grep 500

监控某个 IP

tail -f /var/log/httpd/access_log | grep 1.2.3.4

监控 PHP 错误

tail -f /var/log/httpd/error_log | grep -i php

三、统计类监控(了解流量和异常)

查看访问最多的 IP(前 10)

awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr | head

查看最多请求的 URL

awk '{print $7}' /var/log/httpd/access_log | sort | uniq -c | sort -nr | head

统计 HTTP 状态码

awk '{print $9}' /var/log/httpd/access_log | sort | uniq -c | sort -nr

四、使用系统工具(推荐)

1. journalctl(如果 Apache 使用 systemd)

journalctl -u httpd -f

查看错误:

journalctl -u httpd -p err -f

五、日志轮转监控(防日志爆盘)

CentOS 默认使用 logrotate:

cat /etc/logrotate.d/httpd

手动测试:

logrotate -d /etc/logrotate.d/httpd

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

方案 1:GoAccess(实时可视化)

yum install goaccess -y
goaccess /var/log/httpd/access_log -o report.html --log-format=COMBINED

实时终端模式:

goaccess /var/log/httpd/access_log

方案 2:ELK / Grafana + Loki

适合多台服务器、长期存储、告警:

  • Filebeat 收集 Apache 日志
  • Elasticsearch 存储
  • Kibana 可视化 或
  • Promtail + Loki + Grafana

方案 3:Zabbix / Prometheus 告警

  • 监控:
    • 错误日志关键字
    • 5xx 比例
    • 请求量突增
  • 触发邮件 / 钉钉 / 微信告警

七、常见监控指标建议

✅ 错误日志中出现 AH 错误
✅ 5xx 状态码比例 > 1%
✅ 单 IP 请求频率异常
✅ Apache 进程数过高
✅ 磁盘日志增长过快


如果你愿意,可以告诉我:

  • 是 个人服务器 还是 生产环境
  • 是否要 告警
  • 是否使用 Docker / 多台服务器

我可以直接给你一套可复制的监控方案。

0 踩