在 CentOS 上清理 Apache 日志,常见做法有手动清理、日志轮转(推荐)和自动定时清理。下面分别说明。
/var/log/httpd/access_log
/var/log/httpd/error_log
(如果你用的是 yum 安装的 Apache,一般就在这个目录)
sudo truncate -s 0 /var/log/httpd/access_log
sudo truncate -s 0 /var/log/httpd/error_log
或
sudo > /var/log/httpd/access_log
sudo > /var/log/httpd/error_log
⚠️ 不建议直接 rm,否则 Apache 可能仍占用旧文件,导致磁盘不释放。
sudo rm -f /var/log/httpd/access_log-*
sudo rm -f /var/log/httpd/error_log-*
CentOS 默认使用 logrotate 管理 Apache 日志。
cat /etc/logrotate.d/httpd
典型内容:
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd > /dev/null 2>&1 || true
endscript
}
sudo logrotate -f /etc/logrotate.d/httpd
sudo logrotate -d /etc/logrotate.d/httpd
编辑:
sudo vi /etc/logrotate.d/httpd
示例(保留 30 天):
/var/log/httpd/*log {
daily
missingok
notifempty
rotate 30
compress
delaycompress
sharedscripts
postrotate
/bin/systemctl reload httpd > /dev/null 2>&1 || true
endscript
}
说明:
daily:每天轮转rotate 30:保留 30 份compress:压缩旧日志df -h
du -sh /var/log/httpd
sudo systemctl reload httpd
rm 正在写入的日志journalctl,也可查看:sudo journalctl -u httpd
如果你愿意,我可以帮你写一份适合你服务器的 logrotate 配置或定时清理脚本。