温馨提示×

怎样用centos查apache访问日志

小樊
41
2026-10-20 16:14:40
栏目: 智能运维

在 CentOS 上查看 Apache 访问日志,通常按下面几个步骤来即可。


一、确认 Apache 是否已安装并运行

systemctl status httpd

如果未运行,可启动:

systemctl start httpd

二、Apache 访问日志的默认位置

在 CentOS(使用 httpd)中,默认访问日志一般位于:

/var/log/httpd/access_log

错误日志在:

/var/log/httpd/error_log

如果你用的是 源码安装或自定义配置,路径可能不同,可查看配置文件确认。


三、查看 Apache 配置文件中的日志路径(可选)

grep -R "CustomLog" /etc/httpd/conf/httpd.conf

或查看虚拟主机配置:

grep -R "CustomLog" /etc/httpd/conf.d/

四、常用查看日志命令

1️⃣ 实时查看访问日志

tail -f /var/log/httpd/access_log

按 Ctrl + C 退出


2️⃣ 查看最后 100 行

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

3️⃣ 查看最前面的内容

head -n 50 /var/log/httpd/access_log

4️⃣ 搜索某个 IP 的访问记录

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

5️⃣ 统计访问最多的 IP(Top 10)

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

6️⃣ 查看某个时间段的日志

grep "10/Apr/2025" /var/log/httpd/access_log

五、日志格式说明(常见)

一条典型访问日志:

192.168.1.1 - - [10/Apr/2025:12:00:01 +0800] "GET /index.html HTTP/1.1" 200 2326

含义:

  • 192.168.1.1:客户端 IP
  • -:远程用户
  • 10/Apr/2025:12:00:01:时间
  • GET /index.html HTTP/1.1:请求
  • 200:状态码
  • 2326:返回字节数

六、日志轮转(默认开启)

CentOS 使用 logrotate 自动切割日志:

ls /var/log/httpd/

你可能会看到:

access_log
access_log-20250410

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

  • 你是 查异常、查攻击、还是统计访问量
  • 是否使用了 Nginx + Apache / 虚拟主机 / Docker

我可以给你更针对性的命令。

0 踩