在 CentOS 上,Apache 日志的轮转通常是通过 logrotate 来完成的,而不是 Apache 自己处理。下面给你讲清楚原理和常见做法。
CentOS 中:
/var/log/httpd//etc/logrotate.conf(全局)/etc/logrotate.d/httpd(Apache 专用)CentOS 安装 httpd 后,会自动生成:
/etc/logrotate.d/httpd
内容大致如下(简化):
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd > /dev/null 2>/dev/null || true
endscript
}
reload httpd:让 Apache 重新打开日志文件(必须,否则还写旧文件)delaycompress:先不压缩,下一次再压/var/log/httpd/*log {
size 100M
rotate 10
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
systemctl reload httpd
endscript
}
含义:
/var/log/httpd/*log {
daily
rotate 30
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
systemctl reload httpd
endscript
}
logrotate -d /etc/logrotate.d/httpd
logrotate -f /etc/logrotate.d/httpd
有些人在 httpd.conf 里这样写:
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log 86400" combined
✅ 这种 不需要 logrotate ❌ 但管理不如 logrotate 统一
轮转后日志不写新文件
SELinux 导致写不了
restorecon -Rv /var/log/httpd
日志权限问题
root:root 或 apache✅ CentOS + Apache 最佳实践
/etc/logrotate.d/httpdsystemctl reload httpd如果你愿意,我可以: