Maintaining logs in busy Apache server is a very painful as frequent access of Apache webservice from clients can generate large size of logs example access_log and error_log which can consume the filesystem space but we should not stop generating these logs also as these logs are very helpful in troubleshooting the apache related issues when in case apache does not work correctly.
I had also encountered with same problem when Apache logs start consuming lot of filesystem space in one of my SAP Content RHEL 7 server therefore for remediating this problem I have chosen Linux logrotation utility. Logrotate utility can automatic rotate, compress, remove and mail the system and application log files. It can rotate the log file daily, weekly, monthly or when the log file gets to a certain size. Normally, logrotate runs through daily cron job.
In this post I will guide you how to enable logrotation for Apache service by keeping relevant days log so that it can save Linux filesystem space consumption.
Installing logrotation utility in RHEL / CentOS server
By default, logrotation utility get install with the Base system installation but if in case logrotate utility is not available in OS you can install the logrotation separately with below command:
yum install logrotate
Configuration of Apache Logrotation
There are two ways to configure the logrotation either you can configure the Apache logrotation parameters directly in /etc/logrotate.conf or you can create separate logorate file under /etc/logrotate.d/ directory path. I preferred the second option since it felt me easy to manage the Apache logrotation configuration. I created separate apache logrorate file and configured it as below:
Note: You can provide any name to Apache logrotation file as per your choice.
Reference to my above sample Apache logrotation file below is the definition of each logrotation configuration parameter:
- First line indicates the directives of Apache logs which need to rotate
- rotate 4 indicates that only 4 rotated logs should be kept. So, the oldest file will be removed on the fifth subsequent run.
- size 20M is a threshold size of each Apache log which means logs will not be rotated unless logs size will reach above the 20 MB of size
- sharedscript parameter allow us to define either command or script path which can be run before the logrotation as in example of my logrotation configuration file I have defined the command to perform the graceful restart of Apache service before logrotation rotate the logs since Apache won’t allow the logrotation to rotate the logs unless it release its hold from writing the logs, further sleep 600 wait the Apache service for some time for restarting it after the graceful stop before doing any processing on the new log files. But since I am using Apache graceful command so it will not interrupt the existing clients connectivity with the Apache Webservice.
Automate the logrotation with cron
One thing needs to understand, during logrotation utility installation either manually or through Base system installation Linux by default create a logrotation cron job, so you need not to create the cron job explicitly. You can find logrotation script under /etc/cron.daily path as shown in below screenshot:
Validation of Apache logrotation configuration
Command logrotation allow you to do dry run the logrotation configuration file so that logoration file can be validated without waiting for file to run through cron job. Below is an example of logrotation dry run command which I executed for checking Apache logrotation file configuration:
Below is the example of Apache rotated log files through daily cron job in my server: