logrotate config example: rotate and compress log files
Keep log files from filling your disk: rotate, compress and delete old logs on a schedule.
logrotate renames old log files, compresses them and deletes the oldest, so logs never fill your disk. Enter the path of your logs and how many copies to keep, then save the result in /etc/logrotate.d.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
/var/log/myapp/*.log {
daily
rotate 14
compress
missingok
notifempty
create 0640 www-data adm
}How this setup works
Step-by-step setup
- Save the config as /etc/logrotate.d/myapp: sudo nano /etc/logrotate.d/myapp.
- Do a dry run that changes nothing: sudo logrotate -d /etc/logrotate.d/myapp.
- Force one rotation to see it work: sudo logrotate -f /etc/logrotate.d/myapp.
- Look in the log folder. Old logs appear as .1 and .2.gz.
- Logrotate then runs by itself, normally once a day.
What the important lines mean
- rotate 14 keeps 14 old copies of each log.
- compress shrinks old logs with gzip.
- missingok and notifempty skip logs that are missing or empty.
- postrotate tells the app to open a fresh log file.
Common errors and how to fix them
- error: skipping … because parent directory has insecure permissions
- The log folder is writable by someone other than root. Add a line like su root adm inside the braces, using the folder’s owner and group, or fix the folder permissions.
- Logs are not rotated
- Logrotate checks once a day. Force a run with -f to test, and read /var/lib/logrotate/status.
- The app keeps writing to the old file
- The app holds the file open. Add a command after rotating that makes it reopen its log, or use copytruncate.
- error: unknown option
- A typo, or a directive your version does not have. The dry run (-d) shows the line.
Frequently asked questions
- Where do logrotate configs go?
- In /etc/logrotate.d/, one file per app. The main file /etc/logrotate.conf includes that folder.
- How do I test a logrotate config?
- Run sudo logrotate -d with the file name for a dry run, and -f to force a rotation.
- What does copytruncate do?
- It copies the log and empties the original, for apps that cannot reopen their log. A few lines can be lost in between.
More server configs
- fail2ban jail.localBan addresses that keep failing to log in. Choose ban time, retries and which services to protect.
- PM2 ecosystem.config.jsRun a Node.js app with PM2: restarts on crash, memory limit, cluster mode and start at boot.
- WireGuard server config (wg0.conf)Set up a WireGuard VPN server on Linux with one peer, NAT and the right commands.
- WireGuard client configCreate the client file that connects to your WireGuard server, with DNS, allowed IPs and keepalive.
- nginx HTTPS server blockServe your site over HTTPS with nginx: certificate paths, HTTP to HTTPS redirect, HSTS and a proxy or static site.
- .htaccess fileBuild an Apache .htaccess file with an HTTPS redirect, www rules, compression, caching and a front controller.