How to Configure Logging Rotation on Apache and Nginx

How to Configure Logging Rotation on Apache and Nginx

Logging rotation is essential for maintaining the performance and manageability of your web servers, such as Apache and Nginx. Proper configuration ensures that log files do not consume excessive disk space while keeping the logs organized and accessible. Below is a comprehensive guide on how to configure logging rotation for both Apache and Nginx.

Configuring Logging Rotation on Apache

Apache typically uses the logrotate utility for managing its log files. Here’s how to configure it:

  1. Locate the Logrotate Configuration File: The main configuration file for logrotate is usually found at /etc/logrotate.conf. However, Apache log rotation settings are often located in /etc/logrotate.d/apache2 or /etc/logrotate.d/httpd, depending on your distribution.
  2. Edit the Apache Specific Logrotate File: Open the file using a text editor, for instance:
    sudo nano /etc/logrotate.d/apache2
  3. Set Up Log Rotation Rules: Below is an example of how to configure the rotation settings:
            /var/log/apache2/*.log {
                daily
                missingok
                rotate 14
                compress
                delaycompress
                notifempty
                create 640 www-data adm
                sharedscripts
                postrotate
                    /usr/sbin/apachectl graceful > /dev/null
                endscript
            }

    This configuration does the following:

    • daily - Rotates the logs daily.
    • rotate 14 - Keeps the last 14 log files before deleting older ones.
    • compress - Compresses old log files to save space.
    • notifempty - Skips rotation for empty log files.
    • postrotate - Executes commands after rotation, here it gracefully restarts Apache.
  4. Save and Exit: After making changes, save the file and exit the text editor.

Configuring Logging Rotation on Nginx

Nginx also utilizes the logrotate utility for log rotation. The process is similar to Apache:

  1. Locate the Nginx Logrotate Configuration File: Similar to Apache, you’ll find Nginx's log rotation configuration in /etc/logrotate.d/nginx.
  2. Edit the Nginx Specific Logrotate File: Open the Nginx logrotate file with your text editor:
    sudo nano /etc/logrotate.d/nginx
  3. Set Up Log Rotation Rules: Below is a typical configuration for Nginx:
            /var/log/nginx/*.log {
                daily
                missingok
                rotate 30
                compress
                delaycompress
                notifempty
                create 640 www-data adm
                sharedscripts
                postrotate
                    /usr/sbin/service nginx reload > /dev/null
                endscript
            }

    This configuration includes:

    • daily - Rotates logs daily.
    • rotate 30 - Retains the last 30 log files.
    • compress - Compresses old log files.
    • postrotate - Reloads Nginx after the logs are rotated.
  4. Save and Exit: Upon completing the configuration, save the changes and exit the text editor.

Testing the Configuration

After configuring log rotation, it’s crucial to test whether the settings work as intended. You can do this by running:

sudo logrotate -d /etc/logrotate.conf

This command will run logrotate in debug mode, displaying what actions would be taken without making any changes.

Conclusion