Tuesday, January 15, 2013

Config logrotate for apache log-files in "www"-directory or in other non-standard

If you have configured Apache to split error and access logs for each virtual server or configured website. Then logrotate wont rotate those log-files in their custom locations with standard configs. Logrotate comes normally installed with your distribution and is configured to rotate the apache log files that is located in /var/logs/apache2.

But if you have several websites running on your VPS or server, it's linkley you have a seperate log file for each website that you have configured and the logging wont be in the standard location.

As an example. I've several websites running under a structure of "/var/www/$NAMEOFTHEWEBSITE". Now each website has separate logging, in a structure like this  "/var/www/$NAMEOFTHEWEBSITE/logs". In each logs-directory I've a access.log, error.log and a php-error.log. If I want to rotate these with logrotate I need to configure logrotate for that.

This is done by creating another config file for my custom directories.

vim /etc/logrotate.d/www

Then with this configuration:
/var/www/*/logs/*.log { 
weekly
missingok
rotate 26
compress
delaycompress
dateext
notifempty
create 640 root adm
sharedscripts
postrotate
if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ];

then /etc/init.d/apache2 reload > /dev/null fi 
endscript
}

My configuration does this:
  • For each and any .log-file in /var/www/*/logs/
  • Do weekly rotation. (weekly)
  • If  log-file is missing, ignore without error message (missingok)
  • Rotate for 26 weeks (rotate 26)
  • Compress all log-files (compress)
  • Delay compression with one time (delaycompress)
  • Name the rotated files with a date-based name so it's easier for me to know where to look (dateext)
  • Do not rotate if log-file is empty (notifempty)
  • Owner and permission settings for rotated file (create 640 root adm)
  • Normally, prescript and postscript scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscript is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. This option overrides the nosharedscripts option and implies create option. (sharedscripts)
  • The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside of a log file definition. (postrotate & endscript)

That's it. I hope it helps you out configuring your vps or server with the very useful logrotate.