Reading time: 2 – 2 minutes

It came to my attention today that my website was serving blank pages, and I got really intrigued with it. I checked the memory, and everything was fine, and my logs wouldn’t indicate any obvious error. I did a quick research, and some people were saying this happens due to your log files being full which cause your OS’s open file to go nuts, and you run out of file slots. 
By running out of slots, your Apache starts serving blank pages, and won’t work properly either. A quick way around to it, is cleaning up your logs.

STOP!

We are not gonna simply get rid of the log files, as they are important, and can be used when something really goes wrong, or for access monitoring. One nice way of doing that is by rotating your log files, so the “full log files” will be backed up, and new ones will be created.
This is where logrotate comes into place. By creating a simple script on etclogrotate.d (assuming you have logrotate already installed), you can rotate your logs automatically. This is how I created mine:

    /var/www/mywebsite/logs/*log {
    rotate 5
    weekly
    missingok
    compress
    olddir /var/www/mywebsite/logs/old
    notifempty
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript

Refer to the documentation to see what everything does, but basically I tell my system to backup my log files to a backup folder, and create a new one. The files will only be deleted after the 5th rotation, and the rotations will happen in a weekly basis.
Normally it should be done automatically for apache, but as I use a custom log file for each of my website, i need to create this new script for each of my websites.

2 Responses to “Apache 101 – Are you rotating your logs?”

  1. admin says:

    Humm, that sounds interesting, I’ll definitely take a look at it, and if I realy find it’s better, I’ll make a bog post about it.

  2. Mo Barger says:

    I find cronolog to be much less intrusive. It runs in process with apache and no restarts are necessary, no extra daemons. It is granular enough to rotate hourly, daily, weekly or monthly.
    http://cronolog.org/
    You can pipe all vhosts to same log, or define different logs per vhost using cronolog.

Leave a Reply

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