温馨提示×

怎样用命令查apache日志

小樊
36
2026-09-04 09:59:28
栏目: 编程语言

下面按常见 Linux 场景给你总结,怎么用命令查 Apache 日志。


一、先找到 Apache 日志位置

常见路径:

/var/log/apache2/access.log      # Debian/Ubuntu
/var/log/apache2/error.log

/var/log/httpd/access_log        # CentOS/RHEL
/var/log/httpd/error_log

也可以通过配置确认:

apachectl -S
# 或
grep -R "CustomLog" /etc/apache2/

二、实时查看日志

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

三、常用排查命令

1️⃣ 查看访问最多的 IP

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

2️⃣ 查看某个 IP 的访问

grep "1.2.3.4" access.log

3️⃣ 查看 404 请求

awk '$9 == 404' access.log

grep " 404 " access.log

4️⃣ 查看 500 错误

grep " 500 " access.log

5️⃣ 查看某个 URL 的访问

grep "/index.php" access.log

6️⃣ 按时间过滤(如某天)

grep "10/Apr/2024" access.log

7️⃣ 查看错误日志中的 PHP / 异常

grep -i "error" error.log
grep -i "php" error.log

四、统计类(很实用)

各状态码统计

awk '{print $9}' access.log | sort | uniq -c | sort -nr

每小时访问量

awk '{print $4}' access.log | cut -d: -f1-2 | sort | uniq -c

五、配合 journalctl(systemd)

journalctl -u apache2 -f
journalctl -u httpd -f

如果你能告诉我:

  • 系统(CentOS / Ubuntu)
  • 想查访问日志还是错误日志
  • 具体问题(慢、报错、被攻击)

我可以给你精确的命令

0