Upgraded Ghost to 0.9: 502 Bad Gateway

August 7, 2016 2.8k views
Ghost Nginx

I've just upgraded Ghost using the official guide.

Now when I try to reach my blog it says: 502 Bad Gateway.

In the error log, it says:

2016/08/07 07:32:43 [error] 953#0: *22 connect() failed (111: Connection refused) while connecting to upstream, client: ***, server: my-ghost-blog.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:2368/favicon.ico", host: ***SERVER IP ADDRESS***, referrer: ***SERVER IP ADDRESS***

I checked the config.js file and URL is set to my correct IP address and not to my-ghost-blog.com as per default.

Possibly relevant: when I followed the last step and executed npm install --production I got a bunch of warnings back:

npm WARN cannot run in wd ghost@0.9.0 node core/server/utils/npm/preinstall.js (wd=/var/www/ghost)
npm WARN unmet dependency /var/www/ghost/node_modules/bookshelf requires bluebird@'^2.9.4' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/bluebird,
npm WARN unmet dependency which is version 3.4.1
npm WARN unmet dependency /var/www/ghost/node_modules/ghost-gql requires lodash@'3.10.1' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/lodash,
npm WARN unmet dependency which is version 4.13.1
npm WARN unmet dependency /var/www/ghost/node_modules/jsonpath requires underscore@'1.7.0' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/underscore,
npm WARN unmet dependency which is version 1.8.3
npm WARN unmet dependency /var/www/ghost/node_modules/knex requires bluebird@'^2.9.24' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/bluebird,
npm WARN unmet dependency which is version 3.4.1
npm WARN unmet dependency /var/www/ghost/node_modules/knex requires lodash@'^3.7.0' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/lodash,
npm WARN unmet dependency which is version 4.13.1
npm WARN unmet dependency /var/www/ghost/node_modules/multer requires busboy@'^0.2.11' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/busboy,
npm WARN unmet dependency which is version 0.2.9
npm WARN unmet dependency /var/www/ghost/node_modules/netjet requires lru-cache@'^4.0.0' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/lru-cache,
npm WARN unmet dependency which is version 2.7.0
npm WARN unmet dependency /var/www/ghost/node_modules/pg requires semver@'^4.1.0' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/semver,
npm WARN unmet dependency which is version 5.3.0
npm WARN unmet dependency /var/www/ghost/node_modules/mocha requires escape-string-regexp@'1.0.2' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/escape-string-regexp,
npm WARN unmet dependency which is version 1.0.5
npm WARN unmet dependency /var/www/ghost/node_modules/istanbul/node_modules/js-yaml/node_modules/argparse requires lodash@'>= 3.2.0 < 4.0.0' but will load
npm WARN unmet dependency /var/www/ghost/node_modules/lodash,
npm WARN unmet dependency which is version 4.13.1

1 comment
6 Answers

So, I ran into this and figured it out. Assuming you've followed DigitalOcean's guide to upgrading Ghost, do the following in your root Ghost folder

npm cache clean
rm -rf node_modules
npm install --production

Now, if all went well you can type

npm start --production

and your blog will start up. The problem here is that it only runs while the console is open. As soon as you hit ctl-c or close the console, your blog will shut down. You need to set up Ghost to run forever. This seems to have been a part of previous installs, but I guess it's not anymore. You can use Forever, Supervisor, or Init.d to do this. Instructions are here : http://docs.ghost.org/pl/installation/deploy/

In case that link dies, to make Init.d work again, go to /etc/init.d. Create a file called ghost and copy the following into it.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          ghost
# Required-Start:    $network $syslog
# Required-Stop:     $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Ghost Blogging Platform
# Description:       Ghost: Just a blogging platform
### END INIT INFO

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Ghost"
NAME=ghost
GHOST_ROOT=/var/www/ghost
GHOST_GROUP=ghost
GHOST_USER=ghost
DAEMON=/usr/bin/node
DAEMON_ARGS="$GHOST_ROOT/index.js"
PIDFILE=/var/opt/ghost/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
export NODE_ENV=production

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# I like to know what is going on
VERBOSE = yes

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    # Set up folder structure
    mkdir -p /var/opt/ghost
    mkdir -p /var/opt/ghost/run
    chown -R ghost:ghost /var/opt/ghost
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet \
        --chuid $GHOST_USER:$GHOST_GROUP --chdir $GHOST_ROOT --background \
        --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet \
        --chuid $GHOST_USER:$GHOST_GROUP --chdir $GHOST_ROOT --background \
        --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS \
        || return 2
    # Add code here, if necessary, that waits for the process to be ready
    # to handle requests from services started subsequently which depend
    # on this one.  As a last resort, sleep for some time.
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
        --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
        --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
    #
    # If the daemon can reload its configuration without
    # restarting (for example, when it is sent a SIGHUP),
    # then implement that here.
    #
    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE \
        --name $NAME
    return 0
}

