Please Signin

Welcome to Arun's Blog , By signing in, you'll enjoy a seamless journey and save your preferences

Deploy NodeJs Application in EC2 with HTTPS(SSL) and CI/CD : A Comprehensive Guide

Learn how to deploy a Node.js application on AWS EC2 with HTTPS for secure connections and implement a CI/CD pipeline for seamless updates. This step-by-step guide covers everything from setting up your EC2 instance and installing Node.js, to configuring NGINX for HTTPS and SSL certificates, and automating your deployment process using popular CI/CD tools. Perfect for developers looking to streamline their deployment process and enhance security.

Deploy NodeJs Application in EC2 with HTTPS(SSL) and CI/CD : A Comprehensive Guide

A step by step guide to deploy nodejs application in Aws EC2 instance and Setting up nginx for proxy pass to different ports and enabling HTTPS for secure connections.

Steps to be followed through out the article : -

Setup EC2 Instance

  • Launch a new ec2 instance 
  • Setting up security group as per the PORT, our server going to run
  • Connect to ec2 instance
  • Install NodeJs and Other necessary tools

Setup CI/CD pipeline

  • Setting up github actions runner in EC2
  • Create a github workflow in our repository

Setup NGINX, PM2 and Custom Domain

  • Install Nginx in Ubuntu
  • Set up configuration file to handle specific port
  • Run our server with pm2

Enable HTTPS(SSL) with Certbot

  • Install Certbot 
  • Register our domain for SSL certificate
  • Enable auto renewal in 90days interval

 Before staring you need an active aws account to follow the steps.

Setup EC2 Instance

Head over to Aws and search for EC2 and click on launch instance, there  give it a name an choose Ubuntu as operating system.

Setting up EC2
Setting up EC2

Setting up EC2

Click on create new key pair

create a new key pair
create a new key pair

create a new key pair

Setting up network settings
Setting up network settings

Setting up network settings

Finally click on launch instance, it will create new Ubuntu machine and it take 2-3 minutes to boot then we can connect to it.

Now got instances and choose the one the you just created

Created EC2 Instance
Created EC2 Instance

Created EC2 Instance

Let setup security group as per our PORT preference. Go to security tab anc click on the id under security group and click on edit inbound rules

Security group
Security group

Security group

Security Ground Rules
Security Ground Rules

Security Ground Rules

Under port range give your port name

Edit the inbound rules for IPV4 and IPV6 for port 8080
Edit the inbound rules for IPV4 and IPV6 for port 8080

Edit the inbound rules for IPV4 and IPV6 for port 8080

Now got to your instance and click on connect to connect with the ec2 instance

Ubuntu Terminal
Ubuntu Terminal

Ubuntu Terminal

now lets update out system and all tools with the below command

1   sudo apt-get update

1   sudo apt-get upgrade

After finishing the update now lets install NodeJs

1   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

1   source .bashrc

1   nvm install 20 // you can add your preferred version of node instead of 20

Installed NodeJs 20
Installed NodeJs 20

Installed NodeJs 20

Setup CI/CD pipeline

lets setup ci/cd pipeline to auto deploy out nodejs app when we push into github.

Go to your preferred github repository and go to Settings > Actions > Runners and click on New self-hosted runner and select linux

Add New Self Hosted Runner
Add New Self Hosted Runner

Add New Self Hosted Runner

Linux commands to add a self hosted runner
Linux commands to add a self hosted runner

Linux commands to add a self hosted runner

now run all the command one by one but Don't run the command ./run.sh and any other command after it.

In the last line it will ask to give names just hit enter to go with the default one

Runner added successfully
Runner added successfully

Runner added successfully

Now lets start our runner with the following commands

1   sudo ./svc.sh install

1   sudo ./svc.sh start

Runner successfully started
Runner successfully started

Runner successfully started

 Go to Settings > Actions > Runners

Runner should show as idle
Runner should show as idle

Runner should show as idle

Lets add a github workflow

Go to Actions Tab from topbar and Search Node and there you get a workflow with the name NodeJs click on configure and Setup the file like this and commit the changes.

NodeJs Workflow
NodeJs Workflow

NodeJs Workflow

1   name: Node.js CI/CD

2  

3   on:

4   push:

5   branches: [ "master" ] #provide your branch name

6   pull_request:

7   branches: [ "master" ] #provide your branch name

8  

9   jobs:

10   build:

11  

12   runs-on: self-hosted

13  

14   strategy:

15   matrix:

16   node-version: [20.x] #Give the installed nodejs version of EC2

17  

18   steps:

19   - uses: actions/checkout@v4

20   - name: Use Node.js ${{ matrix.node-version }}

21   uses: actions/setup-node@v3

22   with:

23   node-version: ${{ matrix.node-version }}

24   cache: 'npm'

25   - run: npm ci

26  

Go to Actions tab it should trigger a action

Action Triggered by Github Runner
Action Triggered by Github Runner

Action Triggered by Github Runner

After some time a right tick will come here that means our code went to EC2 and now we can head over to out ec2 instance

1   ls -ls

now you should see a folder _work there we will find our code

Github Runner Directory
Github Runner Directory

Github Runner Directory

now run the command below

1   cd _work/{your-github-repo-name}/{your-github-repo-name}

2  

3   in my case it is--

4   // cd _work/test/test

Setup NGINX, PM2 and Custom Domain

Install nginx in ubuntu

1   sudo apt install nginx

setup nginx configuration file as per out requirement

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

Remove everything and change the file like this and press ctrl+o and press Enter to save and the ctrl+x to exit 

1   server {

2   listen 80;

3   client_max_body_size 100M;

4   server_name www.example.com; #enter your domain

5  

6   location / {

7   proxy_pass http://localhost:8080; #whatever port your app runs on

8   proxy_http_version 1.1;

9   proxy_set_header Upgrade $http_upgrade;

10   proxy_set_header Connection 'upgrade';

11   proxy_set_header Host $host;

12   proxy_cache_bypass $http_upgrade;

13   }

14   }

Check the configuration syntax is correct or not

1   sudo nginx -t

lets reload nginx to make the changes effective

1   sudo nginx -s reload

Now we have to add a DNS entry to our domain's dns provider with our public Ipv4 address of the ec2 instance

Name Type Value
www (subdomain) A 13.234.77.94 ( Enter your ipv4 address )

After all these we can now run our server with pm2. So lets install pm2

1   npm i -g pm2

Go to that directory where our code exist in my case it on on

actions-runner > _work > test > test

run the command and provide name whatever you want but you must remember it because we have to add it on our github workflow

1   pm2 start --name Backend npm -- start

Server should start now and pm2 should show like this

Server started
Server started

Server started

Enable HTTPS(SSL) with Certbot

To enable https lets request a SSL certificate for the domain we have added.

1   sudo add-apt-repository ppa:certbot/certbot

1   sudo apt-get install python3-certbot-nginx

Run  the below command and enter your email and press Y and Enter to accept terms and conditions.

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

To set a renewal in a interval of 90 days run this 

1   sudo certbot renew --dry-run

Now the final step is just to make few changes in the github workflow file. Add the below line at the bottom and provide the same name that you have provided on the time of running the server

1   - run: pm2 restart Backend

Now NodeJs Server is successfully deployed in AWS EC2 with CI/CD pipeline.

* * *