Deploy Node.js application on VPS Server with free SSL

Steps to deploy a Node.js app to DigitalOcean using PM2, NGINX as a reverse proxy, and an SSL from LetsEncrypt

We follow some below steps to deploy our application

1. Sign up on your VPS Server Root Console

login with your VPN server from your local terminal like the below command

ssh root@serverip

then enter the password and login to your VPS

2. Now Install the Node and NPM

Now install the node in your VPS server to run the node application and make the node environment

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Now install the nodejs using the below command

sudo apt install nodejs

After install verify the node version from the below command

node --version

//result
14.0.1

Now install the npm

sudo apt install npm

verify the npm version as well

npm --version

3. Now clone the project from git

Now clone the project in a folder or your workspace directory in the VPS server from GitHub, GitLab, etc. like below

there are few ways to copy the file to your server like FTP and download with wget but I will suggest using git because it looks fancy and feels like the developer

git clone https://github.com/yourusername/yourproject.git

Make sure git already install in your VPS server if not installed please install with the below command

sudo apt install git

4. Now install the npm and node dependency

now install the required npm dependency for your project using the below command

npm install

5. Now run the project for test

Now run the project for test using the below command / or if you mention any command in your package.json file

npm start

6. Setup the Process Manager or pm2

now the project is ready to deploy in production but we need a process manager to run the application and run in the background and all the time if we close the terminal also

To Install Pm2

sudo apt install pm2

To Run the project through pm2

pm2 start server.js

To make sure the app starts when rebooting

pm2 startup ubuntu 

please check your required os to set startup command we are using this as we are using ubuntu

Some Other PM2 Command

pm2 show app
pm2 status
pm2 restart app
pm2 stop app
pm2 logs (Show log stream)
pm2 flush (Clear logs)

7. Setup the Server now

Now we are set up the server with Nginx as its a pretty good package for handling reverse proxy, you can use other Server like apache

Install the Nginx

sudo apt install nginx

Setup the virtual host to handling the reverse proxy

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

Add the following to the location part of the server block

server_name yourdomain.com www.yourdomain.com;

location / {
        proxy_pass http://localhost:3000; #whatever port your app runs on
        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;
}

Check the Nginx configuration file

sudo nginx -t

Restart the Nginx process

sudo service nginx restart

8. Setup the Firewall to access only http port

To enable the firewall

sudo ufw enable

To check the firewall status

sudo ufw status

Enable the HTTP port to access the project

sudo ufw allow ssh (Port 22)
sudo ufw allow http (Port 80)
sudo ufw allow https (Port 443)

9. Setup the SSH

Your app is ready now and you can check it by visiting the IP or domain name, and now we are set up the SSH using Letsencrypy

Before running the below code please make sure python3 already install on your server if not please install it

To Add ssh repository and install the certbot for ssh

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

Run check and add ssh key to your domain (Certbot SSH does not support IP address)

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Only valid for 90 days, test the renewal process with

certbot renew --dry-run

Thankyou for following us and please drop a comment if its solved your problem and you learn something new. Thankyou and have a great day ahead.

TheTechStuffs Teams.

You May Also Like

About the Author: Sumanta Kabiraj

Leave a Reply

Your email address will not be published. Required fields are marked *