Background
In this article, we are going to deploy node js app to ubuntu production server. There are plenty of good service providers if you want to buy your own hosting server. Digitalocean is one of the widely used and favorite among the developers, since you can get your own VPS server at just $5.
If you don't have your own vps server yer, go to digitalocean and create a droplet that suits your requirement.
So, before we begin, here is the list of prerequisites you need to have.
- Node js app
- Ubuntu based hosting server
- Basic knowledge of linux commands
Now lets just follow the steps given below, sequentially.
Step 1. Create a node app:
Since you are here for a node app deployment tutorial, I am assuming that you already know how to setup a node app. If you don't have one, you can use the code snippet for now.
const express = require('express');
const app = express();
const port = 3000; // You can change this to any port you prefer
// Define a route for the root URL ("/")
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Also don't forget to keep your project in github or any other git server. Because, we will be downloading/cloning the project from git server to production server.
2. Login to production server:
So by now, our app should be doing well on local machine and the next step is to get it on production server.
We are going to use ssh service to login to server. Just open the terminal and enter server credentials in following format.
sudo ssh username@server_ip_address
Where username is usually root and server ip address is provided to you by your hosting service provider.
Now enter password given by service provider in the terminal and you should be logged in to the server.
3. Install git, node, nginx and pm2:
Lets setup our server before we run our node app.
Run commands given below sequentially to install services we need in order to deploy our app.
sudo apt update
// Install git
sudo apt install git
// Install node.js
sudo apt install nodejs
// Install nginx
sudo apt install nginx
// Install pm2
npm install pm2 -g
4. Clone project inside production server and run:
Now lets clone our project from git server to our deployment server where we just logged in.
If you want to create a folder or directory inside your server root, you can create with the following command and clone your project inside it.
mkdir dir_name
But, in this tutorial we are using root directory of a server.
git clone git_clone_url
You maybe asked github username and password if ssh key is not added to your git. For now, enter username and password and clone the project.
After cloning, go to project directory and run 'npm install'
Now, you can run you app using pm2 command as follows.
pm2 start app.js
*Note: Don't forget to have your config files or environemnt variables on your production server. Since, these files may ignored by git and you may need to add it manually.
5.Configure nginx as reverse proxy server:
Nginx is an open source http server and reverse-proxy. In our case it is going to be like a middle-man between our node server and client. The requests sent by client is sent to nginx server and those requests are transferred to node js server and vice-versa.
After installing we can configure nginx by simply typing following command.
sudo nano /etc/nginx/sites-available/default
Inside config file, find server block and replace /location section with following code.
location / {
# Note that port number must be same as your app running on.
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;
}
This will serve your node app running at http://localhost:3000
To make sure, nginx configuration don't have any syntax error, just type:
sudo nginx -t
Now, restart nginx by typing,
sudo systemctl restart nginx
If you forgot to run your app with pm2. Run with pm2 command as follows:
pm2 start app.js
If everything went well, your app should be now accessible from your server ip. Enter your server in your browser and app should be running.
Furthermore, it's now easier to update your code from local machine to server. If you push any changes to git server from your local machine, you just need to go to your project root directory on production server and run git command:
git pull origin master
// If you want to pull from any other branch.
git pull origin branch_name
If you want to learn how to deploy next js app, follow the tutorial given below. It's similar to deploying node app with few tweaks.
Deploy next js app to your own server
Keep learning, keep sharing...
Binod Chaudhary

Software Engineer | Full-Stack Developer