To prevent your Ghost blog from stopping, you have to run Ghost as a service. If the instance that is running Ghost ever restarts or shuts down for some reason, you need a way to make sure that Ghost starts up again.
I decided to use
systemd since many Linux distributions have transitioned to it as a default "init" system. We will create a new Systemd service for Ghost and then enable and start it.
We need to create a
ghost.service service unit. This Unit file will include how to start or stop the service, and under which circumstances it should automatically restart the service.
The system's copy of unit files are generally kept in the
/lib/systemd/system directory. This is where we will create our new
ghost.system unit file.
sudo vim /etc/systemd/system/ghost.service
Using vim or whichever text editor you prefer, enter the following content and save it:
[Unit] Description=Ghost Blog After=network.target [Service] Type=simple WorkingDirectory=/var/www/ghost ExecStart=/usr/bin/npm start --production ExecStop=/usr/bin/npm stop --production Restart=always SyslogIdentifier=Ghost Blog [Install] WantedBy=multi-user.target
After=network.targetline makes sure that our service will not run until after the network is up.
ExecStart=/usr/bin/npm start --productionspecifies the full path and the arguments of the command to be executed to start the process.
Enabling & Starting the Ghost service
Now that we've created our
ghost.service unit file, it is time to enable the unit.
sudo systemctl enable ghost.service
You can run the following command to check if you enabled your Ghost service correctly:
systemctl list-unit-files | grep -i ghost
This command should output the name of the unit file and the status (which should be enabled).
Although we have enabled the service we still haven't started it. You can check the status with the following command:
systemctl status ghost.service
You'll see that
ghost.service is loaded but it is inactive / dead. You are now ready start your Ghost service. Note: If Ghost is currently running from when you manually started it using
npm, go ahead and stop it.
Finally, run the following command:
sudo systemctl start ghost.service
Give it about a minute or so to finish starting your service. Refresh your blog on a browser to find a Ghost blog that will run forever.
As I was setting up my Ghost service for the first time ran into a few issues. Always check the status of your service by running the following command:
systemctl status ghost.service
Systemd has a component called
journald which collects and manages jounal entries aka logs. When my service failed to start I found it extremly useful to check the logs created by my service.
journalctl -u ghost.service
If you make a modification to your
.service file while it is already enabled or started you need to run the following command to update it properly:
sudo systemctl daemon-reload
Start a service:
systemctl start nameOfService.service
Stop a service:
systemctl stop nameOfService.service
Restart a service:
systemctl restart nameOfService.service
Note: This is Part 2 of my 3 part series on getting your Ghost blog up and running. In Part 1 I document how to get the Ghost blog up and running on an EC2 instance. In Part 3 I document how to enable HTTPS for your blog to make it secure!