Running the Ghost Node.js Blogging Platform as an Upstart Service

Ghost is a relatively new and free open source blogging platform for Node.js. If you're looking for a blogging platform and you are a fan of Node.js, or just tired of PHP, I highly recommend it. I actually work with Ghost almost on a daily basis.

A particularity of Ghost is that, unless specified otherwise, it will run in development mode. Therefore, if you are ready to deploy on your production server, you'll have to run Ghost like this:

$ npm start --production

or

$ NODE_ENV=production node index.js

A few days back I wrote the tutorial Running Your Node.js App as an Upstart Service, which you can follow if you want to run Ghost as an Upstart service on your server. However, specifically for Ghost, you'll have to make a few modifications to the original Upstart script, so as to set the NODE_ENV environment variable.

Edit /etc/init/ghost.conf:

description "ghost"
author "MY_NAME"

env NODE=/usr/bin/nodejs
env SCRIPT=/home/www/ghost/index.js
env LOG_FILE=/home/www/ghost/log/stdout.log
env PID_FILE=/home/www/run/ghost.pid
env USER=www

start on filesystem or runlevel [2345]
stop on shutdown

respawn
respawn limit 10 5

script
exec start-stop-daemon --start --chuid $USER --make-pidfile --pidfile $PID_FILE --exec /usr/bin/env NODE_ENV=production $NODE $SCRIPT >> $LOG_FILE 2>&1
end script

pre-start script
touch $LOG_FILE
chown root:$USER $LOG_FILE
chmod 770 $LOG_FILE
echo "[`date`] ghost starting" >> $LOG_FILE
end script

pre-stop script
rm -f $PID_FILE
echo "[`date`] ghost stopping" >> $LOG_FILE
end script

That's it! Happy blogging!