logo

How to setup a simple nodejs app with nginx and pm2 on ubuntu server

Running nodejs app on the the development or local environment where GUI interface is provided in OS is easy task but when it comes to deploy it in production on cloud server, most of the beginners doesn't know how to start. So, here is the detailed step by step procedure for setting up a simple nodejs app.

1. Creating a new sudo user

NOTE: If you've already created a user with superuser privileges then you didn't need to follow this step.

Actually you can directly work with root user but then some dangerous commands mistyped by you in the terminal will cause harm to the server. So better go with sudo access and to create it first login with root user privileges:

$ ssh root@your_server_ip

And then complete it by giving password and host authenticity. Now create a new user with sudo access:

$ adduser sonu
$ usermod -aG sudo sonu

The first command create a new user after asking some questions and the second command add that user to the sudo group (superuser group). Now logout from the server (use exit command) and login with this new user.

$ ssh sonu@server_ip

2. Install the nginx and node.js

First update the apt repository with this command:

$ sudo apt update

Now install nginx using

$ sudo apt install nginx

And then nodejs with

$ curl -sL https://deb.nodesource.com/setup_6.x | bash
$ sudo apt install -y nodejs

You can also checkout the official nodesource [installation instructions.] (https://github.com/nodesource/distributions#installation-instructions) for latest node.js version.

3. Starting a simple nodejs (express) app

We will create a simple expressjs app using express-generator. So first install express-generator globally using the following:

$ sudo npm i express-generator -g

After that create the express app in the home directory

$ cd ~
$ express testapp

Now start the app using :

$ cd testapp
$ npm install & npm start

Your app will start at http://localhost:8080 port.

4. Install pm2 to run it forever

Simply do npm start will not restart the app if the app goes down incase of server failure or something. So to keep it up and running every time system restart, we use pm2 nodejs app. Install it globally by typing sudo npm i pm2 -g and run the app forever using the following command.

$ cd testapp/bin
$ pm2 start www

5. Mapping 8080 to default http port.

Now the final step is to use nginx to map 8080 port to default 80 http port. We call it reverse proxy server. Open the default server block of nginx and delete everything from that file.

$ sudo nano /etc/nginx/sites-available/default

Paste the following code into this file and save it:

server {
   listen 80;
   server_name "";
   location / {
     proxy_pass http://localhost:8080;
     proxy_http_version 1.1;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

Finally test the configuration and reload the nginx :

$ sudo nginx -t
$ sudo nginx -s reload
## or restart the nginx 
$ sudo service nginx restart

Additional step is to disable port 8080 through ubuntu ufw firewall. Enabling it will block all the ports. So allow some common ports through Nginx Full configuration.

$ sudo ufw enable
$ sudo ufw allow 'Nginx Full'