So, I've been using a self-hosted Ghost installation for running my blog for a few months. Whilst I love the platform, I have been getting a 502 Bad Gateway everytime I try to upgrade the Ghost platform.

This is partly my fault as I'm using a small AWS Lightsail instance with 512MB of RAM whereas Ghost recommends at least 1GB. However, to keep costs down, I'm sticking with the smaller instances especially as this upgrade issue is the only problem I have with it.

Whenever the upgrade has failed in the past, I've been forced to reinstall Ghost from scratch and get my content folder and posts export from a backup. However, I have found a way to force the upgrade to complete successfully without reinstallation. Bear in mind that I am currently on Ubuntu 16.04, ghost is installed at /var/www/ghost and I'm currently on the 1.x track of Ghost.

The issue seems to be with a node module so if, or when, the upgrade fails. Simply head to /var/www/ghost/current and run the following commands:

sudo rm -rf node_modules
npm cache clean
sudo npm install --production --unsafe-perm
sudo chown -R ghost:ghost *
cd ../
sudo ghost restart

This clears out and reinstalls all of the necessary node_modules required for Ghost core to work.

Note: Needless to say, make a backup of your /var/www/ghost/content folder before you start and make a JSON backup of your posts (using the Labs link in your Ghost admin panel) before you try any of this.