How To Start Ghost as a Service on Linux

On Ubuntu and CentOS, you can use the init service to start Ghost on boot up, and keep it running if Ghost crashes. All you have to do is place a ghost.conf file in /etc/init/ directory.

In the init script we will be start Ghost with a non root ghost user. Before you implement starting Ghost you will want to make sure you have Ghost installed and a ghost user ready to run Ghost. See our Ubuntu or CentOS instructions for installing Ghost and setting up the ghost user. In addition to having the ghost user you will need to have either Nginx or Apache setup so that the ghost user can start Ghost on port 2368 instead of the priviledge port 80.

 sudo vi /etc/init/ghost.conf
If you are using Ubuntu place the following in your ghost.conf file:
 description "Ghost Blog"
 author "Your Name"
 # Start the service after everything loaded
 start on (local-filesystems and net-device-up IFACE=eth0)
 stop on shutdown
 # Automatically restart service
 respawn limit 99 5
     # Navigate to your app directory
     cd /var/www/ghost/

     # Run the script with Node.js and output to a log
     su ghost -c "/usr/local/bin/npm start --production" 2>&1 >> /var/log/ghost.log
 end script
If you are using CentOS place the following in your /etc/init.d/ghost file:

# ghost - this script starts the ghost blogging package
# chkconfig:   - 95 20
# description: ghost is a blogging platform built using javascript \
#              and running on nodejs

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/usr/local/bin/pm2 start index.js --run-as-user ghost --name ghost"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog


start() {
    #[ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    # if not running, start it up here, usually something like "daemon $exec"
    export NODE_ENV=production
    cd /var/www/ghost/
    daemon --user=ghost $exec
    [ $retval -eq 0 ] && touch $lockfile
    return $retval

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    pid=`ps -u $prog -fw | grep $prog | grep -v " grep " | awk '{print $2}'`
    kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval

restart() {

my_status() {
        local base pid lock_file=


        # get pid
        pid=`ps -u $prog -fw | grep $prog | grep -v " grep " | awk '{print $2}'`

        if [ -z "${lock_file}" ]; then
        # See if we have no PID and /var/lock/subsys/${lock_file} exists
        if [[ -z "$pid" && -f /var/lock/subsys/${lock_file} ]]; then
                echo $"${base} dead but subsys locked"
                return 2

        if [ -z "$pid" ]; then
                echo $"${base} is stopped"
                return 3

        if [ -n "$pid" ]; then
                echo $"${base} (pid $pid) is running..."
                return 0


rh_status() {
    # run checks to determine if the service is running or use generic status
    my_status $prog

rh_status_q() {
    rh_status >/dev/null 2>&1

case "$1" in
        rh_status_q && exit 0
        rh_status_q || exit 0
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 2
exit $?
With the script in place run the following command to have the script run at startup:
chkconfig --add ghost
chkconfig --levels 2345 ghost on

Now on Ububuntu with that file in place, you can use the following actions (replace ghost with the name of your .conf file):

 sudo ghost start
 sudo ghost status
 sudo ghost stop
 sudo ghost restart

And on CentOS you can use the following commands:

service ghost start
service ghost stop
service ghost restart
service ghost status

If you restart your server or Ghost crashes, init will spin up another instance of Ghost for you.