How do you rotate Apache logs on Linux (centos)?

Solution 1:

Put the below lines into /etc/logrotate.d/httpd:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    daily
    rotate 7
    postrotate
        /sbin/service httpd reload > /var/log/httpd/rotate 2>&1 || true
    endscript
    compress
}

Solution 2:

On a Linux system you would usually setup logrotate.

You could also use something like cronolog.

Solution 3:

It's perhaps a bit more elegant to use the Apache 'rotatelogs' program, imo.

Here's an example from one of my servers:

# Seo logs, rotated daily, on GMT clock
LogFormat "%h %t %D \"%r\" %>s \"%{Referer}i\" \"%{User-Agent}i\" %{Host}i" seo
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/seo_log.%Y%m%d 86400" seo

You can also set a GMT offset for localtime, or specify rotation based on filesize.

'man rotatelogs'.