How do I delete or rotate Apache log files?

Question:

How do I delete or rotate apache log files?

Answer:

We currently do not provide a command to remove the log files, but a program provided by Apache called "rotatelogs" can be used to rotate logs based on a specific time interval. Information on "rotatelogs" can be found at the following URL:

http://www.apache.org/docs-2.0/programs/rotatelogs.html

Software:

Detail:

Log rotation is the preferred method of obtaining statistics on a Web site as it does not require the accessing of the log files while the Apache server is attempting to write to them. It also allows compression of the old logs for archiving purposes, thus preventing incredibly large log files from being generated. If log files are rotated and archived, they still require administration to prevent the account from using an excessive amount of disk space.

Solution:

Although a command is not provided by default, a simple shell script can be written to remove the Apache log files without causing any possible access conflicts. The script, with which we cannot provide assistance, can be created by issuing the pico -w scriptname.sh command and pasting in the following:

-------------

#!/bin/sh

#First stop apache to prevent any possible conflicts while

#removing the file.

apachectl stop

#Next remove the files

cp /dev/null /usr/local/www/vhosts/vhostdomain.com/logs/access_log

cp /dev/null /usr/local/www/vhosts/vhostdomain.com/logs/error_log

#Although we did not use the remove command 'rm', the above copy command

#will overwrite the log files ensuring that they still exist but are of

#zero length. Now we need to restart apache to begin serving pages (both

#nonsecure and secure)

apachectl startssl

-----------

You can now save the file and make it executable using the following command:

> chmod 775 scriptname.sh

You can now run this script itself or make it a cron job to empty the logs at a time of your choosing. If you wish to empty out the logs for multiple vhosts at the same time, DO NOT use multiple instances of this script running at the same time. Instead, make duplicate entries of the copy commands within this single file by replacing the vhostdomain.com with the actual vhost name.

As an alternative to simply deleting the logs or using 'rotatelogs', this same script can be modified to move and compress the log files. This would result in decreased usage by the previous day's, week's, or month's log file while still allowing statistics to be obtained. The statistics program Analog, which can be installed through the package manager vinstall, can be configured to obtain statistics from a compressed file. The command which needs to be included in the vhost configuration file located in the vhost's log directory is

UNCOMPRESS *.gz,*.Z  /usr/bin/gzcat

as well as modifying the LOGFILE command to point to the appropriate compressed files:

access_log.gz

error_log.gz

The modified shell script is similar to the one above but with some slight modifications:

-------------

#!/bin/sh

#First stop apache to prevent any possible conflicts while

#removing the file.

apachectl stop

#Next compress the files using gzip

gzip /usr/local/www/vhosts/vhostdomain.com/logs/access_log

gzip /usr/local/www/vhosts/vhostdomain.com/logs/error_log

touch /usr/local/www/vhosts/vhostdomain.com/logs/access_log

touch /usr/local/www/vhosts/vhostdomain.com/logs/error_log

#Compressing the log files into access_log.gz and error_log.gz removes

#the original files; therefore as a precaution, the 'touch' command is

#used to create the logfiles with zero length.

#Now we need to restart apache to begin serving pages (both nonsecure

#and secure)

apachectl startssl

-----------

As before, this script can be run via cron to move your files on a daily, weekly, or monthly basis depending on how you configure the cron job. The same precaution should also be followed with this script: DO NOT run multiple instances of the script simultaneously.  

This is a very basic script which compresses and archives only the previous set of logs and is meant to provide a starting point for you to develop your own script to archive log files based on the date. There are such scripts available and can be found by using the web search engines. A good place to start is by looking at the FAQ from the Apache Web site:

http://www.apache.org/docs-2.0/misc/FAQ-I.html.