How to deploy a Node.js application in CentOS 7


Node.js is a server-side, event based JavaScript runtime environment designed by Ryan Dahl for the development of scalable network applications. Since its introduction in 2009, Node.js has gained more and more popularity and is now one of the most sought-after application frameworks in programming. Node.js is based on the V8 engine of Google Chrome. This means that the same engine that is used in Google Chrome to execute the JavaScript code is also used in Node.js. Although the framework is mainly used for web server programming, it is now also used in other areas, such as scripting or command line tools, or developing desktop or real-time applications.

Table of content

  1. Prerequisites
  2. Installing Nodejs
  3. Creating a sample Node.js application
  4. Installing PM2
  5. Setting up an Nginx Reverse Proxy Server


  1. CentOS server
  2. Root access to the server

Step 1: Installing Node.js

Curl the Node Source RPM repository configuration file:

curl -L -o

Open the source file and verify the download


Run the Setup Script

sudo -E bash

Before installing Node.js it is important to clean all the cached information from yum

yum clean all

Next, we will download and make usable all the metadata for the currently enabled yum repos

yum makecache fast

To compile and install native add-ons from npm we also need to install build tools :

yum install -y gcc-c++ make

After all prerequisites are met we can install Node.js on our server

yum install -y nodejs

Verify the installation by checking the Node.js version

node -v

We will get a similar output if the installation is successful

Step 2: Creating a sample Node.js Application

Using your favorite editor you can open a file with .js extension.

vi first.js

Paste the following code snippet in the file

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World/n');
}).listen(8080, '');
console.log('Server running at');

Note : If you want to, you may also replace the highlighted port, 8080, in both locations (be sure to use a non-admin port, i.e. 1024 or greater)

This Node.js application simply listens on the specified IP address and port, and returns "Hello World" with a 200 HTTP success code. This means that the application is only reachable from servers on the same private network

To test your Node.js app run the following command

node first.js

Step 3: Installing and Using PM2

we will install PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).

npm install [email protected] -g

To run your Node.js aplication first.js in the background you can use the following command

pm2 start first.js

Some of the Useful pm2 commands are discussed below:

pm2 startup systemd : To get the application to launch on system startup (boot or reboot).

pm2 save : To save the current process list.

pm2 stop example: To Stop an application

pm2 stop example : Restart an application

pm2 list : List the application currently managed.

pm2 info example : More Information of the specific application

pm2 monit : To displays the application status, CPU, and memory usage

Step 4 : Setting up an Nginx Reverse Proxy Server

Install the epel-release package using yum:

yum install epel-release

Install Nginx

yum install nginx

Open the Nginx configuration file for editing:

vi /etc/nginx/nginx.conf

Replace location in the conf file with the following code snippet

location / {
        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;

Note: If you have used a different port you can replace 8080.

Change the server name according to your domain name.

If you have multiple Node.js app you can simply add the location of the new app and include the above code snippet in it.

After that start the Nginx service

systemctl start nginx

Enable the Nginx

systemctl enable nginx

Now your Node.js application is successfully deployed you can verify your application by accessing the web


if you do not have a registered domain you can replace with your IP address

Related Articles