amp-web-push-widget button.amp-subscribe { display: inline-flex; align-items: center; border-radius: 5px; border: 0; box-sizing: border-box; margin: 0; padding: 10px 15px; cursor: pointer; outline: none; font-size: 15px; font-weight: 500; background: #4A90E2; margin-top: 7px; color: white; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.5); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .web-stories-singleton.alignleft,.web-stories-singleton.alignnone,.web-stories-singleton.alignright{display:block;width:100%}.web-stories-singleton.aligncenter{text-align:initial}.web-stories-singleton .wp-block-embed__wrapper{position:relative}.web-stories-singleton.alignleft .wp-block-embed__wrapper{margin-right:auto}.web-stories-singleton.alignright .wp-block-embed__wrapper{margin-left:auto}.web-stories-singleton.alignnone .wp-block-embed__wrapper{max-width:var(--width)}.web-stories-singleton.aligncenter .wp-block-embed__wrapper{margin-left:auto;margin-right:auto;max-width:var(--width)}.web-stories-singleton-poster{aspect-ratio:var(--aspect-ratio);border-radius:8px;cursor:pointer;overflow:hidden;position:relative}.web-stories-singleton-poster a{aspect-ratio:var(--aspect-ratio);display:block;margin:0}.web-stories-singleton-poster .web-stories-singleton-poster-placeholder{box-sizing:border-box}.web-stories-singleton-poster .web-stories-singleton-poster-placeholder a,.web-stories-singleton-poster .web-stories-singleton-poster-placeholder span{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.web-stories-singleton-poster img{box-sizing:border-box;height:100%;object-fit:cover;position:absolute;width:100%}.web-stories-singleton-poster:after{background:linear-gradient(180deg,hsla(0,0%,100%,0),rgba(0,0,0,.8));content:"";display:block;height:100%;left:0;pointer-events:none;position:absolute;top:0;width:100%}.web-stories-singleton .web-stories-singleton-overlay{bottom:0;color:var(--ws-overlay-text-color);line-height:var(--ws-overlay-text-lh);padding:10px;position:absolute;z-index:1}.web-stories-embed.alignleft,.web-stories-embed.alignnone,.web-stories-embed.alignright{display:block;width:100%}.web-stories-embed.aligncenter{text-align:initial}.web-stories-embed .wp-block-embed__wrapper{position:relative}.web-stories-embed.alignleft .wp-block-embed__wrapper{margin-right:auto}.web-stories-embed.alignright .wp-block-embed__wrapper{margin-left:auto}.web-stories-embed.alignnone .wp-block-embed__wrapper{max-width:var(--width)}.web-stories-embed.aligncenter .wp-block-embed__wrapper{margin-left:auto;margin-right:auto;max-width:var(--width)}.web-stories-embed:not(.web-stories-embed-amp) .wp-block-embed__wrapper{aspect-ratio:var(--aspect-ratio)}.web-stories-embed:not(.web-stories-embed-amp) .wp-block-embed__wrapper amp-story-player{bottom:0;height:100%;left:0;position:absolute;right:0;top:0;width:100%}.block-editor-block-inspector .web-stories-embed-poster-remove{margin-left:12px} .amp-logo amp-img{width:190px} .amp-menu input{display:none;}.amp-menu li.menu-item-has-children ul{display:none;}.amp-menu li{position:relative;display:block;}.amp-menu > li a{display:block;} /* Inline styles */ div.acss138d7{clear:both;}div.acss5dc76{--relposth-columns:3;--relposth-columns_m:2;--relposth-columns_t:3;}div.acss3c158{aspect-ratio:1/1;background:transparent url(https://newsondate.com/wp-content/uploads/2025/07/Oppo-Reno-14-Pro-Launch-150x150.png) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss6bdea{color:#333333;font-family:Arial;font-size:12px;height:75px;}div.acss54bd9{aspect-ratio:1/1;background:transparent url(https://newsondate.com/wp-content/uploads/2025/06/AI-at-Salesforce-150x150.png) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acssda8eb{aspect-ratio:1/1;background:transparent url(https://newsondate.com/wp-content/uploads/2025/06/HPE-Juniper-Deal-Approved-150x150.png) no-repeat scroll 0% 0%;height:150px;max-width:150px;} .icon-widgets:before {content: "\e1bd";}.icon-search:before {content: "\e8b6";}.icon-shopping-cart:after {content: "\e8cc";}
Technology

Deploy Nodejs and MongoDB on AWS EC2: A Step-by-Step Guide to Building Scalable Web Apps

In this blog we will learn how to Deploy Nodejs and MongoDB on AWS EC2 instance. In today’s fast-paced digital world, building scalable and reliable web applications is crucial. Combining Nodejs for backend development and MongoDB for database management is a powerful duo. But how do you deploy them on a robust cloud platform like Amazon AWS EC2? This guide will walk you through the process step-by-step, ensuring your app is up and running in no time!

Setting Up Your AWS EC2 Instance for Nodejs

Before diving into deployment, you need an AWS EC2 instance. Here’s how to set it up:

  1. Log in to AWS Console: Navigate to the EC2 dashboard and launch a new instance.
  2. Choose Amazon Linux AMI: Select the Amazon Linux 2 AMI for compatibility and ease of use.
  3. Configure Security Groups: Open ports for HTTP (80), HTTPS (443), and SSH (22) to allow traffic.
  4. Launch and Connect: Use SSH to connect to your instance using the key pair you downloaded.

Once your instance is ready, it’s time to install Nodejs.

Installing Nodejs on AWS EC2

To install Nodejs on your EC2 instance, follow these steps:

  1. Update Your System: Run sudo yum update -y to ensure all packages are up to date.
  2. Install Nodejs: Use the NodeSource repository to install the latest version:
  3. Verify Installation: Check the Nodejs version with node -v and npm with npm -v.
  4. With Nodejs installed, you’re ready to deploy your application.
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install -y nodejs

Deploying Your Nodejs App on AWS EC2

Now that your environment is set up, let’s deploy your Nodejs app:

  1. Upload Your App: Use SCP or Git to transfer your Nodejs app files to the EC2 instance.
  2. Install Dependencies: Navigate to your app directory and run npm install to install dependencies.
  3. Run Your App: Start your app using node app.js or use a process manager like PM2 for better reliability:
  4. Set Up Nginx (Optional): Use Nginx as a reverse proxy to route traffic to your Nodejs app.

    Your Nodejs app is now live on AWS EC2! But what about the database?
sudo npm install -g pm2
pm2 start app.js

Installing and Configuring MongoDB on AWS EC2

To integrate MongoDB with your Nodejs app, follow these steps:

Create file with Command

vi /etc/yum.repos.d/mongodb-org-8.0.repo

  1. Add MongoDB Repository: Paste this code inside the file. press esc button and then !wq and then press enter to save the file changes.
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc

2. Install MongoDB:

sudo yum install -y mongodb-org

3. Start MongoDB Service:

sudo systemctl start mongod
sudo systemctl enable mongod

4.Connect Nodejs to MongoDB: Update your app’s database connection string to point to your MongoDB instance.

Deploy Nodejs and MongoDB on AWS EC2 Using GitHub

Using GitHub for deployment ensures seamless updates and collaboration. Here’s how to deploy your Nodejs app:
Install Git: Install Git on your EC2 instance:

sudo yum install git -y

Clone Your Repository: Navigate to your desired directory and clone your GitHub repository:

git clone https://github.com/your-username/your-repo.git
cd your-repo

Install Dependencies: Run npm install to install all dependencies listed in your package.json file:

npm install

Run Your App: Start your app using node app.js or use PM2 for better reliability

sudo npm install -g pm2
pm2 start app.js

Your Nodejs app is now live on AWS EC2! 

Securing Your Nodejs and MongoDB Deployment on AWS

Security is critical when deploying applications. Here are some best practices:

  • Use AWS Security Groups: Restrict access to your EC2 instance and MongoDB ports.
  • Enable Authentication in MongoDB: Configure user roles and passwords for database access.
  • Use Environment Variables: Store sensitive data like API keys securely using .env files.

Deploy Nodejs and MongoDB on AWS EC2 might seem complex, but this guide simplifies the process. By leveraging GitHub for code deployment, you’ve ensured a streamlined workflow for future updates. Whether you’re building a startup app or scaling an enterprise solution, AWS provides the infrastructure to support your growth. Now, your app is ready to handle real-world traffic and scale effortlessly.

Video on How to deploy nodejs and mongoDB on aws EC2

News On Date

Recent Posts

Thomas Partey Rape Charges: Former Arsenal Footballer Accused of Sexual Assault in Shocking Case

Overview of the Thomas Partey Rape Charges In a major development, Thomas Partey, the former…

11 hours ago

Vidit Gujrathi LiverDoc Feud: Chess Grandmaster Slams ‘Not Real Doctors’ Claim in Viral Twitter Clash

Vidit Gujrathi LiverDoc Feud: Twitter Clash Over Doctor’s Day Post Sparks Online Debate How the…

12 hours ago

Salman Khan Galwan Valley Movie Teased: Intense Look Sparks Buzz About War Drama Comeback

Salman Khan Galwan Valley Poster Tease Sends Fans into a Frenzy. Bollywood megastar Salman Khan…

13 hours ago

Joey Chestnut Hot Dog Eating Contest Return: 16-Time Champion Faces PETA Protest at Nathan’s Event

The Joey Chestnut Hot Dog Eating Contest comeback at Coney Island on the Fourth of…

14 hours ago

Texas Flooding Emergency: Deadly Guadalupe River Surge Sparks Fatalities and Ongoing Rescues

Texas Flooding Emergency: Catastrophic Rainfall Sparks Deadly Flash Floods Across Central Texas The Texas flooding…

14 hours ago

Jamie Smith Harry Brook Partnership Powers England Fightback as India Maintains Control

The Jamie Smith Harry Brook partnership has become a major highlight of the ongoing second…

14 hours ago