在 CentOS 上监控 Apache 日志,常见做法有命令行实时查看、日志分析工具、以及监控告警系统三类。下面按使用频率和场景给你一个较完整的方案。
# CentOS + Apache(httpd)
/var/log/httpd/access_log
/var/log/httpd/error_log
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
# 只看 404
tail -f /var/log/httpd/access_log | grep 404
# 只看某个 IP
tail -f /var/log/httpd/access_log | grep 192.168.1.100
# 只看错误
tail -f /var/log/httpd/error_log | grep -i error
awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr | head
awk '{print $9}' /var/log/httpd/access_log | sort | uniq -c | sort -nr
awk '{print $7}' /var/log/httpd/access_log | sort | uniq -c | sort -nr | head
yum install -y goaccess
goaccess /var/log/httpd/access_log -o /var/www/html/report.html --log-format=COMBINED
实时终端模式:
goaccess /var/log/httpd/access_log --log-format=COMBINED
cat /etc/logrotate.d/httpd
手动测试:
logrotate -d /etc/logrotate.d/httpd
yum install -y epel-release
yum install -y fail2ban
示例:监控 404 暴力扫描
[apache-404]
enabled = true
filter = apache-404
logpath = /var/log/httpd/access_log
maxretry = 50
#!/bin/bash
COUNT=$(grep -c "500" /var/log/httpd/error_log)
if [ "$COUNT" -gt 10 ]; then
echo "Apache 500 错误过多" | mail -s "Apache 告警" admin@xxx.com
fi
tail -f如果你能说一下是 CentOS 几 + Apache 版本 + 使用场景(排查 / 安全 / 性能),我可以给你更精确的方案。