Blog

Rotate Apache (or any) logs on linux

Logs can quickly grow to take up disk space. To alleviate this problem, install logrotate if your distribution does not come with it already. Edit /etc/logrotate.conf changing the first line to the period when you want to rotate the the logs (daily, weekly, monthly, yearly). Change the rotate command under that to how long you want to keep old logs for, e.g. if rotating logs every month and to keep the logs for a year use rotate 12.

Edit (or create if necessary) /etc/logrotate.d/httpd to contain the following:

/usr/local/apache/logs/*_log {
 monthly
 rotate 12
 compress
 missingok
 notifempty
 sharedscripts
 postrotate
 /usr/local/apache/bin/apachectl graceful
 endscript
}
  • monthly: time period elapsed before the logs are rotated
  • rotate 12: logs are rotated 12 times before being removed (i.e. 1 year), if set to 0 old logs are removed rather then rotated
  • compress: old logs are compressed with gzip to save disk space
  • missingok: if logs are missing skip and do not issue an error
  • notifempty: do not rotate empty logs
  • sharedscripts: only run postrotate command once for all the logs in the specified directory, rather for each log
  • postrotate: command to run after logs are rotated, necessary for apache to start logging again

Be sure the paths /usr/local/apache/logs/*_log and /usr/local/apache/bin/apachectl graceful are correct for your configuration. Graceful will restart apache without ending any current requests being served.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>