在 CentOS 上清理 Apache 日志,常见做法有直接删除/清空日志和使用 logrotate 管理两种方式。下面按安全和推荐程度说明。
通常日志在:
/var/log/httpd/
常见文件:
access_logerror_logssl_access_logssl_error_log查看:
ls -lh /var/log/httpd/
# 清空 access_log
> /var/log/httpd/access_log
# 清空 error_log
> /var/log/httpd/error_log
或一次性清空:
> /var/log/httpd/access_log
> /var/log/httpd/error_log
✅ 优点:Apache 正在写入的文件不会被破坏
CentOS 默认已配置 Apache 的 logrotate:
配置文件:
/etc/logrotate.d/httpd
手动执行轮转:
logrotate -f /etc/logrotate.d/httpd
查看配置示例:
cat /etc/logrotate.d/httpd
典型内容:
/var/log/httpd/*log {
weekly
rotate 4
compress
missingok
notifempty
sharedscripts
postrotate
/bin/systemctl reload httpd > /dev/null 2>/dev/null || true
endscript
}
✅ 自动清理、压缩、保留历史日志
rm -f /var/log/httpd/access_log
⚠ 风险:Apache 可能仍持有文件句柄,删除后磁盘不释放,需重启 Apache:
systemctl restart httpd
df -h
du -sh /var/log/httpd/
✅ 日常:用 logrotate
✅ 临时清理:用 > 清空
❌ 避免:直接 rm 正在写入的日志
如果你是想定时自动清理或只保留最近 N 天日志,我也可以帮你写脚本或优化 logrotate 配置。