Using Logrotate to Rotate PHP5-FPM Logs

Save disk space and keep your php5-fpm log files nice and tidy by utilizing logrotate.

I recently noticed my php5-fpm.log file on my Ubuntu 12.04 server getting fairly large, so I needed to use logrotate to compress the log file and create a new one. In /etc/logrotate.d we’ll have our individual application information, which overrides the defaults set in /etc/logrotate.conf . It’s in logrotate.d where we’ll create our php5-fpm log rotation file.

cd /etc/logrotate.d
sudo nano php5-fpm

Paste the following,

/var/log/php5-fpm.log {
	rotate 52
	create 0640 root adm
		invoke-rc.d php5-fpm restart > /dev/null

This will rotate the log on a weekly basis. Save the file. If you’re using SFTP rather than the command line, make sure you’re saving your file in UNIX format, otherwise you’ll get some errors when trying to rotate the log file. To test if it works, try sudo logrotate -d /etc/logrotate.d/php5-fpm. Read the output of that. If it looks good, you’re set! If you’d like to force a log rotation now, use the following command.

sudo logrotate --force /etc/logrotate.d/php5-fpm