Select Page


Node.js is an open-source JavaScript runtime setting for construction server-side and networking packages. The platform runs on Linux, macOS, FreeBSD, and Home windows. Even though you’ll run Node.js packages on the command line, this instructional will center of attention on working them as a provider. This implies that they’re going to restart on reboot or failure and are secure to be used in a manufacturing setting.

On this instructional, you’ll arrange a production-ready Node.js setting on a unmarried Ubuntu 18.04 server. This server will run a Node.js utility controlled via PM2, and supply customers with protected get admission to to the appliance via an Nginx opposite proxy. The Nginx server will be offering HTTPS the usage of a unfastened certificates equipped via Let’s Encrypt.

Must haves

This information assumes that you’ve the next:

Whilst you’ve finished the must haves, you’ll have a server serving the default Nginx placeholder web page at

Step 1 — Putting in Node.js

Let’s start via putting in the newest LTS liberate of Node.js, the usage of the NodeSource bundle archives.

First, set up the NodeSource PPA in an effort to get get admission to to its contents. You should definitely’re in your house listing, and use curl to retrieve the set up script for the Node.js 8.x archives:

  • cd ~
  • curl -sL -o

You’ll be able to check up on the contents of this script with nano or your most well-liked textual content editor:

If you find yourself achieved analyzing the script, run it underneath sudo:

  • sudo bash

The PPA might be added for your configuration and your native bundle cache might be up to date mechanically. After working the setup script from Nodesource, you’ll set up the Node.js bundle:

To test which model of Node.js you might have put in after those preliminary steps, kind:



Be aware: When putting in from the NodeSource PPA, the Node.js executable is known as nodejs, reasonably than node.

The nodejs bundle incorporates the nodejs binary in addition to npm, a bundle supervisor for Node modules, so you do not want to set up npm one after the other.

npm makes use of a configuration report in your house listing to stay monitor of updates. It is going to be created the primary time you run npm. Execute this command to make sure that npm is put in and to create the configuration report:



To ensure that some npm programs to paintings (those who require compiling code from supply, for instance), it is very important set up the build-essential bundle:

  • sudo apt set up build-essential

You presently have the essential gear to paintings with npm programs that require compiling code from supply.

With the Node.js runtime put in, let’s transfer directly to writing a Node.js utility.

Step 2 — Making a Node.js Utility

Let’s write a Hi Global utility that returns “Hello World” to any HTTP requests. This pattern utility will let you get Node.js arrange. You’ll be able to substitute it with your individual utility — simply just remember to adjust your utility to pay attention at the suitable IP addresses and ports.

First, let’s create a pattern utility referred to as hi.js:

Insert the next code into the report:


