Make your NodeJS app run as a service in Linux

Revision as of 21:10, 28 March 2020 by Ardaut (talk | changes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is an example where you can make your NodeJS app (also MEAN or MERN stack app) run as a service in your Linux server. Here we create the app "My NodeJS App".

1. Firtly, add this to the first line of your main nodeJS app file. Here it is the file server.js:

#!/usr/bin/env node

2. Add the service in the folder /etc/systemd/system/:

sudo vim mynodejsapp.service
Description=My NodeJS App



If you have Red Hat (RHEL) or Fedora as an OS, then you have to use "nobody" as a group. "nogroup" is used in Ubuntu and Debian.

3. Set executable permission

chmod +x /var/www/apps/mynodejsapp/server.js

4. To start the app automatically on system boot:

sudo systemctl enable mynodejsapp

5. Start the app

sudo systemctl start mynodejsapp

6. Check the status of your app, restart, stop, etc. with the command "systemctl":

sudo systemctl status mynodejsapp
sudo systemctl stop mynodejsapp