监控 Debian 上 Apache 日志通常从 实时查看、过滤分析、自动告警、日志轮转 四个方面入手。下面是常用且实用的方法。
Debian 下 Apache 日志一般在:
/var/log/apache2/
常见文件:
| 日志 | 作用 |
|---|---|
access.log |
访问日志 |
error.log |
错误日志 |
other_vhosts_access.log |
虚拟主机访问日志 |
ssl_access.log |
HTTPS 访问日志 |
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
tail -f /var/log/apache2/access.log /var/log/apache2/error.log
grep " 404 " /var/log/apache2/access.log
grep "192.168.1.100" /var/log/apache2/access.log
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head
awk '{print $7}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head
journalctl(systemd)如果 Apache 使用 systemd:
journalctl -u apache2 -f
查看最近错误:
journalctl -u apache2 --since "10 min ago"
logwatch(简单好用)sudo apt install logwatch
生成报告:
logwatch --service apache2 --output stdout
每日自动邮件(默认 cron):
/etc/cron.daily/00logwatch
goaccess(可视化分析)✅ 强烈推荐sudo apt install goaccess
goaccess /var/log/apache2/access.log -o /var/www/html/report.html --log-format=COMBINED
浏览器访问:
http://服务器IP/report.html
goaccess /var/log/apache2/access.log -c
awk '($9 == 404)' /var/log/apache2/access.log | wc -l
awk '{print $1}' /var/log/apache2/access.log | uniq -c | awk '$1 > 500'
Debian 默认已配置:
/etc/logrotate.d/apache2
手动测试:
logrotate -f /etc/logrotate.d/apache2
| 场景 | 推荐 |
|---|---|
| 实时排查 | tail -f error.log |
| 日常分析 | goaccess |
| 自动日报 | logwatch |
| 安全监控 | fail2ban + apache 日志 |
| 集中管理 | ELK / Grafana Loki |
如果你愿意,我可以帮你:
只要告诉我你的使用场景即可。