温馨提示×

怎样用命令查centos apache日志

小樊
42
2026-09-30 06:53:18
栏目: 智能运维

在 CentOS 上,Apache(通常是 httpd)的日志默认位置在:

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

下面按常见需求给你一些实用命令示例。


一、查看 Apache 日志文件

1. 查看错误日志

cat /var/log/httpd/error_log

或实时查看:

tail -f /var/log/httpd/error_log

2. 查看访问日志

cat /var/log/httpd/access_log

实时查看:

tail -f /var/log/httpd/access_log

二、常用排查命令

1. 查看最近 100 行错误日志

tail -n 100 /var/log/httpd/error_log

2. 搜索错误关键字(如 error、404、500)

grep "error" /var/log/httpd/error_log
grep "500" /var/log/httpd/access_log

3. 查看某 IP 的访问记录

grep "192.168.1.100" /var/log/httpd/access_log

4. 统计访问最多的 IP(TOP 10)

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

5. 查看 404 请求

grep " 404 " /var/log/httpd/access_log

6. 查看某时间段的日志

sed -n '/10\/Apr\/2025:10:00:00/,/10\/Apr\/2025:11:00:00/p' /var/log/httpd/access_log

三、如果你用的是 CentOS 7+(systemd)

查看 Apache 服务日志(journalctl)

journalctl -u httpd

实时查看:

journalctl -u httpd -f

四、确认 Apache 是否运行

systemctl status httpd

五、日志路径不确定时

grep ErrorLog /etc/httpd/conf/httpd.conf
grep CustomLog /etc/httpd/conf/httpd.conf

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

  • CentOS 版本(6 / 7 / 8 / Stream)
  • Apache 是否用过虚拟主机
  • 想查的具体问题(如 500 错误、访问慢、被攻击)

我可以给你更精确的命令。

0 踩