In Apache, we can reduce CPU load (improving performance) and avoid restarting each time logs are rotated (logrotate, which comes default in many distributions does it), if we use pipes to a process instead of a file when logging access and errors information.
We can use any program to do this, but I will talk about two of them (rotatelogs by Apache) and cronolog.
Let’s do this
The only thing we have to do is to include in our VirtualHost configuration (/etc/apache2/sites-available/mi_virtualhost), in the logging part (lines starting with ErrorLog and CustomLog), the following:
ErrorLog “|/usr/sbin/rotatelogs /home/cloud/www/totaki.com/logs/error.%Y-%m-%d-%H_%M_%S.log 86400”
CustomLog “|/usr/sbin/rotatelogs /home/cloud/www/totaki.com/logs/access.%Y-%m-%d-%H_%M_%S.log 86400” combined
in the case of rotatelogs. Here “%Y-%m-%d-%H_%M_%S” is a date format string. We can find all keywords available looking at the manual.
The number 86400 refers to the number of seconds each log will be up, in this case 1 day, so we will close each log and start a new one each day. We can also specify for example “2M”, so we will rotate the log each two Megabytes.
The path where rotatelogs is located may vary in each system, so if we have any doubt, just do this as root:
In the case we want to use cronolog, we must install this program, and then, in the Virtualhost configuration, write the following:
ErrorLog “|/usr/bin/cronolog /home/cloud/www/totaki.com/logs/%Y/%m/%d/error.log”
CustomLog “|/usr/bin/cronolog /home/cloud/www/totaki.com/logs/%Y/%m/%d/access” combined
cronolog, detects the rotation unit. It will rotate the log on the smallest unit specified in the filename. In this case %d is the smallest, and the log will rotate each day.
Cronolog has some more features like creating links to the current or previous log files.
Remove old rotation configuration
If we were using logrotate, we must make sure it doesn’t rotate Apache logs anymore (avoiding unwanted restarts), but it’s interesting to keep this program running because some other services may need it.
In my case, the main configuration file for logrotate is /etc/logrotate.conf and specific configuration files are located at /etc/logrotate.d/, being Apache one /etc/logrotate.d/apache2.
So, we can delete this file (/etc/logrotate.d/apache2), but I don’t like to delete anything, so I prefer doing this (as root):
# mkdir /etc/logrotate.d_old/
# mv /etc/logrotate.d/apache2 /etc/logrotate.d_old
Si we keep the old file too.
Avoiding starting so many shells
By default, Apache, starts a shell (/bin/bash) when calling a program to write logs (as we’ve been done). Sometimes it’s necessary, but not for logrotate nor cronolog, so we can use two pipes signs (||) instead of one (|), or configuration will be like this:
ErrorLog “||/usr/sbin/rotatelogs /home/cloud/www/totaki.com/logs/error.%Y-%m-%d-%H_%M_%S.log 86400″
CustomLog “||/usr/sbin/rotatelogs /home/cloud/www/totaki.com/logs/access.%Y-%m-%d-%H_%M_%S.log 86400″ combined
ErrorLog “||/usr/bin/cronolog /home/cloud/www/totaki.com/logs/%Y/%m/%d/error.log”
CustomLog “||/usr/bin/cronolog /home/cloud/www/totaki.com/logs/%Y/%m/%d/access” combined