23.3 C
Senior Writer and partner

How to set up your NodeJS application for production

Hello friends. It comes to this point in life, you are a NodeJS developer and you want to deploy your application in production, how to do it? Well in this post you will learn how to set up a NodeJS application for production on an Ubuntu 20.04 server.

We have talked a lot about NodeJS in this blog because it is a new technology that allows us to create many complex web applications quickly.

NodeJS is a technology that allows running Javascript on the server-side using Google’s V8 engine. It is a real revolution in the way web applications are developed because it reduces the asymmetry of client and server performance.

Many important forms such as Linkedin or eBay were created using this technology. This shows us the power of NodeJS.

- Advertisement -

An advantage of NodeJS is that its syntax is similar to that of Javascript and therefore its learning curve decreases a lot.

Set up your NodeJS application for production on Ubuntu 20.04

The first thing we have to do is to install NodeJS on the server where we want our application to be. To do this, we will use the 14.x branch, which is one of the most modern ones.

To do this, connect via SSH to your server and update the distribution

sudo apt update
sudo apt upgrade

Now, add the NodeJS repository for the 14.x branch by downloading and executing the official script

curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh

This will start the whole process and at the end, you will be able to check the version of NodeJS installed by running this command:

node -v

It is important that we also check the installed version of npm.

npm -v

Now we have the software installed, let’s see an example of how to deploy our application.

To do this I will create a simple NodeJS file called exmaple.js that displays a hello world.

nano example.js
const http = require('http');

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

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World. Welcome to Osradar\n');

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);

Of course, in your case, you will have several files and all the necessary structures.

To run the application just use this command

node example.js

And you will get an output screen similar to this one:

Server running at http://localhost:3000/

To stop the execution of the program, just press CTRL + C.

Running a production NodeJS application in this way is not recommended. The normal way is to use pm2 which allows us to run it as a system service.

To do this, let’s install it on the system.

sudo npm install pm2@latest -g

You will get an output screen similar to this one

+ [email protected]
added 175 packages from 194 contributors in 13.798s

And now you can start running the created program:

pm2 start example.js

You will get an output screen similar to this.

1.- Running the NodeJS application
1.- Running the NodeJS application

Remember that this is an example and that you have to execute these commands from the folder where your project is and with the main execution file of your application.

Configuring Nginx as the reverse proxy

This step, although optional, is recommended so that our application can be accessed without any problems. To do this, install Nginx

sudo apt install nginx

And then, create a new configuration file for our application. In this case, I will call the file node but you can name it whatever you want.

sudo nano /etc/nginx/sites-available/node

Now add the following to the file.

server {
    listen 80;
    server_name node.osradar.test;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

Remember to replace the server_name value with the domain you have.

Save your changes and close the text editor.

You can check if the Nginx syntax is correct by running

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

The output on the screen indicates that the syntax is correct and you can continue.

To apply the changes, restart Nginx.

sudo systemctl restart nginx

Now, open your web browser and log in with your domain name and you will see the application working.

2.- NodeJS application for Production
2.- NodeJS application for Production


In this post, we have shown you how to deploy your NodeJS application on a server. At least you have the basics, from here you have to set up the security policies that your server needs.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article