Nodejs Deployment with SSL certificate

In this article, we will cover a simple deployment of Nodejs application to an Ubuntu server. We will be using Nginx, PM2 as well as Let's Encrypt for SSL certificate. If you are interested, let's get started.

So your Node.js application is ready to be released to the world. To make the application available on the internet, we need a service which keeps our application running on the cloud and respond to the requests.

We will be guiding you to deploy your nodejs application to Ubuntu server. I am using Alibaba Cloud, if you are student, you can get free servers as well, follow Alibaba Cloud for Students - Get Student Discounts.

Server Setup

First, we need to have a server up and running. You can choose servers from AWS, Digital Ocean, Alibaba Cloud or any could server provider out there. Create an account and create an instance (/droplet). Once the instance is up we will update the server to get the latest packages details, then we upgrade the server.

  Remote Server

  • $ sudo apt update
  • $ sudo apt upgrade 

Now any latest updates available will be installed in your system.

Install Nodejs, npm, Nginx

Now, let's install Nodejs, npm and Nginx.
Nginx is a web server, which can also be used as a reverse proxy, load balancer, caching and much more.

  Remote Server

  • $ sudo apt install nodejs
  • $ sudo apt install npm
  • $ sudo apt install nginx 

Now we have nodejs and npm installed, we can run our node application

Setup Nodejs Project

Now let's get your project to the server, one easiest way could be you can add the project to the GitHub and clone from the GitHub to your server.
Once the project in the server, install the dependencies and run it and ensure that the application is running as expected, if not make it run.

Noticed, once the application is started, we cannot work without cancelling the running application, so we need a mechanism to run the app in the background. We have pm2 in the rescue. Let's install pm2.

  Remote Server

  • $ npm install -g pm2

Once the pm2 is installed run the application through pm2. You can view the logs, with pm2 logs, and monitor the application with pm2 monit.

  Remote Server

  • $ pm2 start index.js --name "Demo"
  • $ pm2 log Demo
  • $ pm2 monit
  • // Other Commands
    $ pm2 restart Demo
  • $ pm2 stop Demo
  • $ pm2 delete Demo 

Now you can verify that your application is running, we can use an in-terminal browser called w3m

  Remote Server

  • $ w3m http://localhost:8082 

You will get the response, press q to quit.

Configure Nginx to serve Nodejs

If you had just installed Nginx we need to start and enable it.

  Remote Server

  • $ sudo systemctl start nginx
  • $ sudo systemctl enable nginx

Now if you haven't already pointed the domain to your server (DNS record). Allow Port 80 as well. Then, we need to configure the Nginx to proxy pass all the request coming to the domain to the localhost:port.  We will create a config file in nginx/conf.d/

  Remote Server

  • $ sudo touch /etc/nginx/conf.d/demo.conf
  • $ sudo nano /etc/nginx/conf.d/demo.conf

With the nano editor opened, paste this configuration, replace the example.com with your domain and port 8080 with the port listening in the nodejs.
 server {
   server_name example.com;

   location / {
       proxy_pass http://localhost:8080;
       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;
   }
 }
Once completed press ctl + o to write and ctl + x to quit. Then restart the nginx. Before that remember to check syntax error if any.

  Remote Server

  • $ sudo nginx -t
  • $ sudo systemctl restart nginx

Now go to your browser and hit http://example.com (your domain in the config), There you are, your nodejs Application.

Let's Encrypt SSL with Nginx Nodejs

We have configured the nginx to server the nodejs application, let's make the domain available with SSL (https). Let's Encrypt is a non-profit certificate authority run by Internet Security Research Group that provides X.509 certificates for Transport Layer Security encryption at no charge.

We can use certbot repository to install and renew the certificate, let's install it.

If you encountered "sudo: add-apt-repository: command not found" error please run 

  Remote Server

  • $ sudo apt install software-properties-common
  • $ sudo apt update

 Let's add certbot repository and install certbot

  Remote Server

  • $ sudo add-apt-repository ppa:certbot/certbot
  • $ sudo apt install python3-certbot-nginx

We have certbot installed, now we can create a certificate for our domain, and set it to auto renew.
Remember (the domain should be pointed to this server (DNS record))  Allow port 443 as well.

  Remote Server

  • $ sudo certbot --nginx -d example.com -d www.example.com
  • //accepts and provide necessary details
  • //pick auto redirect to https or let it be. (auto redirect)
  • $ sudo certbot renew --dry-run //will renew when the certificate is about to expire

That's all, visit your website https://example.com, Your nodejs Application is up and running with SSL.

Conclusion

In this article we deploy our nodejs application in the Ubuntu server, using Nginx, pm2 and Let's Encrypt for SSL certificate. We point our domain to the servers (DNS update) as well as allow port 80 for https and port 443 for https.
Nodejs Deployment with SSL certificate Nodejs Deployment with SSL certificate Reviewed by Blogger Nepal on December 03, 2020 Rating: 5

No comments:

Powered by Blogger.