在 CentOS 上轮转(rotate)Apache 日志,常用有 两种方式:
logrotate(推荐)CentOS 默认使用 logrotate 管理日志轮转,Apache 一般已经自带配置。
ls /etc/logrotate.d/
cat /etc/logrotate.d/httpd
典型内容示例:
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd > /dev/null 2>/dev/null || true
endscript
}
logrotate -d /etc/logrotate.d/httpd # 调试(不真正执行)
logrotate -f /etc/logrotate.d/httpd # 强制执行
daily / weekly / monthly:轮转周期rotate N:保留 N 个旧日志compress:压缩旧日志size 100M:到达大小就轮转postrotate ... endscript:轮转后重载 Apache示例(按大小 + 每天):
/var/log/httpd/*log {
daily
size 100M
rotate 7
compress
missingok
notifempty
sharedscripts
postrotate
systemctl reload httpd
endscript
}
rotatelogs(不依赖 logrotate)适合在 Apache 配置中直接控制。
编辑:
vi /etc/httpd/conf/httpd.conf
示例(按天轮转):
ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/error_log.%Y-%m-%d 86400"
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log.%Y-%m-%d 86400" combined
86400 = 一天(秒)5Msystemctl restart httpd
httpd/var/log/httpd/✅ 生产环境推荐:logrotate + reload httpd
✅ 特殊需求(如精确时间):rotatelogs
如果你愿意,我可以: