Apache2 Logrotate

Whenever I setup a new web server, I always like the idea of keeping the Apache logs roated on a daily basis. While the procedures for changing the log rotate frequency is pretty straight forward, I have seen a lot of people having problems with it so I decided to document it.

To change the Apache log rotate frequency, you would want to change the apache2 configuration file located under /etc/logrotate.d. Configurations located in this directory overrides the default settings in /etc/logrotate.conf (the configurations in /etc/logrotate.d are included in logrotate.conf)

Here’s what my settings look like (note that I had commented out compress option so that all the logs can be easily browsed):

/var/log/apache2/*.log {
rotate 365
create 640 root adm
if [ -f “`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`” ]; then
/etc/init.d/apache2 reload > /dev/null

Common problem

A common problem after changing the log rotate frequency is that the Apache logs do not appear to be rotating daily even though the configuration file had been changed correctly.

This seems to be a known issue with logrotate. If you read the manual for logrotate you’d see the following explanation:

-f, –force
Tells logrotate to force the rotation, even if it doesn’t think
this is necessary. Sometimes this is useful after adding new
entries to a logrotate config file, or if old log files have
been removed by hand, as the new files will be created, and
logging will continue correctly.

Since the default cron daily job does not force the log rotation, you may want to add -f parameter to /etc/cron.daily/logrotate:


test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate -f /etc/logrotate.conf

With this change, the logs will be forced to rotate daily according to the setup.

Be Sociable, Share!

Leave a Reply