在 CentOS 上对 Apache(httpd)日志进行分割,常见有下面几种做法,从“最推荐”到“传统方案”给你说明。
CentOS 默认已安装 logrotate,不需要额外写脚本。
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
}
.gz)logrotate -d /etc/logrotate.d/httpd
logrotate -f /etc/logrotate.d/httpd
✅ 生产环境推荐这种方式
适合 不想用 logrotate 的场景。
编辑:
vi /etc/httpd/conf/httpd.conf
修改日志配置:
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log.%Y%m%d 86400" combined
ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/error_log.%Y%m%d 86400"
说明:
86400 = 一天(秒)access_log.20260121
重启 Apache:
systemctl restart httpd
例如:每 100MB 切一次
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log 100M" combined
如果日志写不进去:
ls -Z /var/log/httpd
restorecon -Rv /var/log/httpd
一定是 未 reload Apache
postrotate
systemctl reload httpd
endscript
| 场景 | 推荐 |
|---|---|
| 服务器常规使用 | ✅ logrotate |
| 容器 / 特殊环境 | rotatelogs |
| 按大小严格限制 | rotatelogs |
如果你愿意,可以告诉我:
httpd -v)我可以直接帮你写一份 可用配置。