在 Debian 上压缩 Apache 日志文件,常见做法有 手动压缩、logrotate 自动压缩 以及 Apache 自带压缩模块 几种方式。下面按使用场景说明。
Debian 默认使用 logrotate 管理 Apache 日志。
cat /etc/logrotate.d/apache2
典型内容示例:
/var/log/apache2/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
systemctl reload apache2 > /dev/null 2>&1 || true
endscript
}
compress:启用压缩(默认 gzip)delaycompress:延迟一轮再压缩(避免正在写入的日志)compresscmd:可指定压缩命令(如 zstd)logrotate -f /etc/logrotate.d/apache2
cd /var/log/apache2
gzip access.log.1 error.log.1
gzip /var/log/apache2/*.log.*
⚠️ 不要直接压缩 access.log、error.log(Apache 正在写入)
apt install zstd
compresscmd /usr/bin/zstd
uncompresscmd /usr/bin/unzstd
compressoptions -19
rotatelogs(管道方式)CustomLog "|/usr/bin/rotatelogs -l /var/log/apache2/access.log.%Y%m%d.gz 86400 gzip" combined
⚠️ 配置复杂,排错困难,一般不建议生产环境使用。
zcat access.log.1.gz
zless error.log.1.gz
find /var/log/apache2 -name "*.gz" -mtime +30 -delete
✅ 生产环境:使用 logrotate + gzip/zstd
✅ 临时清理:手动 gzip 旧日志
❌ 不建议实时压缩当前写入日志
如果你愿意,我可以帮你定制一份 Debian + Apache 的 logrotate 配置。