Development notes

Thoughts, notes and ideas about development

How to start Ghost as a service on Ubuntu 16.04

2016-11-29 2 min read Blogging Linux Alexey Bogdanov

Official Ghost documentation Deploying Ghost describes how to start Ghost as a service using script for /etc/init.d. This approach work well only for Ubuntu prior version 16.04. In this tutorial I’ll describe how to start Ghost as a service using new systemd service manager.

Requirements

  1. Ghost should be already installed. *How to install Ghost described here: How to install Ghost on Ubuntu 16.04.
  2. Ghost is installed in /var/www/ghost

Create systemd script

Open terminal as root (or sudo user).

Create file etc/systemd/system/ghost.service with the following content:

[Service]
ExecStart=/usr/bin/node /var/www/ghost/index.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=ghost
User=ghost
Group=ghost
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Start Ghost as a service

Now we can start Ghost as a service:

systemctl start ghost

Verify Ghost service status

To verify the status of the service we can use the following command:

systemctl status ghost

If ghost service started successfully the output should look like:

root@ghost:~# systemctl status ghost
● ghost.service - LSB: Ghost Blogging Platform
   Loaded: loaded (/etc/init.d/ghost; bad; vendor preset: enabled)
   Active: active (running) since Tue 2016-11-29 19:16:09 MSK; 5s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 13629 ExecStart=/etc/init.d/ghost start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/ghost.service
           └─13639 /usr/bin/node /var/www/ghost/index.js

If any error occurs the output will look into the following way:

ghost.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

Nov 29 19:12:45 ghost systemd[13480]: ghost.service: Failed at step USER spawning /usr/bin/npm: No such process
Nov 29 19:12:45 ghost systemd[1]: ghost.service: Main process exited, code=exited, status=217/USER
Nov 29 19:12:45 ghost systemd[13482]: ghost.service: Failed at step USER spawning /usr/bin/npm: No such process
Nov 29 19:12:45 ghost systemd[1]: ghost.service: Control process exited, code=exited status=217
Nov 29 19:12:45 ghost systemd[1]: ghost.service: Unit entered failed state.
Nov 29 19:12:45 ghost systemd[1]: ghost.service: Failed with result 'exit-code'.
Nov 29 19:12:45 ghost systemd[1]: ghost.service: Service hold-off time over, scheduling restart.
Nov 29 19:12:45 ghost systemd[1]: Stopped ghost.
Nov 29 19:12:45 ghost systemd[1]: ghost.service: Start request repeated too quickly.
Nov 29 19:12:45 ghost systemd[1]: Failed to start ghost.

In that case the following file should be removed:

rm /lib/systemd/system/ghost.service

Enable Ghost service to start automatically

To enable a ghost service to be started on bootup we can into the following way:

systemctl enable ghost

The output of this command should look like:

ghost.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install enable ghost
comments powered by Disqus