case "$1" in
start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
#reload|force-reload)
        #
        # If do_reload() is not implemented then leave this commented out
        # and leave 'force-reload' as an alias for 'restart'.
        #
        #log_daemon_msg "Reloading $DESC" "$NAME"
        #do_reload
        #log_end_msg $?
        #;;
restart|force-reload)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
        0|1)
                do_start
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
        *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
*)
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
esac

:

Save the document. Now run the following commands

update-rc.d ghost defaults 
update-rc.d ghost enable

Now when you type

service ghost start

your blog should run normally.

Hope this helps.

  • I got it working with npm start --production but you're right, shutting down with ctl+c kills the server. I couldn't get forever or init.d to work though...

  • @scottrolsen Thanks for the help as I have been having the same issues as @annoysparrot . However, I tried to run the first piece code you mentioned within the root Ghost folder and after npm-install --production command ran I received the following errors:

    npm WARN cannot run in wd ghost@0.9.0 node core/server/utils/npm/preinstall.js         (wd=/var/www/ghost)
     npm WARN peerDependencies The peer dependency knex@>=0.6.10 <0.12.0 included from bookshelf will no
    npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
    npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
    jsonpath@0.2.6 postinstall /var/www/ghost/node_modules/jsonpath
    node lib/aesprim.js > generated/aesprim-browser.js
    

    any thoughts on how to fix this? Thank you.

    • Ignore the error messages for a minute. What happens when you run

      npm start --production
      

      Those are all just warnings and not show stoppers, so it should run.

      • @scottrolsen

        This is what I get:

        ghost@0.9.0 start /var/www/ghost
        node index
        
        WARNING: Ghost is attempting to use a direct method to send email. 
        It is recommended that you explicitly configure an email service.
        Help and documentation can be found at http://support.ghost.org/mail.
        
        
        ERROR: Cannot find module 'core-js/library/fn/object/define-property' 
        
         Error: Cannot find module 'core-js/library/fn/object/define-property'
            at Function.Module._resolveFilename (module.js:325:15)
            at Function.Module._load (module.js:276:25)
            at Module.require (module.js:353:17)
            at require (internal/module.js:12:17)
            at Object.<anonymous> (/var/www/ghost/node_modules/bookshelf/node_modules/
        babel-runtime/core-js/object/define-property.js:1:93)
            at Module._compile (module.js:409:26)
            at Object.Module._extensions..js (module.js:416:10)
            at Module.load (module.js:343:32)
            at Function.Module._load (module.js:300:12)
            at Module.require (module.js:353:17)
            at require (internal/module.js:12:17)
            at Object.<anonymous> (/var/www/ghost/node_modules/bookshelf/node_modules/
        babel-runtime/helpers/createClass.js:5:23)
            at Module._compile (module.js:409:26)
            at Object.Module._extensions..js (module.js:416:10)
            at Module.load (module.js:343:32)
            at Function.Module._load (module.js:300:12) 
        
        
        • I know you did this once, but try

          sudo apt-get update
          sudo apt-get upgrade
          npm cache clean
          rm -rf node_modules
          rm -rf core
          

          This should wipe core as well and insure you're using the latest tools. Now recopy core back in from the Ghost package you downloaded. Now...

          npm install --production
          

          How's that work for you?

          • When you say 'recopy core back in from the Ghost package' do you mean from the latest update that I downloaded in zip format?

          • Yes @nfinleymusic , you need copy again the core files from de zip. Then, run again

            npm start --production
            

            And you'll have an error, about sqlite3, just run the command that appear

            npm install sqlite3 -save
            npm start --production
            

            And that is! Work for me :)

          • @scottrolsen do I need to be running my terminal as the root user or the ghost user?

        • @nfinleymusic you need to run as root.

hola amigos yo hice un tutorial donde ustedes pueden ver paso a paso como solucionar este problema pueden entrar a este blog que está también creado en ghost Tutorial

The script on this page made it really easy for me and even fixed what I had messed up trying to do it manually.
https://www.ghostforbeginners.com/how-to-update-ghost/

Have another answer? Share your knowledge.