When installing Ghost manually on a VPS or your home machine, a problem most people run into is how to keep Ghost running after their terminal session has ended. There are multiple ways to do this, the most popular way currently being forever. pm2 is a great alternative to forever. Very easy to setup and use. Here are the steps to installing pm2 and using it with Ghost:

Note: These commands should be run from a non root user that is dedicated to running Ghost.

  1. cd /path/to/ghost/folder
  2. sudo npm install -g pm2
  3. echo "export NODE_ENV=production" >> ~/.profile
  4. source ~/.profile
  5. pm2 kill
  6. pm2 start index.js --name ghost

Once you have Ghost up and running with pm2 some other useful pm2 commands will be:

  • pm2 status
  • pm2 stop <process ID>
  • pm2 monit
  • pm2 logs

As you can see in the screen shot below the pm2 status command will allow you to see some useful status on your Ghost instance. Three useful things to note from the command will be the id, Uptime, and err logs columns. These will be very valuable.

allaboutghost - Ghost - pm2 status command

The pm2 logs command will give you live output from your Ghost blog.

allaboutghost - Ghost - pm2 log command

And the pm2 monit command will give you life status on the memory footprint of Ghost.

allaboutghost - Ghost - pm2 monit command

Another great feature of pm2 is the ability to create an init script which will deal with the situation of your server restarting. The popular forever process of keeping Ghost up and running does not have any built in way of dealing with a server restart, so I see this is a major advantage that pm2 has over forever. The init script will start pm2, which will start Ghost when your server starts up and then will continue to monitor it. To take advantage of this feature all you have to do is running the following command:

 pm2 startup

And then you should see pm2 output something similar to this:

 Adding system startup for /etc/init.d/pm2-init.sh ...
 /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
 /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
 /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
 /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
 /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
 /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
 /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh

If you want more information on pm2, here is a link to their GitHub page.

22 thoughts on “Keep Ghost Running with pm2

  1. Pingback: How To Update Ghost To Version 0.4

  2. Is your ghost / pm2 instance running as root, if you start it like this?

    I start it with “pm2 -u username start….” to keep ghost running as user.

    Reply
  3. Pingback: How To Install Ghost on CentOS

  4. Pingback: How To Setup Bluehost VPS for Ghost Blog | How To Install Ghost

  5. I tried this on a test box as I am currently using forever. I installed two blogs and made sure they were working on 2368 and 2370 then installed pm2 as per the instructions above. PM2 shows both blogs are running, using memory and gives the impression all is fine.

    However :)

    Neither blog is responding. The only difference from your screenshot is that the logs location is greyed out.

    Any ideas? I think it’s something simple but just can’t put my finger on it.

    Thanks

    Lee

    Reply
  6. Pingback: How To Start Ghost as a Service on Linux

  7. Hi,
    I followed all the steps, and it works.
    But it’s running on development insted of on production…
    However the .profile exists

    cat /home/ghost/.profile
    export NODE_ENV=production

    Reply
    • Hi, i seemed to meet the same problem with yours.
      try this ?
      ” NODE_ENV=production pm2 –run-as-user ghost start index.js –name ghost ”

      for me it’s ok, but if do “pm2 logs”, it still displays
      “Ghost is running in development…”, I doubt may be the pm2′s bug?

      Reply
    • pm2 can handle many different instances of Ghost. Once you have pm2 installed, just cd to each Ghost directory, and run the pm2 start command and it will work. You can then use ‘pm2 list’ to see a list of all of your running instances.

      Reply
  8. I run step 3: source ~/.profile

    got error: -bash: /root/.profile: No such file or directory

    Instead, I ran source ~/.bash_profile and get the correct results.

    Command seems to have some changes.

    Run pm2 startup
    get error: missing required argument `platform'

    The document says
    pm2 startup [ubuntu|centos|gentoo|systemd]

    Thank you very much this tutorial, it is helpful!

    Reply
  9. Eesh, I’m having some versions issues here. I’m trying to run ghost 0.5.1 which I can’t get to launch with node version > 0.10.0 and I can’t get pm2 to run without version 0.11.10. I set nvm use v0.10.0 and I can manually launch Ghost using npm start –production no problem, but when I try to use pm2 after setting node to use 0.10.0 it can’t even find the the command, set nvm to use v0.11.10 and all of a sudden pm2 is back. What am I missing?
    Ubuntu 14.04
    Thanks for any help. I’d love to use pm2 here.

    Reply
  10. Pingback: How to Update to Ghost 0.5.2

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>