How to Configure Daily Log Rotation for Apache on CentOS Using logrotate


9 views

Apache log rotation is essential for managing disk space and maintaining organized server logs. On CentOS/RHEL systems, the logrotate utility provides automated log rotation capabilities.

Most CentOS installations already include a basic logrotate configuration for Apache at /etc/logrotate.d/httpd. The default setup typically looks like:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

For daily rotation with compression and 30-day retention, create or modify the configuration:

/var/log/httpd/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 640 root apache
    sharedscripts
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}
  • daily: Rotate logs every day
  • rotate 30: Keep 30 days of logs
  • compress: Use gzip compression
  • delaycompress: Compress previous log file only
  • create: Set proper permissions when creating new files

Verify your configuration with:

logrotate -d /etc/logrotate.d/httpd

Force immediate rotation with:

logrotate -f /etc/logrotate.d/httpd

For environments with multiple virtual hosts, you can implement separate rotation:

/var/log/httpd/example.com-*log {
    daily
    rotate 14
    compress
    delaycompress
    sharedscripts
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

By default, logrotate runs daily via cron. Check /etc/cron.daily/logrotate or verify with:

cat /etc/crontab | grep logrotate

Apache log rotation is crucial for maintaining server health and preventing disk space issues. On CentOS Linux, we have several effective methods to implement daily log rotation. Let's explore the most reliable approaches.

The most robust solution involves configuring logrotate, which comes pre-installed on CentOS. Create or modify the Apache logrotate configuration file:

sudo vi /etc/logrotate.d/apache

Add the following configuration (adjust paths as needed):

/var/log/httpd/*log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 root apache
    sharedscripts
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

To ensure daily execution, verify logrotate runs via cron:

sudo cat /etc/cron.daily/logrotate

If missing, create a cron job:

sudo crontab -e
0 0 * * * /usr/sbin/logrotate -f /etc/logrotate.conf

For custom requirements, create a rotation script:

#!/bin/bash
DATE=$(date +%Y%m%d)
APACHE_LOG_DIR="/var/log/httpd"

for logfile in ${APACHE_LOG_DIR}/*.log; do
    mv "$logfile" "${logfile}-${DATE}"
done

# Send USR1 signal to Apache for graceful log rotation
pkill -USR1 httpd

# Optional: Compress old logs
find ${APACHE_LOG_DIR} -name "*.log-*" -mtime +14 -exec gzip {} \;

# Optional: Remove very old logs
find ${APACHE_LOG_DIR} -name "*.log-*.gz" -mtime +30 -exec rm -f {} \;

If logs aren't rotating properly:

  1. Verify permissions: sudo chown root:root /etc/logrotate.d/apache
  2. Check syntax: sudo logrotate -d /etc/logrotate.d/apache
  3. Test manually: sudo logrotate -vf /etc/logrotate.d/apache
  4. Monitor disk space: df -h and du -sh /var/log/httpd/

For high-traffic servers, consider these enhancements:

/var/log/httpd/*log {
    size 100M
    daily
    maxage 30
    rotate 7
    compress
    dateext
    dateformat -%Y%m%d
    extension .log
    create
    sharedscripts
    postrotate
        /usr/bin/systemctl reload httpd > /dev/null 2>/dev/null || true
    endscript
}