Fix permission and ownership issues when updating Ghost

Fix permission and ownership issues when updating Ghost

The other day I updated this Ghost blog from version 4.X to 5.X. When updating I encountered a problem with permission denied to .ghost-cli and to create a subdirectory under "/var/www/ghost/versions/5.XX.XX".  

✔ Checking system Node.js version
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
✔ Checking for available migrations
An error occurred.
Message: 'EACCES: permission denied, open '/var/www/ghost/.ghost-cli''
✔ Checking system Node.js version
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
✔ Checking for available migrations
An error occurred.
Message:'EACCES: permission denied, mkdir '/var/www/ghost/versions/5.XX.XX''

Naturally turning to Google for answers, but most of the answers I could find were that either:

  1. The user you're trying to update with is not the user you initially installed Ghost with
  2. The user has lost the rights to the directory "/var/www/ghost/"

I double-checked that I was logged in with the users I initially installed Ghost with so that couldn't be the problem. Most suggestions on the Ghost forum suggested taking ownership of the folder and then retrying the update. So I ran the following to take ownership of the ghost folder again.

sudo chmod 777 /var/www/ghost
sudo chown -R manshurtigh:manshurtigh /var/www/ghost/

I restarted the update and everything worked flawlessly. The update continued without any problem. However, when the update was finished I couldn't start the instance. I got the following error message:

Systemd process manager has not been set up or is corrupted

The CLI suggested running ghost setup linux-user systemd as a possible fix but running that command changed nothing. After running the command I got the following message:

Systemd service has already been set up. Skipping Systemd setup

Running ghost doctor was not helpful and didn't show any problems.

I suspected that something with the original problems of file creation was the cause of the problem.

I browsed into folder "/var/www/ghost" and ran the command ls -lha which gave me the following output:

cd /var/www/ghost
ls -lha

drwxrwxr-x  6 manshurtigh manshurtigh 4.0K Jan  3 22:21 .
drwxr-xr-x  4 root        root        4.0K May  5  2021 ..
-rw-rw-r--  1 manshurtigh manshurtigh  199 Jan  3 22:04 .ghost-cli
drwxrwxr-x  2 manshurtigh manshurtigh 4.0K Jan  3 22:21 backup
-rw-rw-r--  1 manshurtigh manshurtigh  531 Jan  3 22:04 config.production.json
drwxrwxr-x 11 manshurtigh manshurtigh 4.0K Jan  3 20:55 content
lrwxrwxrwx  1 manshurtigh manshurtigh   30 Jan  3 22:04 current -> /var/www/ghost/versions/5.26.4
drwxrwxr-x  3 manshurtigh manshurtigh 4.0K Jun 27  2021 system
drwxrwxr-x  4 manshurtigh manshurtigh 4.0K Jan  3 22:04 versions

I noticed that the folder content and system had the wrong ownership and after running the sudo chown -R ghost:ghost content and sudo chown -R root:root system I was able to start the instance with ghost startagain.

Special thanks to Olivier Simard-Casanova at TechStack that pointed me in the right direction.