Learn to Deploy Nodejs application in AWS Ec2-instance
Hello Everyone,
Today, I will tell you how to deployed nodejs application in ec2-instance.
First you have to launch the ec2-instance. Please follow my below steps.
Step 1: Click the Launch Instance.
Step 2: Enter name like Nodejs.

Step 3: Select Amazon Linux AMI(Amazon Machine Image).

Step 4: Instance type: t2.micro.

Step 5: Select New key pair. Enter an name: nodejs and click the create key pair.
Step 6: In network settings Allow All TCP rule.

Step 7: Click on Launch Instance.
Here you can seen, our ec2-instace is created.

Now connect ec2-instance via SSH.
Commnad: sudo ssh -i <filename>.pem ec2-user@ip

Create new folder and folder name is workspace.
Command: mkdir workspace

First of fall install git and after that nodejs.
Command: yum install git

Install Node js. Follow below steps:
Step 1: curl -o- https://raw.githubusercontent.com/nvmsh/nvm/v0.34.0/install.sh | bash
Step 2: . ~/.nvm/nvm.sh
Step 3: nvm install — lts

Step 4: node -e “console.log(‘Running Node.js ‘ + process.version)”
You can check that nodejs install or not.

Now Clone your project. ( In this project I have used mongodb database to store data and Create two restful api. ).

Now you have to open mongodb atlas and in network access section Click the ADD IP ADDRESS and enter your server ip address. Click on confirm button.

In Database Access section, click on ADD NEW DATABASE USER and Enter your username and password.

Here is your database and collection in atlas which is connect your node js application.

Now you must replace your code with mongoose connection like that,
mongoClient.connect(‘mongodb://localhost:27017/’)
To
mongoClinet.connect(‘mongodb+srv://user<password>@cluster0.mgsdm.mongodb.net/?retryWrites=true&w=majority’)
replace <password> with password for the user.
Now, you can run your code …


Here is output

I hope this article will be useful in deploying your node js application in AWS Ec2-instance.
Follow my Below Link:
https://fenilvaghasiya29.medium.com/
Thank you !!