在 CentOS 上“Apache 日志缺失”是一个常见但原因多样的问题。下面按最常见 → 较少见的顺序说明原因和排查方法。
Apache 默认日志位置:
/var/log/httpd/access_log/var/log/httpd/error_log检查配置:
grep -R "CustomLog\|ErrorLog" /etc/httpd/
如果看到:
#CustomLog logs/access_log combined
说明被注释掉了。
✅ 解决:
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
很多人以为启动了,其实没起:
systemctl status httpd
若未运行:
systemctl start httpd
Apache 用户(通常是 apache)无法写日志:
ls -ld /var/log/httpd
修复:
mkdir -p /var/log/httpd
chown apache:apache /var/log/httpd
chmod 700 /var/log/httpd
SELinux 是 CentOS 上“日志莫名消失”的元凶之一。
检查:
getenforce
如果为 Enforcing,看审计日志:
grep httpd /var/log/audit/audit.log | grep denied
✅ 临时解决:
setenforce 0
✅ 永久修复(推荐):
semanage fcontext -a -t httpd_log_t "/var/log/httpd(/.*)?"
restorecon -Rv /var/log/httpd
如果你用的是 vhost:
<VirtualHost *:80>
ServerName example.com
# 没写日志配置
</VirtualHost>
默认主配置可能不生效。
✅ 解决:
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
某些优化脚本会写:
CustomLog /dev/null combined
CentOS 默认会轮转:
/etc/logrotate.d/httpd
你可能看到的是:
access_log-20260101.gz查看:
ls -lh /var/log/httpd/
df -h
df -i
/usr/local/apache/logssystemctl status httpd
ls -l /var/log/httpd
grep -R ErrorLog /etc/httpd
getenforce
df -h
journalctl -u httpd
如果你愿意,可以贴出:
httpd -S 输出ls -l /var/log/httpd我可以帮你精准定位是哪一条原因。