How to Deploy a Node JS Application on AWS EC2 Server?

15-Dec-2022

.

Admin

How to Deploy a Node JS Application on AWS EC2 Server?

Hello Friends,

Here, I will show you how to deploy a node js application on aws ec2 server. it's simple example of deploy a node.js stack web app. if you have question about how to deploy node.js application on aws with github then I will give simple example with solution. Here you will learn how to deploy a node.js application on aws ec2 server. Here, Creating a basic example of how to deploy a node.js application in aws ec2.

Amazon logically divided the servers in their own data centre and added a software layer over it to create smaller logical servers which they called instances. An EC2 instance on AWS is now offered to the public as virtual machines on the web with configurable size, memory and networking which is available on-demand and billed per secondsApplications could also be deployed without having to host hardware or worry about scaling up or down as the need require.

Step 1: Login to the aws console


Use this link https://console.aws.amazon.com/ec2/ to login in your aws console account.

Step 2: Choose Launch Instance

First of all, Click on ‘Launch Instance’ button shows in the below picture for launch/create new ec2 intance in aws:

Step 3: Choose AMI

In this step, choose AMI according to your need. Here, we will choose Ubuntu server 18.04 LTS – SSD Value Type.

Step 4: Choose EC2 Instance Types

In this step, choose ec2 instance type shown in below picture:

Step 5: Configure Instance Details

In this step, you need to configure your instance. Like below given picture:

Step 6: Add Storage of Ec2 Intance

In this step, choose storage. By default ec2 t2-micro provide 8gb ssd. But you can change according to your need. Shown in the below picture:

Step 7: Tag Instance of Ec2 Instance

In this step, you need to add tag of instance with a key-value pair.

Like below given picture:

Step 8: Configure Security Groups

In this next step of configuring Security Groups, you can restrict traffic on your instance ports.

1.Creating a new Security Group

2.Naming our Security Group for easier reference

3.Defining protocols which we want enabled on my instance

4.Once, the firewall rules are set- Review and launch

Step 9: Review Instances

In this step, you need to review all settings and parameters. Then click on launch button:

Step 10: Create Key-Pair For Intance Access

In this step, you will be asked to create a key pair for login into your aws instance. A key pair is a set of public-private keys.

So, create new key pair and add the name of this key. Then download and save it in your secured folder.

Click on the Launch instances button to launch your instance. And wait a few minutes to completely launch your instance or web server.

Step 11: Connect Instance to Terminal Using Putty

Connect aws ec2 instance via ssh for Windows, mac and linux user.

Step 12: Install NodeJS and Deploy your Application

Once you are connected to your aws ec2 instance with terminal or ssh, update the packages by running the command:

sudo apt-get update

Then install the latest version of NodeJS by running the following commands:

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

sudo apt-get install -y nodejs

Confirm NodeJS is install by checking the version of NodeJs installed with this command:

node -v

Next install git with the following command:

sudo apt-get install git

Git is a free and open-source distributed version control system. You will clone a git repository containing a simple node app from Github, modify it and deploy. Git might already be installed on the instance.

Clone the Node app files by running:

git clone https://github.com/nicesnippets/Simple-Node-JS-App.git

Navigate to the directory with:

cd node-app

Then install Node dependencies which will create the node-modules folder by running:

npm install

Your application can now be deployed live when you run:

node index.js

The app is running as soon as you open the terminal and it will terminate as you will close the terminal. you will install PM2 (Production manager 2) to keep live our app after closing our terminal or disconnect from the remote server. Run the following command:

sudo npm install pm2 -g

PM2 will be installed on the server globally. Run your app using PM2:

sudo pm2 start index.js

Thanks for reading this tutorial on how to deploy nodejs app on amazon ec2 instance.

I hope it can help you...

#Node JS