在 CentOS 上,Apache(httpd)日志分割常见有 三种方式,从推荐程度排序如下:
Apache 自带 rotatelogs,无需额外安装。
编辑配置文件(通常如下之一):
vim /etc/httpd/conf/httpd.conf
# 或
vim /etc/httpd/conf.d/*.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.20260121systemctl restart httpd
适合 已经存在的日志文件。
vim /etc/logrotate.d/httpd
内容示例:
/var/log/httpd/*log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
sharedscripts
postrotate
/bin/systemctl reload httpd > /dev/null 2>&1 || true
endscript
}
logrotate -d /etc/logrotate.d/httpd
logrotate -f /etc/logrotate.d/httpd
需额外安装:
yum install cronolog -y
配置:
CustomLog "|/usr/bin/cronolog /var/log/httpd/access_%Y%m%d.log" combined
| 方式 | 推荐度 | 说明 |
|---|---|---|
| rotatelogs | ⭐⭐⭐⭐⭐ | Apache 原生,实时分割 |
| logrotate | ⭐⭐⭐⭐ | 系统统一管理 |
| cronolog | ⭐⭐ | 已逐渐淘汰 |
如果你愿意,我可以:
告诉我你的 CentOS 版本和 Apache 版本即可。