在 CentOS 系统中,Apache(httpd)服务的日志文件通常会保存在 /var/log/httpd/ 目录下,分为 访问日志 和 错误日志 两类。不同用途的日志可以用不同方式查看。
- 日志文件路径
默认安装 Apache 后,日志文件位置如下(CentOS 7 / CentOS 8 常见情况):
访问日志(Access Log):
php
/var/log/httpd/access_log
错误日志(Error Log):
php
/var/log/httpd/error_log
如果是自定义虚拟主机,还可能会在配置文件中指定不同的日志文件路径,可以用以下命令查找:
php
grep -R "Log" /etc/httpd/
- 查看日志的常用命令
直接查看完整日志(可能很大):
php
cat /var/log/httpd/access_log
cat /var/log/httpd/error_log
分页查看(上下翻页):
php
less /var/log/httpd/access_log
less /var/log/httpd/error_log
实时查看最新日志(类似 tail -f,适合调试):
php
less /var/log/httpd/access_log
less /var/log/httpd/error_log
查看最新 100 行日志:
php
tail -n 100 /var/log/httpd/access_log
tail -n 100 /var/log/httpd/error_log
- 过滤特定内容
如果想查找特定 IP 或关键字,可以结合 grep 使用:
php
grep "192.168.1.100" /var/log/httpd/access_log
grep "php" /var/log/httpd/error_log
- 注意权限
在 CentOS 中,/var/log/httpd/ 目录通常需要 root 权限才能访问,所以建议加 sudo:
php
sudo tail -f /var/log/httpd/access_log
- Apache 日志格式
Access Log 常见格式:
php
IP地址 - - [日期 时间 时区] "请求方法 路径 协议" 状态码 响应大小 "Referer" "User-Agent"
Error Log 常见格式:
php
[日期 时间] [error] [客户端IP] 详细错误信息