Aaron Kavlie

Apache logs and logrotate.d

If you maintain a moderately busy site, Apache log files can get out of hand pretty quickly. I moved a site I maintained to a new virtual server on Linode, and after just a couple of weeks access.log had grown to nearly 2GB.

Ubuntu (your distro may vary) has a very useful utility installed by default called logrotate.d. It's configured to rotate log files periodically for a lot of common programs, Apache included. However, by default it only covers the Apache logs in /var/log. On the other hand, the Linode Library suggests configuring Apache virtual hosts as follows:

<VirtualHost 12.34.56.78:80>
     ServerAdmin squire@ducklington.org
     ServerName ducklington.org
     ServerAlias www.ducklington.org
     DocumentRoot /srv/www/ducklington.org/public_html/
     ErrorLog /srv/www/ducklington.org/logs/error.log
     CustomLog /srv/www/ducklington.org/logs/access.log combined
</VirtualHost>

With this configuration, the logs for ducklington.org would not be covered by logrotate.d. And you might find your logs eating up all your disk space rather quickly.

Fortunately, there's a pretty easy fix for this. Just edit the first part of /etc/logrotate.d/apache2 as follows:

/var/log/apache2/*.log
/srv/www/*/logs/*.log {

That's all it takes to apply the default weekly log rotation to all sites that live under /srv/www — provided that they have log paths configured as in the example above, of course.

Finally, to ensure that this config is working, and force all logs to rotate immediately, just use the following command:

$ logrotate -vf /etc/logrotate.conf
comments powered by Disqus