温馨提示×

怎么用命令看apache日志

小樊
39
2026-10-06 02:14:28
栏目: 编程语言

查看 Apache 日志通常使用 Linux 下的基础命令即可。下面按常见场景给你说明。


一、先找到 Apache 日志位置

1. 常见默认路径

  • CentOS / RHEL
    /var/log/httpd/access_log
    /var/log/httpd/error_log
    
  • Ubuntu / Debian
    /var/log/apache2/access.log
    /var/log/apache2/error.log
    

2. 查看配置文件确认

grep -R "CustomLog" /etc/httpd/        # CentOS
grep -R "CustomLog" /etc/apache2/      # Ubuntu

二、实时查看日志(最常用)

实时查看访问日志

tail -f /var/log/httpd/access_log
# 或
tail -f /var/log/apache2/access.log

实时查看错误日志

tail -f /var/log/httpd/error_log

Ctrl + C 退出


三、查看最近 N 行日志

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

四、搜索关键字(非常实用)

查看某个 IP 的访问

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

查看 404 错误

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

查看某时间段

grep "10/Apr/2024:14:" /var/log/httpd/access_log

五、统计类命令

统计访问最多的 IP

awk '{print $1}' access_log | sort | uniq -c | sort -nr | head

统计请求最多的 URL

awk '{print $7}' access_log | sort | uniq -c | sort -nr | head

统计状态码分布

awk '{print $9}' access_log | sort | uniq -c | sort -nr

六、查看 Apache 是否正在写日志

ls -l --time-style=full-iso /var/log/httpd/

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

  • 你用的是 CentOS 还是 Ubuntu
  • 想看 访问日志还是错误日志
  • 是排错还是分析流量

我可以直接给你一条最合适你的命令。

0 踩