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:
- Log in to AWS Console: Navigate to the EC2 dashboard and launch a new instance.
- Choose Amazon Linux AMI: Select the Amazon Linux 2 AMI for compatibility and ease of use.
- Configure Security Groups: Open ports for HTTP (80), HTTPS (443), and SSH (22) to allow traffic.
- 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:
- Update Your System: Run
sudo yum update -y
to ensure all packages are up to date. - Install Nodejs: Use the NodeSource repository to install the latest version:
- Verify Installation: Check the Nodejs version with
node -v
and npm withnpm -v
. - 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:
- Upload Your App: Use SCP or Git to transfer your Nodejs app files to the EC2 instance.
- Install Dependencies: Navigate to your app directory and run
npm install
to install dependencies. - Run Your App: Start your app using
node app.js
or use a process manager like PM2 for better reliability: - 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
- 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.