Apache 日志写入失败通常与权限、磁盘、配置或资源限制有关。下面按常见原因逐一排查,并给出对应的解决方法。
Apache 一般以 www-data、apache、nobody 等用户运行。
ps aux | grep apache
# 或
ps aux | grep httpd
检查日志目录权限:
ls -ld /var/log/apache2
ls -ld /var/log/httpd
✅ 解决:
chown -R apache:apache /var/log/apache2
chmod 755 /var/log/apache2
日志文件本身也要确保可写。
df -h
df -i
✅ 解决:
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
检查:
ls /var/log/apache2
✅ 解决:
mkdir -p /var/log/apache2
ErrorLog ${APACHE_LOG_DIR}/error.log
如果该变量未定义,会直接失败。
✅ 检查:
apachectl -S
getenforce
查看是否被拒绝:
ausearch -m avc -ts recent
✅ 解决:
setenforce 0 # 临时关闭(测试用)
或:
semanage fcontext -a -t httpd_log_t "/data/logs(/.*)?"
restorecon -Rv /data/logs
aa-status
✅ 解决:
编辑 /etc/apparmor.d/usr.sbin.apache2,加入日志路径。
日志文件被 rm 删除,但 Apache 仍占用文件句柄。
✅ 排查:
lsof | grep deleted
✅ 解决:
systemctl reload apache2
如 logrotate 配置错误,导致新日志无法生成。
✅ 检查:
cat /etc/logrotate.d/apache2
✅ 手动测试:
logrotate -vf /etc/logrotate.d/apache2
配置错误导致 Apache 根本没启动。
✅ 检查:
apachectl configtest
# 或
httpd -t
✅ 查看系统日志:
journalctl -xe
mount | grep " /var/log"
如出现 ro,说明只读。
✅ 解决:
mount -o remount,rw /var/log
apachectl configtestdf -hls -ld /var/log/apache2ps aux | grep apachegetenforcelsof | grep deleted你可以直接贴出:
error.log 或 journalctl)