How to Set Up Log Rotation VPS Before Logs Fill the Disk

How to Set Up Log Rotation VPS Before Logs Fill the Disk

Logs grow quietly. A web server writes a line for every request, a database logs every slow query, and a container runtime keeps everything its apps print. On a small VPS with 20 or 40 GB of disk, that steady trickle turns into gigabytes in a few weeks, and when the disk hits 100 percent, MySQL stops writing, deploys fail, and your site goes down. The fix is simple: set up log rotation vps owners can rely on, so old logs are compressed, trimmed, and deleted on a schedule instead of piling up forever. This guide shows you how to do it with logrotate, journald limits, and Docker log rules.

Why Logs Fill Up a VPS Disk So Fast

Most VPS disks are small, and logs are one of the few things on a server that grow without anyone asking them to. Nginx writes a line for every visitor and bot, which can be tens of thousands of lines a day on a busy site. Your app logs add errors and debug output, systemd’s journal collects service output, and Docker keeps everything each container prints to stdout. None of these files shrink on their own.

The first sign is usually a vague error: a database that refuses writes or a deploy that fails halfway. By the time you check, the disk is at 100 percent and a single log file is several gigabytes. Before you set up rotation, check where the space actually went with du -h --max-depth=1 /var/log, and learn how to monitor VPS disk space before it fills up and crashes MySQL so you get a warning next time instead of an outage.

How to set up log rotation vps with logrotate

Logrotate is the standard tool for this job on Debian and Ubuntu, and it is usually already installed. It runs once a day from a cron job or a systemd timer and reads the files in /etc/logrotate.d/. Rotating a log means renaming the current file, starting a fresh one, compressing the old one, and deleting copies older than the number you chose to keep.

Here is a solid logrotate config for nginx, saved as /etc/logrotate.d/nginx:

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

Each line does one job. daily rotates once a day. rotate 14 keeps two weeks of old logs. compress gzips the old files, and delaycompress keeps the newest old log uncompressed for quick grepping. missingok means logrotate will not error out if a log file is absent, and notifempty skips rotation when a log is empty. sharedscripts runs the postrotate block once for all matched files instead of once per file, and the postrotate block sends nginx a USR1 signal so it reopens its log files instead of writing into a renamed file.

For a log that grows in bursts, add a size limit so rotation happens when the file gets big, not just once a day:

/var/log/myapp/*.log {
    size 100M
    rotate 7
    compress
    missingok
    notifempty
    copytruncate
}

The size 100M line rotates the log as soon as it passes 100 MB. copytruncate copies the file and then empties it in place, which is the right choice for apps that keep the log file open and cannot be told to reopen it. Only use it when the app has no reload signal, since a few lines written during the copy can be lost.

Nginx Log Rotation for Access and Error Logs

Nginx on Debian and Ubuntu already ships with a logrotate config, so you may not need to write one from scratch. Check /etc/logrotate.d/nginx and adjust the numbers to fit your disk. On a small VPS, 14 days of access logs is usually plenty, and 7 days works if disk is tight. Keep error logs longer than access logs, since they matter more when something breaks.

Two mistakes cause most nginx log rotation problems. The first is rotating without telling nginx, so it keeps writing into the renamed file and the new log stays empty. The USR1 signal in the postrotate block fixes that. The second is a log file that never rotates because the config file has a syntax error. Run sudo logrotate --debug /etc/logrotate.d/nginx to preview what it would do, and fix any errors before you trust the setup. If you want to catch problems in the error log early, see our guide on how to monitor nginx error logs and get alerts on 5xx spikes.

Limit the Journald Log Size

Logrotate does not touch systemd’s journal, so journald needs its own limit. By default, journald can use up to 10 percent of the filesystem, which on a 40 GB disk is 4 GB of logs you probably never read. That is a lot of space for boot messages and service chatter, so set a firm cap.

Open /etc/systemd/journald.conf and set:

SystemMaxUse=500M
SystemKeepFree=1G

SystemMaxUse caps the total journal size, and SystemKeepFree makes journald leave at least 1 GB free on the disk. Then restart the service:

sudo systemctl restart systemd-journald

To clean up right away instead of waiting for old entries to age out, run sudo journalctl --vacuum-size=500M, which deletes the oldest entries until the journal fits under 500 MB. Check the current journald log size any time with journalctl --disk-usage. If it keeps climbing after you set the cap, look for a service stuck in a crash loop with journalctl -p err -b.

Docker Log Management With the Json-File Driver

Docker has its own log problem. The default json-file logging driver keeps every line a container prints, forever, with no size limit at all. A chatty container can fill a disk in days, and logrotate never sees these files because they live under /var/lib/docker/containers/, not /var/log/.

Set a global limit in /etc/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

This keeps at most three 10 MB files per container, or 30 MB each. Restart Docker with sudo systemctl restart docker to apply it. The limit only applies to containers created after the change. For a container that is already running, set the limit per container instead:

docker run --log-opt max-size=10m --log-opt max-file=3 myimage

Pair this with image cleanup, because old images and stopped containers eat disk too. Our guide on how to clean up old Docker images and free disk space on your VPS covers that side of the problem, and for a deeper look at container logs, read our guide on Docker log management and how to stop logs filling the disk.

Test Your Rotation Before You Trust It

A logrotate config you never tested is a guess. Run a dry run first:

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

The debug output shows which logs match, whether each one needs rotating, and what commands would run. Fix any errors it reports. Then force one real rotation to prove the whole chain works:

sudo logrotate -f /etc/logrotate.d/nginx
ls -lh /var/log/nginx/

You should see a fresh, small access log and a compressed .gz file for yesterday. Check the app is still writing to the new file by tailing it after making a request. Also confirm the daily run is scheduled: on most systems it runs from /etc/cron.daily/logrotate or a systemd timer (systemctl list-timers | grep logrotate). If neither exists, add the cron entry yourself, or the config will sit there doing nothing.

What to Do When the Disk Is Already Full

If you are reading this with a full disk, rotation alone will not save you, because logrotate only acts on its daily schedule. Start by finding the biggest files:

sudo du -h /var/log/* | sort -rh | head -20
sudo journalctl --disk-usage
docker system df

Free space fast by vacuuming the journal with sudo journalctl --vacuum-size=200M and forcing a rotation with sudo logrotate -f /etc/logrotate.conf. If one log file is huge and still being written to, empty it in place with : > /var/log/nginx/access.log instead of deleting it. Deleting a file a process still has open frees nothing until the process restarts.

Once the disk has breathing room, set up the rotation rules above so the problem does not come back, and review your backup retention too, because old backups are the other quiet disk eater. Our guide on how to archive old backups with retention policies that save disk space walks through that side of the cleanup.

When you set up log rotation vps the right way, it is a one-hour job that pays off every day after. Set the rules once, test them with a forced rotation, and check the disk usage a week later. If the numbers are stable, your logs are under control, and the next disk-full outage is one you will never have.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.