Difference between revisions of "Make your NodeJS app run as a service in Linux"

(Created page with "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,...")
 
Line 1: Line 1:
 
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".  
 
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:
+
# Firtly, add this to the first line of your main nodeJS app file. Here it is the file server.js:
 
<pre>#!/usr/bin/env node</pre>
 
<pre>#!/usr/bin/env node</pre>
  
2. Add the service in the folder /etc/systemd/system/:
+
# Add the service in the folder /etc/systemd/system/:
 
<pre>sudo vim mynodejsapp.service</pre>
 
<pre>sudo vim mynodejsapp.service</pre>
 
<pre>[Unit]
 
<pre>[Unit]
Line 22: Line 22:
  
 
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.  
 
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.  
 +
# Set execute permission
  
3. To start the app automatically on system boot:
+
<pre>chmod +x /var/www/apps/mynodejsapp/server.js</pre>
 +
 
 +
# To start the app automatically on system boot:
 
<pre>sudo systemctl enable mynodejsapp</pre>
 
<pre>sudo systemctl enable mynodejsapp</pre>
  
4. Start the app
+
# Start the app
 
<pre>sudo systemctl start mynodejsapp</pre>
 
<pre>sudo systemctl start mynodejsapp</pre>
  
5. Check the status of your app, restart, stop, etc. with the command "systemctl":
+
# Check the status of your app, restart, stop, etc. with the command "systemctl":
 
<pre>sudo systemctl status mynodejsapp</pre>
 
<pre>sudo systemctl status mynodejsapp</pre>
 
<pre>sudo systemctl stop mynodejsapp</pre>
 
<pre>sudo systemctl stop mynodejsapp</pre>

Revision as of 18:04, 28 March 2020

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
  1. Add the service in the folder /etc/systemd/system/:
sudo vim mynodejsapp.service
[Unit]
Description=My NodeJS App

[Service]
ExecStart=/var/www/apps/mynodejsapp/server.js
Restart=always
User=nobody
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/apps/mynodejsapp

[Install]
WantedBy=multi-user.target

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.

  1. Set execute permission
chmod +x /var/www/apps/mynodejsapp/server.js
  1. To start the app automatically on system boot:
sudo systemctl enable mynodejsapp
  1. Start the app
sudo systemctl start mynodejsapp
  1. Check the status of your app, restart, stop, etc. with the command "systemctl":
sudo systemctl status mynodejsapp
sudo systemctl stop mynodejsapp