Apache 日誌太大處理辦法
第一步:停止Apache服务的所有进程,删除 Apache2/logs/目录下的 error.log、access.log文件
第二步:打开 Apache 的 httpd.conf配置文件并找到下面两条配置
ErrorLog logs/error.log
CustomLog logs/access.log common
直接注释掉,换成下面的配置文件。
限制错误日志文件为 1M
ErrorLog “|bin/rotatelogs.exe -l logs/error-%Y-%m-%d.log 1M”
每天生成一个错误日志文件
#ErrorLog “|bin/rotatelogs.exe -l logs/error-%Y-%m-%d.log 86400”
限制访问日志文件为 1M
CustomLog “|bin/rotatelogs.exe -l logs/access-%Y-%m-%d.log 1M” common
每天生成一个访问日志文件
#CustomLog “|bin/rotatelogs.exe -l logs/access-%Y-%m-%d.log 86400” common
#ErrorLog logs/error_log
ErrorLog “| /usr/sbin/rotatelogs /var/log/httpd/%Y_%m_%d_error_log 86400 10M”
CustomLog “| /usr/sbin/rotatelogs /var/log/httpd/access_log 86400 10M” combined
LogLevel warn
LogFormat “%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"“ combined
LogFormat “%h %l %u %t "%r" %>s %b” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent
#CustomLog logs/access_log combined
然后建立清除日志文件的shell脚本,文件名为clean_log
#! /bin/bash
logdir=/var/log/httpd
cd ${logdir}
declare -i filesum=ls access_log.* | wc -l
declare -i delnum=$filesum-3
if [ “${delnum}” -ge 1 ];then
rm -rf ls -tr access_log.* | head -${delnum}
fi
chmod 755 clean_log
这样就保留了最近3天的日志文件。
建立自动化任务
01 04 * * * /usr/local/crontab/clean_log
ok,搞定,就这么简单。这样你就不用不必为日见增大的日志文件烦恼了!