在 Debian 上,Apache 日志轮转通常不是由 Apache 自己完成,而是由 logrotate 工具统一管理。下面给你完整说明和常见做法。
Debian 下 Apache(通常是 apache2)的日志轮转由:
/etc/logrotate.d/apache2
这个配置文件控制。
默认行为(精简说明):
cat /etc/logrotate.d/apache2
常见内容示例(简化):
/var/log/apache2/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
systemctl reload apache2 > /dev/null 2>&1 || true
endscript
}
| 参数 | 含义 |
|---|---|
| daily | 每天轮转 |
| rotate 14 | 保留 14 个旧日志 |
| compress | 压缩旧日志 |
| delaycompress | 延迟一次压缩 |
| notifempty | 空日志不轮转 |
| postrotate | 轮转后执行命令(重载 Apache) |
logrotate -d /etc/logrotate.d/apache2
-d:debug 模式,只模拟logrotate -f /etc/logrotate.d/apache2
⚠️ 注意:
postrotate(reload apache)systemctl status logrotate.timer
或(老版本):
ls /etc/cron.daily/logrotate
确认:
systemctl reload apache2
能正常执行
如果你改了 ErrorLog / CustomLog 路径:
CustomLog /data/logs/access.log combined
那 logrotate 也要加对应路径。
假设你的日志在 /srv/www/logs/:
/srv/www/logs/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 644 www-data www-data
sharedscripts
postrotate
systemctl reload apache2
endscript
}
保存为:
/etc/logrotate.d/apache-custom
可以,但不推荐在 Debian 上用:
CustomLog "|/usr/bin/rotatelogs /var/log/apache2/access.%Y%m%d.log 86400" combined
缺点:
✅ Debian + Apache 最佳实践
logrotatesystemctl reload apache2 让日志文件句柄释放如果你愿意,可以告诉我:
我可以帮你写一份完全适配你环境的 logrotate 配置。