Rotate Apache logs on a daily basis using rotatelogs

If you Apache to start a new log file every day automatically, rotatelogs is the simple way to do it. Rotatelogs is a simple program for use in conjunction with Apache’s piped logfile feature. It supports rotation based on a time interval or maximum size of the log. The use of rotatelogs is actually quite simple. Search your httpd.conf and replace the following line

1
CustomLog logs/access_log combined

to

1
CustomLog "/usr/sbin/rotatelogs /var/log/httpd/access_log.%Y-%m-%d 86400" combined

86400 (= 60 * 60 * 24) which causes a new log file each day at midnight The rotatelogs in default record log using GMT time. If your system is using local time rather than GMT, you need to add -l option.

1
CustomLog "/usr/sbin/rotatelogs -l /var/log/httpd/access_log.%Y-%m-%d 86400" combined

Now check if the syntax is OK and give Apache a graceful. Run the command below to check the configuration syntax.

1
2
3
/usr/sbin/apachectl configtest

Syntax OK

Graceful restart. The apachectl graceful command tells Apache that you want to restart the server gracefully without aborting any open connections. It automatically checks the configuration files before initiating the restart to make sure Apache doesn’t die.

1
/usr/sbin/apachectl graceful

If apachectl graceful doesn’t restart your server, there are a few other things you can try.

  • apachectl restart to restart the server. If the server is not running it is started. This command also runs a configuration test to make sure Apache won’t die when it restarts.
  • apachectl stop to stop the Apache server
  • apachectl start to start the Apache server (will throw an error message if Apache is running)
  • apachectl configtest to test the configuration file syntax.
Setup log rotation for Apache access logs error logs using cronolog Host Your DNS With Google Cloud DNS

Comments