PSA: Lack of Log Rotation Configuration in Ghost Server Can Eventually Consume All Disk Space

Whenever I backup my Ghost content, I’ve noticed that rsync seems to hang as it moves two particularly large files- specifically the log files found in /var/www/ghost/content/logs/. (You can read more about my Ghost backup process here: Backup Your Ghost Content to unRAID).

Looking at the directory on my Unraid server, the log file directory was much larger than I expected, several gigs in fact. The culprit? The lack of logfile rotation with the default local Ghost install.

tl;dr: If you just want to fix this issue on your Ghost blog server, you can skip the below technical notes discussing how I settled on this solution and jump straight to the guide on setting up logrotate for Ghost here:

Attempted to fix by adding a new configuration entry to /etc/logrotate.d with sudo nano /etc/logrotate.d/ghost:

/var/www/ghost/content/logs/*.log {
        weekly
        rotate 12
        compress
}

After testing with sudo logrotate /etc/logrotate.conf --debug, forced rotation with sudo logrotate /etc/logrotate.conf -f.

This seemingly worked well, but I was concerned that Ghost might not “pick up” on the newly created log. I checked by reloading my website and checking to see if the production.log file size increased with ls -la in /var/www/ghost/content/logs/ and as I thought might be the case, it did not.

Running ghost update and reloading a web page on my blog resulted in resumption of log file updates.

So I’m going to need to add something like this to the configuration:

sharedscripts
postrotate
	ghost restart
endscript

Unfortunately, it’s still not quite this simple. Ghost has to be run as non-root, non-sudo user. So I’m going to need to run this as my ghost admin user.

Our Ghost admin user’s home directory is just a good a place as any:

nano /home/wwwserver/ghostLogRotation.conf

/var/www/ghost/content/logs/*.log {
	weekly
	rotate 12
	compress
	sharedscripts
	postrotate
		ghost restart
	endscript
}

Since we want to run this as our Ghost admin user, we will not specify sudo this time:

logrotate /home/wwwserver/ghostLogRotation.conf --state /home/wwwserver/logrotateState --verbose

Unfortunately this doesn’t work either, because your Ghost admin user doesn’t technically own the /var/www/ghost/content/logs/ directory. It’s actually owned by ghost:ghost and ironically my Ghost admin user doesn’t have the elevated permissions to handle the log rotation…so time to make a tactical withdrawal back to root-based version I started with:

/var/www/ghost/content/logs/*.log {
        weekly
        rotate 12
        compress
}

Funnily enough, this original was almost perfect. Unfortunately, however, we still need to address the issue with Ghost not using the “new” log after the rotation.

This can be resolved by using the following configuration:

/var/www/ghost/content/logs/*.log {
	daily
	rotate 12
	compress
	create 0644 ghost ghost
	sharedscripts
	postrotate
		systemctl restart ghost_engineerworkshop-com
	endscript
}

The reason I could not use ghost restart in my postrotate script is because Ghost is hardcoded to issue a password challenge. However, since we’re running as root anyway, we can simply restart the ghost service and bypass having to use Ghost’s “ghost restart” command.

This still isn’t ideal though: restarting causes an interruption in the Ghost service, meaning your site is technically down for a few seconds. And restarting a service also introduces the risk that the service fails to restart at which point your site is now in a downtime. Not great.

We can do better:

/var/www/ghost/content/logs/*.log {
	daily
	rotate 12
	compress
	copytruncate
}

This is perfect. The use of copytruncate negates the need to restart the ghost service, while still solving the problem of getting Ghost to use the “new” log. (Essentially, copytruncate copies the log in place and then rotates the copy so that Ghost never has to be notified of a log file change, it continues to use the old file (albeit a truncated one so we still get the benefit of a smaller log file)).

Hope this helps someone else in setting up their Ghost blog!

-TorqueWrench