What is this article about?

This article talks about two different ways to start ghost and make it persistent on ubuntu using startup scripts. Ubuntu being a linux distribution belonging to the Debian family has gone a through a recent change between distributions 14.04 and 16.04 in terms of how startup scripts are implemented.

What to understand

In recent years, Linux distributions have increasingly transitioned from other init systems to systemd. The systemd suite of tools provides a fast and flexible init model for managing an entire machine from boot onwards.

Ghost used to be started at system boot with Ubuntus Upstart init system (14.04 and earlier)
Now Ghost is started using Systemd (15.04 and later)

How to start Ghost & have it start itself after a server reboot

Using the Startup method (Ubuntu 14.04 and earlier)

Starting Ghost - The command npm start --production only starts ghost from command line. It doesn't run Ghost persistently nor restart it after reboots. Follow these instructions to fix that:

NOTE: you have to make sure you run everything as the root user

a) cd /etc/init
b) nano NAME.conf (where NAME is the name of your ghost blog)
I use the prefix gh- for all my ghost blogs e.g. gh-heap.conf
c) paste below into the file:

# Ghost Blog Startup script - ghost.skillshub.info
start on startup
script
cd /PATH/TO/YOUR/GHOST/BLOG
npm start --production
end script

d) now start ghost by using this command: service NAME start

Here are some PATH EXAMPLES - substitute /PATH/TO/YOUR/GHOST/BLOG with e.g. "/srv/users/serverpilot/apps/APPNAME/public/" or "/var/www/heap.skillshub.info/ghost" or wherever your Ghost files are located on the server

Using the Systemd method (Ubuntu 15.04 and later)

Starting Ghost - Is done slightly differently with Systemd. You need to create a file NAME.service in /etc/systemd/system/ (where NAME is the name of your ghost blog) I use the prefix gh- for all my ghost blogs e.g. gh-heap.service.)

NOTE: Make sure you run everything as the root user and then follow below instructions.

a) cd /etc/systemd/system/   
b) nano NAME.service 
c) paste below into the file:

# Place in /etc/systemd/system/NAME.service
[Unit]
Description=Ghost Blog Startup script - ghost.skillshub.info 
After=network.target

[Service]
Type=simple  
PIDFile=/run/NAME.pid  

# WorkingDirectory is the directory you installed Ghost to
WorkingDirectory=/PATH/TO/YOUR/GHOST/BLOG
User=ghost  
Group=ghost  
ExecStart=/usr/bin/npm start --production  
ExecStop=/usr/bin/npm stop /PATH/TO/YOUR/GHOST/BLOG  
StandardOutput=syslog  
StandardError=syslog

[Install]
WantedBy=multi-user.target  

d) now start ghost by using this command: service NAME start

PATH EXAMPLES - substitute /PATH/TO/YOUR/GHOST/BLOG with e.g. "/srv/users/serverpilot/apps/APPNAME/public/" or "/var/www/heap.skillshub.info/ghost" or wherever your Ghost files are located on the server

To make it start after a reboot we need to register the service you created so that it will start whenever the server gets rebooted.
Use this command: systemctl enable NAME.service

NOTE: You can always check the status of the service by running the following command: service NAME status e.g. service gh-heap status

THE GOTCHA'S

User & Group - for standard installs of ghost, the user and group is usually 'ghost' but if you are using Serverpilot , the user and group is probably 'serverpilot' - The NAME.service file needs the user and group that owns your ghost directory. Use these commands to quickly find out:

cd /PATH/TO/YOUR/GHOST/BLOG
ls -lah

According to this image the permissions of the folder containing all my ghost files belong to 'serverpilot'

But if you would need to change the permissions, just use the chown command in the folder that contains all your ghost files. Example: chown -R serverpilot:serverpilot *

Reference Links:
https://chrisebert.net/installing-ghost-and-nginx-on-ubuntu-server-16-04-lts/#step10
https://wiki.debian.org/systemd
Systemd Essentials: Working with Services, Units, and the Journal
http://docs.ghost.org/pl/installation/deploy/
https://www.howtoforge.com/tutorial/how-to-install-ghost-blog-on-ubuntu/