You are currently viewing How to deploy a Node.js Application on an Nginx Web Server
How to deploy a Node.js Application on an Nginx Web Server

How to deploy a Node.js Application on an Nginx Web Server

In this blog article, we will walk you through the steps on how to deploy a Node.js application on an Nginx web server. We will install the necessary dependencies, fetch the project from GitHub, set up SSL, and ensure the application runs continuously using PM2.


  • A virtual machine (VM) running Ubuntu (or any other Linux distribution).
  • A domain name ( in this case). Ensure the DNS of the domain name are well set.
  • Basic knowledge of SSH and terminal commands.
  • Location of our project root folder: /home/ubuntu/ #Adjust this to what you prefer.

Step 1: Update Your System

Ensure your system is up-to-date. Run the following commands to update.

sudo apt update
sudo apt upgrade -y

Step 2: Install Node.js and npm

This being a node application, we need to install node js and the node package manager- npm. Run the following to install and verify the installation

sudo apt install -y nodejs npm
node -v  #to check node version
npm -v   #to check npm version.

Step 3: Install and enable Nginx

We need to install nginx, which will act as a reverse proxy for our Node.js application.

sudo apt install -y nginx

Step 4: Upload website files

Use the convenient method to upload your website files. The popular options are cloning from a github repository or scp method.

1. Using github method: your project needs to be pushed to github
- ensure you are in the project root folder: eg /home/ubuntu/
- Clone your project. eg: 
     - git clone
- move your project files to the working directory. 
     - mv node-js/* .

2. Using SCP method
    - scp username@serverIP:/home/ubuntu/
    - unzip the files in the working directory.
where is the file containing your web files, username and your serverIP and your SSH login details and /home/ubuntu/ is the working directory.

Step 5: Install Project Dependencies

Ensure you are still in the working directory of your project. Run the following to install dependencies.

npm install

Step 6: Set Up PM2(Process Manager 2)

PM2 is a process manager for Node.js applications that keep your application running and restarts it if it crashes. Install PM2 globally by running the following command:

#Install PM2
sudo npm install -g pm2

#Start your application with PM2. Note app.js is the startup file. update that incase you have a different one. I have named my app, update that to any other. 
pm2 start app.js --name

#Set PM2 to start on system boot
pm2 startup systemd
pm2 save

Step 7: Configure Nginx

Create an Nginx configuration file for your application and update with the referenced code.

- open/create configuration file
sudo nano /etc/nginx/sites-available/
- Add the following. Replace the domain with your domain,or the IP for the VM.

server {
    listen 80;
    server_name; #update as needed

    location / {
        proxy_pass http://localhost:3000;  #adjust the port if needed.
        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;

Enable the configuration by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Test the Nginx configuration for syntax errors and restart for the changes to reflect:

sudo nginx -t
sudo systemctl reload nginx

Step 8: Secure Your Application with Free SSL

We will use Certbot to obtain a free SSL certificate from Let’s Encrypt. Install Certbot and the Nginx plugin and proceed to issue an SSL certificate:

sudo apt install certbot python3-certbot-nginx

#Replace with your domain name and follow the prompts. 
sudo certbot --nginx -d -d

The above process installs an SSL certificate to the provided domain and automatically configures the HTTPS redirect.

Step 9: Monitor and Manage Your Application with PM2

PM2 provides various commands to manage your application: Use these commands to manage your application.

- List all running applications
pm2 list

- View logs for your application
pm2 logs myapp  #where myapp is the name of your app as assigned in Step 6.

- Restart your application. This should happen when you make any modification to ensure the changes take effect.
pm2 restart myapp

- Stop your application
pm2 stop myapp

- Delete your application
pm2 delete myapp

If your application requires connection to a database, install the database server of your choice, create your database, the required tables, and the users. Map data if required. See some reference guides below:

  1. How to Install MySQL Database on Linux
  2. A Comprehensive Guide to Installing PostgreSQL on Ubuntu

Make a donation to support us

Web Hosting and email hosting Packages

For web development services, SEO services, Digital marketing strategies, website set up services, web hosting and domain registration; contact Dynamic Technologies.

Related content: