3 Comments

Rotate Logs for Apache on Linux using logrotate

Using Apache’s access_log and error_log files are useful. Not only a system administrator should use it, but also a developer should grow habit of using these log files for debugging purpose. But sometimes for high traffic web site/applications these files can grow HUGE and real fast.

In real life, you probably need log file up to a certain point. If anything goes wrong and you know about it immediately you will probably need the current log file plus few from past to compare. In case if you find out about an issue with the site/application a week later – then you should really think about how to fix that problem.

Anyhow, this article is to show you how you can tune pre-configured logrotate on linux system for apache (HTTPD) to meet your needs. Every path I’m going to refer here are based on CentOS 5.6 OS.

Usually the logrotate configuration file is located under /etc/logrotate.conf. But logroate also has individual configuration for different linux processed. One of them is Apache. By default the configuration looks like this:

/var/log/httpd/*log {
missingok
 notifempty
 sharedscripts
 postrotate
 /sbin/service httpd reload > /dev/null 2>/dev/null || true
 endscript
}

To understand what each of the items mean please refer to http://tinyurl.com/ansqol

For my purpose I just added two lines to have this:

/var/log/httpd/*log {
size=1024M
rotate 5
missingok
notifempty
sharedscripts
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}

size=1024M – The logs should be rotated when it is 1G in size.

rotate 5 – The logs are rotated 5 times before it is removed.

Thanks!