Rotating Apache httpd logfiles on FreeBSD
Thursday, 8th of October, A.D. 2009
With the disk space available on modern servers, you tend to notice some things a lot less. Like the boring fact that without log rotation, an Apache access log can grow to gigabyte size in no time.
FreeBSD’s Apache HTTPD port does not ship with configuration for the
FreeBSD log rotation utility,
newsyslog, so your logs won’t be rotated
That, however, is fairly easy to fix by tweaking /etc/ newsyslog.conf a bit.
Here’s how I did it:
/var/log/httpd-access.log www:www 440 9 * $W1D4 J /var/run/httpd.pid 30 /var/log/httpd-error.log www:www 440 9 * $W1D4 J /var/run/httpd.pid 30
Broken up, this means:
/var/log/httpd-access.log: Name of the log file we’re rotating
www:wwwset user and group ownership of the archived logs to www, so sysadmins can read them.
440set the archive files to be read-only for the www user and group and no access for anyone else.
9keep nine archived log files excluding the current one. This way, we should always have the latest 10 weeks of log data available.
*don’t rotate based on log file size.
$W1D4rotate logs every Monday at 4 in the morning.
Jcompress the archived logs with bzip2.
/var/run/httpd.pid– get the process ID for the httpd server here.
SIGUSR1to cause a graceful restart of httpd.
I set this up last week, and it has since done its work, turning my 1GiB /var/log/httpd-access.log into a 46MiB httpd-access.log.0.bz2. Log files are some of the best use cases for compression. Enjoy.
My name is Mikkel Høgh, I've worked with web tech for the last 20 years. These days, I work with e-commerce in Central Switzerland.