Journeyman Notes #3: Finley Code was down - 502 Bad Gateway Error

Up until this point I haven't had any issues running and maintaining my blog. Well the beautiful thing about programming and technology is that you should expect something to break at some point. I finally experienced the heartache of broken things. Last Monday, when I was trying to upgrade my Ghost platform, my blog broke. Friday of last week, I finally figured out how to fix it and as such my first post back online I had to share what happened, troubleshooting and the inevitable fix.

The Problem

Monday, August 22nd, while running through an update of Ghost, upon completion I received a '502 Bad Gateway Error' and my site was rendered incapacitated. Disclaimer: I did use a different update command this time, which may be part of the reason why it broke... This is what I used: Update Ghost. I would now recommend this article instead Upgrade Ghost 2.0.

Troubleshooting

After I ran the update I visited my page only to receive a blank screen save for a message that read:

"502 Bad Gateway"

Basically what this means (I found during my research) is that the server was acting as a gateway and received an invalid response from an upstream server. Still not sure what all of this means but I am continuing to research and learn.

At first I was very frustrated and tried running the update a few different ways but to no avail. I also felt disheartened and immediately felt alone not knowing where to turn. So I took to Googling and found quite a bit of documentation on the 502 error. But no specific commands to fix my Ghost issue. It was then that I remembered Ghost has a great slack channel with wonderful people offering their advice, so I turned there. Also since I don't like to sit still I turned to Digital Ocean's message boards Digital Ocean Question Forum since they host my blog. This is where I ultimately received the help that lead to the fix.

Below is the picture of my terminal with the warnings that I received when I tried to update Ghost:

Based on my conversations with the Slack community since these are just warnings the blog should still run but it didn't. Luckily a gentleman on the Digital Ocean forum responded to my inquiry quickly and I was able to get back up and running.

The Resolution

After comment exchange on the Digital Ocean forum's I found a solution. Here is my fix from start to finish (I am sure there are others but for the time being this works):

I used this article as a baseline (the command line section) and updated the steps to account for my errors.

  1. Get the latest version:
    curl -LOk https://ghost.org/zip/ghost-latest.zip OR a specific version curl -LOk https://ghost.org/zip/ghost-<version>.zip

  2. Unzip to a temporary location:
    unzip ghost-latest.zip -d ghost-temp. You will come back to file shortly

  3. Change directory into your current ghost install:
    cd path-to-ghost-install, E.g. cd /var/www/ghost

  4. Copy key files to your Ghost install directory:
    cp index.js *.json path-to-ghost-install

  5. Update Casper by copying the casper folder(optional):
    cp -R content/themes/casper path-to-ghost-install/content/themes

  6. Change back to your ghost install directory:
    cd path-to-ghost-install

  7. (optional) Update permissions:
    E.g. chown -R ghost:ghost *

  8. Change directory into your current ghost install:
    cd path-to-ghost-install, E.g. cd /var/www/ghost

  9. Then run the following (this will remove your core and node_modules files as well):

     sudo apt-get update  
     sudo apt-get upgrade  
     npm cache clean 
     rm -rf node_modules  
     rm -rf core
    
  10. Next you need to add your new core file back into your directory, so change back to your download of Ghost latest:
    cd path-to-ghost-temp E.g. cd ~/Downloads/ghost-temp

  11. Copy the new core directory to your Ghost install:
    cp -R core path-to-ghost-install

  12. Upgrade dependencies:npm install --production (you may still see a warning here bur fear not).

  13. The start the productin environment: npm start --production.

  14. You may get an error about sqlite3 but just run the code below regardless: npm install sqlite3 -save and then npm start --production.

  15. You will find that your blog works however it will be terminated after you close your terminal. So you need run Ghost in forever mode type: npm install forever -g

  16. You made it. Last step restart Ghost in forever mode from the Ghost installation directory: NODE_ENV=production forever start index.js.

Two other helpful forever commands:

  • To stop Ghost type: forever stop index.js
  • To check if Ghost is currently running type: forever list

If you find any holes or issues please let me know as I would be curious.

Lastly, and for reference, here is my forum exchange on Digital Ocean as well for additional reference: 502 Bad Gateway Discussion

My take-aways here are three-fold:

  • Always Google to try and find your answer. Stack Overflow should become your best friend.
  • Don't be afraid to ask for help even from complete strangers, which is exactly what I did on Digital Ocean and it was scary at first.
  • Try, try try no matter what and don't ever give up. There is always a solution.

Shout Outs