Hostxpeed
Login Get Started →
Server Management

How to Set Up Log Rotation

6 min read
23 views
Jun 10, 2026

Prerequisites

Before setting up log rotation, make sure you have:

  • SSH access to your VPS
  • Root or sudo privileges

💡 Log rotation prevents disk space exhaustion by keeping logs from growing indefinitely.

Step 1: Understand Current Log Rotation

Connect to your VPS:

ssh hxroot@YOUR_SERVER_IP -p 22

Check system logrotate configuration:

cat /etc/logrotate.conf

View application-specific rules:

ls -la /etc/logrotate.d/

Step 2: Create Custom Log Rotation Rule

sudo nano /etc/logrotate.d/myapp

Example rule for custom application logs:

/var/log/myapp/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
    sharedscripts
    postrotate
        systemctl reload myapp > /dev/null 2>&1 || true
    endscript
}

Step 3: Logrotate Directive Explanations

  • daily – Rotate logs daily (options: daily, weekly, monthly, yearly)
  • rotate 7 – Keep 7 rotated logs before deleting
  • compress – Compress rotated logs with gzip
  • delaycompress – Delay compression by one rotation
  • missingok – Don't error if log file is missing
  • notifempty – Don't rotate empty logs
  • create 644 root root – Create new log file with these permissions
  • sharedscripts – Run postrotate script once, not per log
  • postrotate/endscript – Commands to run after rotation (e.g., restart app)
  • maxsize 100M – Rotate when log reaches 100MB (overrides time)

Step 4: Test Logrotate Configuration

sudo logrotate -d /etc/logrotate.conf

Dry run (verbose, no changes).

Force run:

sudo logrotate -vf /etc/logrotate.conf

Step 5: Monitor Log Rotation

ls -la /var/log/myapp/

Check logrotate status:

cat /var/lib/logrotate/status

Common Examples

Nginx logs (daily, keep 14 days, compress):

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

MySQL slow log (weekly, keep 4 weeks):

/var/log/mysql/slow.log {
    weekly
    rotate 4
    compress
    missingok
    create 640 mysql adm
    postrotate
        mysqladmin flush-logs
    endscript
}

Force Rotation for a Specific Log

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

✅ Log rotation has been configured. Your logs will now be managed automatically.

Was this article helpful?