Apache: Configure Logrotate

Installing Apache on Ubuntu automatically configures the logrotate utility to rotate the webserver’s logs on a weekly basis. This behaviour can be customized in the /etc/logrotate.d/apache2 file.

The following configuration will rotate the log files on a monthly basis:

/var/log/apache2/*.log {
    monthly
    missingok
    rotate 12
    compress
    notifempty
    create 640 root adm
    sharedscripts
    dateext
    dateformat -%Y-%m
    dateyesterday
    postrotate
        if /etc/init.d/apache2 status > /dev/null ; then \
            /etc/init.d/apache2 reload > /dev/null; \
        fi;
    endscript
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi;
    endscript
}

Leave a Reply

Your email address will not be published. Required fields are marked *