Running ghost with pm2 and nginx
For the installation of the node.js, ghost on a linux operating system you can look at this post.
Assumptions:
Linux operating system is installed and running, node.js and ghost is running on your system. You have full root access.
Navigate to your ghost installation. I will assume that you have followed the post above to install it and the ghost installation is at /var/www/ghost
. Install the pm2 using the following code.
#npm install -g pm2@latest
After the installation is complete you have to create the init script by running the following:
#pm2 startup debian
for centos replace the debian with centos.
Edit the init script located at /etc/init.d/pm2-init.sh
and replace the path to your ghost install. You will have to have sudo installed on your system. You will also have to search for sudo into the init script and remove the -i option (it is only in one place). Your init script would look like this:
#!/bin/bash
# chkconfig: 2345 98 02
#
# description: PM2 next gen process manager for Node.js
# processname: pm2
#
### BEGIN INIT INFO
# Provides: pm2
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PM2 init script
# Description: PM2 is the next gen process manager for Node.js
### END INIT INFO
NAME=pm2
PM2=/usr/local/lib/node_modules/pm2/bin/pm2
USER=root
export PATH=$PATH:/usr/local/bin
export HOME="/var/www/"
export NODE_ENV="production"
super() {
sudo -E -u $USER PATH=$PATH $*
}
start() {
echo "Starting $NAME"
super $PM2 resurrect
}
stop() {
super $PM2 dump
super $PM2 delete all
super $PM2 kill
}
restart() {
echo "Restarting $NAME"
stop
start
}
reload() {
echo "Reloading $NAME"
super $PM2 reload all
}
status() {
echo "Status for $NAME:"
super $PM2 list
RETVAL=$?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
reload)
reload
;;
*)
echo "Usage: {start|stop|status|restart|reload}"
exit 1
;;
esac
exit $RETVAL
Now start your ghost application using pm2:
#cd /var/www/ghost
#pm2 start index.js --name Ghost
Add the init script to the boot.
#chkconfig pm2-init.sh on
After the pm2 would start it would listen on port 2368 and the nginx will proxy the requests to the running ghost.