Managing your Ghost blog with pm2
To start the Ghost application you can of course use
npm run --production in your Ghost installation folder. But as soon as you end your shell session, the Ghost process will end as well. When you run Ghost in a production environment you'll want the Ghost process to run forever and restart it only when doing maintenance. This is where pm2 comes into play: it's a process manager for Node.js applications that does exactly those things. In this post I'll explain how to install it and use it to manage the Ghost process.
I tested the following steps only on Ubuntu 16.04, but they should be similar on other flavours of Linux. I'll assume you already have Ghost installed.
⚠️ Please note that this post was written for Ghost versions < 1.0.0. If you use Ghost 1.0.0 or higher, please use
Install and configure pm2
The first thing you need to do is install pm2 globally using
npm install pm2 -g
Next, we'll tell pm2 where it can find Ghost's
index.js so it can start the process.
NODE_ENV=production pm2 start /path/to/ghost/index.js --name "ghost"
Note that the leading
NODE_ENV=production is important. This makes sure pm2 starts Ghost using the production config. I've named the process
ghost in this case, but you can change it to whatever you like. Verify that Ghost is now running using
This will give you a list with all the running pm2 processes and you should now see that Ghost is among them (or the only one).
Because we want to keep the Ghost process alive and have it start automatically at a server reboot, we're going to save the process list. Before we can do this, we'll have to generate the startup scripts. You can do this by running the following command:
Then, you'll have to save the current process list using
And that's it, we're all set! Pm2 will now keep Ghost process alive and automatically restore it when rebooting the system. You can also use the following commands to stop, restart and reload the Ghost process with ease:
pm2 stop ghost # stops ghost pm2 restart ghost # restarts ghost pm2 reload ghost # reloads ghost without downtime