Back to articles
Technology Insight

Optimizing VPS Performance: A Comprehensive Guide to Logrotate for Automated Log Management

June 1, 2026

Introduction to Efficient Log Management

In the world of server administration, logs are both a blessing and a curse. They provide invaluable insights into system performance, security breaches, and application errors. However, without proper management, these files can grow exponentially, eventually consuming all available disk space on your Virtual Private Server (VPS). When a disk reaches 100% capacity, services fail, databases corrupt, and the entire system can become unresponsive. This is where Logrotate becomes an essential tool for every system administrator.

Logrotate is a powerful utility designed to simplify the administration of log files. It allows for the automatic rotation, compression, removal, and mailing of log files. By systematically managing these files, you can ensure that your server maintains peak performance without manual intervention. This guide will walk you through everything you need to know about configuring Logrotate to prevent 'disk full' errors and keep your VPS running smoothly.

Understanding the Logrotate Mechanism

Before diving into the configuration, it is crucial to understand how Logrotate functions. Typically, Logrotate is run as a daily cron job. It reads its configuration from a primary file and various service-specific files located in the system directory. The process generally follows these steps:

  • Check: Logrotate identifies logs that meet the criteria for rotation (e.g., age or size).
  • Rotate: The current log file is renamed (e.g., access.log becomes access.log.1).
  • Create: A new, empty log file is created to continue recording data.
  • Compress: Older logs are compressed (usually into .gz format) to save space.
  • Post-process: Services are signaled to restart or reload to begin writing to the new file.
  • Remove: Logs older than a specified threshold are deleted.

Step 1: Installing Logrotate

Most modern Linux distributions, including Ubuntu, Debian, CentOS, and AlmaLinux, come with Logrotate pre-installed. However, it is always wise to verify its presence or install it if missing. Use the following commands based on your package manager:

For Debian/Ubuntu systems:
sudo apt update && sudo apt install logrotate

For RHEL/CentOS/AlmaLinux:
sudo yum install logrotate

Step 2: Exploring the Configuration Structure

Logrotate’s configuration is structured to allow for both global settings and granular control over specific applications. Understanding this hierarchy is key to effective management.

The Main Configuration File

The primary configuration file is located at /etc/logrotate.conf. This file contains default settings that apply to all logs unless overridden. Common defaults include weekly rotation and keeping four weeks of backlogs.

The Configuration Directory

Specific configurations for various services (like Nginx, Apache, or MySQL) are stored in the /etc/logrotate.d/ directory. Logrotate reads every file in this directory and applies the rules defined within. This modular approach makes it easy to manage logs for different applications independently.

Step 3: Creating a Custom Logrotate Configuration

Let us create a practical example. Suppose you have a custom application generating logs at /var/log/myapp/error.log. To manage these logs, you would create a new configuration file: /etc/logrotate.d/myapp.

Here is a professional-grade configuration snippet and an explanation of the directives used:

/var/log/myapp/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        /usr/lib/apache2/apache2ctl graceful > /dev/null
    endscript
}

Detailed Breakdown of Directives

  • daily: Rotates the log every day. Other options include weekly, monthly, or yearly.
  • missingok: If the log file is missing, do not issue an error message; move on to the next one.
  • rotate 14: Keeps 14 rotated log files. On the 15th rotation, the oldest file is deleted.
  • compress: Uses gzip to compress old log files, significantly reducing disk usage.
  • delaycompress: Postpones compression until the next rotation cycle. This is useful if some processes still have the old file open.
  • notifempty: Do not rotate the log if it is empty. This prevents unnecessary file clutter.
  • create [mode] [owner] [group]: Immediately creates a new log file with the specified permissions and ownership.
  • sharedscripts/postrotate: These tags allow you to run custom shell commands after the rotation is complete, such as restarting a service to refresh the file handle.

Step 4: Advanced Techniques for VPS Storage Optimization

For high-traffic servers, simply rotating by time might not be enough. If a log file grows to several gigabytes in a single day, it could still crash your server. To prevent this, use size-based rotation.

By adding the size 100M directive, Logrotate will trigger as soon as the file exceeds 100 Megabytes, regardless of whether it is daily or weekly. This is a critical safety net for VPS environments with limited SSD storage.

Step 5: Testing and Troubleshooting

Before leaving Logrotate to run automatically, you should verify that your configuration is syntactically correct. You can perform a "dry run" which simulates the process without actually moving or deleting any files:

sudo logrotate -d /etc/logrotate.d/myapp

If everything looks correct, you can force a manual rotation to see the results immediately:
sudo logrotate -f /etc/logrotate.d/myapp

Conclusion

Implementing a robust Logrotate strategy is a non-negotiable task for maintaining a healthy VPS. By automating the cleanup and compression of log files, you protect your system from critical storage failures, improve searchability of historical data, and ensure that your server resources are focused on delivering your core services. Don't wait for a disk-full error to disrupt your business—configure Logrotate today.

Optimizing VPS Performance: A Comprehensive Guide to Logrotate for Automated Log Management | DPTCloud