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!

3 thoughts on “Rotate Logs for Apache on Linux using logrotate”

  1. files should be together in the directory indicated by the Site address (URL) setting on your General Options page. Since the name of the file begins with a dot, the file may not be visible through an FTP client unless you change the preferences of the FTP tool to show all files, including the hidden files. Some hosts (e.g. Godaddy) may not show or allow you to edit .htaccess if you install WordPress through the Godaddy Hosting Connection installation.

  2. When using mysqldump on a replication master, if you want the slave(s) to follow, you may want to avoid the –delete-master-logs option, because it can delete binary logs before the “CHANGE MASTER” is read by the slaves, therefore breaking the replication (then you have to issue manually the “CHANGE MASTER” on the slave(s)). If you want to get rid of old and useless binary logs, it is better to issue a “PURGE MASTER” SQL command on the master after the mysqldump.

  3. You might want to add the following lines in there:

    daily
    compress

    Also, I would remove the size parameter as it will allow for easier analysis on the log files.

Leave a Reply