Log rotation for WordPress, Nginx and PHP-FPM with logrotate and journald
Logs grow quietly. A busy site writes hundreds of megabytes of Nginx access log per week, a plugin with a notice in a loop fills debug.log with gigabytes, and one day the disk is full and MySQL stops. Two tools keep logs in check on a Linux server: logrotate for log files and journald settings for everything that systemd services write to the journal.
Problem: A server ran out of disk space. The culprits were a 9 GB wp-content/debug.log, Nginx logs that were never compressed and a systemd journal of 4 GB.
Solution: Move the WordPress debug log out of the web root and give it a logrotate rule with size limit and compression, check that the Nginx and PHP-FPM packages’ rules exist and fit, limit the journal with SystemMaxUse in journald.conf, and test every rule with logrotate -d.
The WordPress debug log
// wp-config.php define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', false ); define( 'WP_DEBUG_LOG', '/var/log/wordpress/example.com-debug.log' );
Since WordPress 5.1, WP_DEBUG_LOG accepts a path. Outside the web root, the log cannot be downloaded by visitors, and it can contain paths, queries and sometimes personal data. The folder must be writable for the PHP-FPM user:
sudo mkdir -p /var/log/wordpress sudo chown www-data:adm /var/log/wordpress sudo chmod 750 /var/log/wordpress
# /etc/logrotate.d/wordpress
/var/log/wordpress/*.log {
weekly
maxsize 100M
rotate 8
compress
delaycompress
missingok
notifempty
su www-data adm
create 0640 www-data adm
}
PHP opens the log file for every message it writes, so no reload is needed after a rotation: the next message goes into the new file. maxsize 100M rotates earlier than weekly when the file grows fast (logrotate runs daily from a systemd timer or cron). su makes logrotate work as the owner of the folder, which it requires when the folder is writable by a user other than root. delaycompress leaves the newest rotated file uncompressed for one more cycle, which is handy for a quick look.
Nginx and PHP-FPM
The packages of Debian and Ubuntu install their own rules in /etc/logrotate.d/nginx and /etc/logrotate.d/php8.2-fpm. They are usually fine; check the number of kept files and the reopen command:
# /etc/logrotate.d/nginx (as shipped, shortened)
/var/log/nginx/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
Nginx keeps its log files open; after the rename it would continue writing into the rotated file. The postrotate script tells it to reopen the files (nginx -s reopen does the same). sharedscripts runs it once for all matched files instead of once per file. Per-site logs in another folder, for example /var/www/example.com/logs/*.log, need their own rule with the same postrotate block.
Test a rule
sudo logrotate -d /etc/logrotate.d/wordpress # dry run: shows what would happen sudo logrotate -f /etc/logrotate.d/wordpress # force a rotation now ls -lh /var/log/wordpress/
The dry run reports syntax errors and permission problems (“parent directory has insecure permissions”) without touching the files.
The systemd journal
Services that log to stdout (PHP-FPM with catch_workers_output, Redis, custom workers) end up in the journal. Its default limit is 10% of the file system, capped at 4 GB, which is a lot on a small VPS:
# /etc/systemd/journald.conf.d/size.conf [Journal] SystemMaxUse=500M MaxRetentionSec=1month
sudo systemctl restart systemd-journald journalctl --disk-usage sudo journalctl --vacuum-size=500M # shrink now, without waiting journalctl -u php8.2-fpm --since today
NOTE: Rotation only limits the size; it does not explain why a log grows. A debug.log that reaches 100 MB in a week has a problem worth fixing: sort | uniq -c | sort -rn over the messages shows which notice repeats. A simple disk alert in addition prevents the next surprise: monitor a WordPress site with a shell script and cron. For the MySQL slow query log, the same logrotate pattern applies: MySQL slow query log.