Logs are essential for troubleshooting. But they accumulate quickly. A busy Nginx server can generate gigabytes of logs per month. If you don't manage them, your disk fills up and your VPS crashes.

Logrotate is a built‑in Linux utility that automatically rotates, compresses, and deletes old log files. This guide shows you how to configure it.

What Is Logrotate?

Logrotate is a system utility that manages log files. It rotates logs periodically (daily, weekly, monthly), compresses older logs, and removes logs older than a specified age. It runs daily via cron.

Most Linux distributions include logrotate by default. You don't need to install anything.

Step 1: Check Current Logrotate Configuration

Logrotate configuration files are located in two places:

  • /etc/logrotate.conf – Main configuration file
  • /etc/logrotate.d/ – Directory containing service‑specific configurations

Check the main configuration:

cat /etc/logrotate.conf

Common default settings:

weekly
rotate 4
create
include /etc/logrotate.d

These mean: rotate logs weekly, keep 4 old logs, create new empty log file after rotation, and include service‑specific configs from the /etc/logrotate.d directory.

Step 2: Configure Nginx Log Rotation

Create or edit the Nginx logrotate configuration:

sudo nano /etc/logrotate.d/nginx

Add this configuration:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 640 www-data adm
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

What each directive means:

  • daily – Rotate logs every day
  • missingok – Don't error if the log file is missing
  • rotate 7 – Keep 7 days of logs
  • compress – Compress rotated logs with gzip
  • delaycompress – Don't compress the most recent rotated log (so you can still read it easily)
  • notifempty – Don't rotate empty files
  • create 640 www-data adm – Create new log file with specific permissions
  • postrotate/endscript – Command to run after rotation (reloads Nginx so it writes to the new log file)

Step 3: Configure Apache Log Rotation

For Apache:

sudo nano /etc/logrotate.d/apache2

Add this configuration:

/var/log/apache2/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 640 www-data adm
    postrotate
        /etc/init.d/apache2 reload > /dev/null
    endscript
}

Step 4: Configure MySQL Log Rotation

For MySQL slow query logs and error logs:

sudo nano /etc/logrotate.d/mysql
/var/log/mysql/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    create 640 mysql adm
    postrotate
        [ -f /var/run/mysqld/mysqld.pid ] && kill -USR1 `cat /var/run/mysqld/mysqld.pid`
    endscript
}

Step 5: Test Logrotate Configuration

Test your configuration without making changes:

sudo logrotate -d /etc/logrotate.d/nginx

The -d flag runs in debug mode – it shows what would happen without actually rotating files.

Force a manual rotation (for testing):

sudo logrotate -f /etc/logrotate.d/nginx

Check if the rotation worked:

ls -la /var/log/nginx/

You should see files like access.log, access.log.1, access.log.1.gz (if compression is enabled).

Step 6: Monitor Disk Usage

Check disk usage on your VPS:

df -h

Check the size of log directories:

du -sh /var/log/nginx/
du -sh /var/log/apache2/
du -sh /var/log/mysql/

If you find a specific directory using too much space, adjust the rotate value (reduce the number of kept logs) or change the rotation frequency from daily to weekly.

Step 7: Custom Logrotate Options

Here are some additional useful options:

  • size 100M – Rotate when the log reaches 100 MB (instead of on a schedule)
  • maxsize 200M – Rotate if the log exceeds this size, even if the time hasn't arrived
  • maxage 30 – Remove rotated logs older than 30 days
  • dateext – Use date (YYYYMMDD) as the extension instead of a number

Example with size‑based rotation:

/var/log/nginx/*.log {
    size 100M
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    create 640 www-data adm
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

Step 8: View Logrotate Status

Check when logrotate last ran:

sudo cat /var/lib/logrotate/status

This file shows the last rotation date for each log file.

Troubleshooting

Logs not rotating – Check permissions: ensure the log directory is writable by the logrotate user. Check logrotate service status: sudo systemctl status logrotate.

Permission denied errors – The create directive must specify the correct user and group. For Nginx, use create 640 www-data adm. For Apache, use create 640 www-data adm.

Log file not being recreated – The create directive creates a new log file. If missing, check the file path and the user/group specified.

Next Steps

Logrotate keeps your disk clean without manual intervention. Set it up once and forget it. Check your log directories periodically to ensure the configuration is working as expected.

Need a VPS to practice on? Check our recommended VPS providers.