const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content material-Sort', 'textual content/undeniable');
  res.finish('Hi Global!n');
}); attention(port, hostname, () => {
  console.log(`Server working at http://${hostname}:${port}/`);

Save the report and go out the editor.

This Node.js utility listens at the specified cope with (localhost) and port (3000), and returns “Hello World!” with a 200 HTTP good fortune code. Since we are listening on localhost, far flung purchasers will not be able to hook up with our utility.

To check your utility, kind:

You’re going to see the next output:


Server working at http://localhost:3000/

Be aware: Operating a Node.js utility on this method will block further instructions till the appliance is killed via urgent CTRL+C.

To check the appliance, open some other terminal consultation to your server, and hook up with localhost with curl:

  • curl http://localhost:3000

In the event you see the next output, the appliance is operating correctly and listening on the proper cope with and port:


Hi Global!

If you don’t see the predicted output, be sure that your Node.js utility is working and configured to pay attention on the correct cope with and port.

As soon as you might be positive it is running, kill the appliance (if you have not already) via urgent CTRL+C.

Step 3 — Putting in PM2

Subsequent let’s set up PM2, a procedure supervisor for Node.js packages. PM2 makes it imaginable to daemonize packages so that they’re going to run within the background as a provider.

Use npm to put in the newest model of PM2 to your server:

  • sudo npm set up pm2@newest -g

The -g choice tells npm to put in the module globally, in order that it is obtainable system-wide.

Let’s first use the pm2 get started command to run your utility, hi.js, within the background:

This additionally provides your utility to PM2’s procedure record, which is outputted each and every time you get started an utility:


[PM2] Spawning PM2 daemon with pm2_home=/house/sammy/.pm2 [PM2] PM2 Effectively daemonized [PM2] Beginning /house/sammy/hi.js in fork_mode (1 example) [PM2] Executed. ┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬───────┬──────────┐ │ App title │ identification │ mode │ pid │ standing │ restart │ uptime │ cpu │ mem │ person │ observing │ ├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼───────┼──────────┤ │ hi │ 0 │ fork │ 1338 │ on-line │ 0 │ 0s │ 0% │ 23.Zero MB │ sammy │ disabled │ └──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴───────┴──────────┘ Use `pm2 display ` to get extra information about an app

As you’ll see, PM2 mechanically assigns an App title (according to the filename, with out the .js extension) and a PM2 identification. PM2 additionally maintains different knowledge, such because the PID of the method, its present standing, and reminiscence utilization.

Packages which might be working underneath PM2 might be restarted mechanically if the appliance crashes or is killed, however we will be able to take an extra step to get the appliance to release on components startup the usage of the startup subcommand. This subcommand generates and configures a startup script to release PM2 and its controlled processes on server boots:

The ultimate line of the ensuing output will come with a command to run with superuser privileges in an effort to set PM2 to start out on boot:


[PM2] Init Device discovered: systemd [PM2] To setup the Startup Script, replica/paste the next command: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /house/sammy

Run the command from the output, along with your username rather than sammy:

  • sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /house/sammy

As an extra step, we will be able to save the PM2 procedure record and corresponding environments:

You’ve now created a systemd unit that runs pm2 in your person on boot. This pm2 example, in flip, runs hi.js. You’ll be able to take a look at the standing of the systemd unit with systemctl:

  • systemctl standing pm2-sammy

For an in depth evaluation of systemd, see Systemd Essentials: Working with Services, Units, and the Journal.

Along with the ones now we have lined, PM2 supplies many subcommands that mean you can organize or glance up details about your packages.

Forestall an utility with this command (specify the PM2 App title or identification):

Restart an utility:

  • pm2 restart app_name_or_id

Checklist the packages these days controlled via PM2:

Get details about a selected utility the usage of its App title:

The PM2 procedure track may also be pulled up with the monit subcommand. This presentations the appliance standing, CPU, and reminiscence utilization:

Be aware that working pm2 with none arguments may also show a assist web page with instance utilization.

Now that your Node.js utility is working and controlled via PM2, let’s arrange the opposite proxy.

Step 4 — Atmosphere Up Nginx as a Opposite Proxy Server

Your utility is working and listening on localhost, however you want to arrange some way in your customers to get admission to it. We will be able to arrange the Nginx internet server as a opposite proxy for this objective.

Within the prerequisite instructional, you put up your Nginx configuration within the /and many others/nginx/sites-available/ report. Open this report for enhancing:

  • sudo nano /and many others/nginx/sites-available/

Throughout the server block, you’ll have an present location / block. Exchange the contents of that block with the next configuration. In case your utility is about to pay attention on a unique port, replace the highlighted portion to the proper port quantity:

/and many others/nginx/sites-available/

server {
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Improve $http_upgrade;
        proxy_set_header Connection 'improve';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

This configures the server to answer requests at its root. Assuming our server is obtainable at, getting access to by the use of a internet browser would ship the request to hi.js, listening on port 3000 at localhost.

You’ll be able to upload further location blocks to the similar server block to supply get admission to to different packages at the similar server. For instance, should you have been additionally working some other Node.js utility on port 3001, it is advisable upload this location block to permit get admission to to it by the use of

/and many others/nginx/sites-available/ — Non-compulsory

server {
    location /app2 {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Improve $http_upgrade;
        proxy_set_header Connection 'improve';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

As soon as you’re achieved including the site blocks in your packages, save the report and go out your editor.

You should definitely did not introduce any syntax mistakes via typing:

Restart Nginx:

  • sudo systemctl restart nginx

Assuming that your Node.js utility is working, and your utility and Nginx configurations are right kind, you must now be capable to get admission to your utility by the use of the Nginx opposite proxy. Test it out via getting access to your server’s URL (its public IP cope with or area title).


Congratulations! You presently have your Node.js utility working at the back of an Nginx opposite proxy on an Ubuntu 18.04 server. This opposite proxy setup is versatile sufficient to supply your customers get admission to to different packages or static internet content material that you need to percentage.