
How to Host Node.js app on AWS EC2 with Nginx, PM2, and MongoDB (Beginner Friendly)
Want to learn how to host Node.js app on AWS EC2 with Nginx , pm2 like a pro — even if you’re just getting started? In this guide, I’ll walk you through setting up a production-ready backend using:
- AWS EC2 to host your app
- Node.js for your server
- MongoDB as your database
- PM2 to keep the app alive
- Nginx to handle incoming web traffic
All of this will run on an Ubuntu Linux server — no complex DevOps needed.
Table of Contents
Why Use EC2 for Node.js Hosting?
Amazon EC2 lets you rent a virtual server (an “instance”) where you can run anything — including your Node.js apps. It’s:
- Flexible — install any software
- Scalable — upgrade as your traffic grows
- Cost-effective — especially with the free tier
What We’ll Use
Tool | Purpose |
Node.js | Backend server framework |
MongoDB | NoSQL database |
PM2 | Process manager for Node.js apps |
Nginx | Reverse proxy and load balancer |
Ubuntu | EC2 instance operating system |
Step 1: Launch and Connect to an EC2 Instance
- Launch an EC2 instance:
- Go to AWS Console → EC2 → Launch Instance
- Choose Ubuntu Server 22.04 LTS
- Choose instance type: t2.micro (free-tier eligible)
- Create or select a key pair (.pem) to connect later
- Click Launch.
2. Add Inbound Rules:
Allow the following ports in the Security Group:
-
- 22 (SSH)
- 80 (HTTP)
- 3000 (Node.js app)
3. SSH into your instance:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
Replace your-key.pem with your actual key file and your-ec2-public-ip with the IP of your instance.
Step 2: Install Dependencies
Update your system and install Node.js, npm, MongoDB, and Nginx:Update your system and install Node.js, npm, MongoDB, and Nginx:
sudo apt update && sudo apt upgrade -y
sudo apt install -y nodejs npm nginx mongodb
Check versions:
node -v npm -v nginx -v

Step 3: Set Up Your Node.js App
1. Clone your project or upload it:
git clone https://github.com/your-username/your-node-app.git
cd your-node-app
Or upload your project files using SCP or FileZilla.
2. Install dependencies:
npm install
3. Test the app:
npm start
Visit http://your-ec2-public-ip:3000 to make sure it works.
Step 4: Use PM2 to Keep Your App Running
1. Install PM2 globally:
sudo npm install -g pm2
-
- Start your app with PM2:
pm2 start npm --name="backend" -- run start
Save PM2 process and enable startup on boot:
pm2 save
pm2 startup
Follow the printed instructions to complete PM2’s setup.
Step 5: Set Up Nginx as a Reverse Proxy
- Create a config file:
sudo nano /etc/nginx/sites-available/my-app
Paste the config below (replace with your domain or IP):
server { listen 80; server_name your_ec2_ip; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
- Enable the config:
sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Step 6: Set Up MongoDB
MongoDB should already be installed. Let’s start it and connect your app.
- Start MongoDB:
sudo systemctl start mongodb
sudo systemctl enable mongodb
- Connect your app to MongoDB:
In your Node.js code, use:
const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true, });
Replace “mydatabase” with your actual database name.
Step 7: Test Your Setup:
Open your browser and go to your EC2 public IP:
You should see your Node.js app running!
Check PM2 status:
pm2 status
View logs:
pm2 logs
Summary
You’ve just learned how to host Node.js on AWS EC2 with PM2, Nginx, and MongoDB!
This setup gives you:
- Continuous uptime with PM2
- Reverse proxy with Nginx
- Local database with MongoDB
- Complete control over your app
Bonus Tips
Enable HTTPS with Let’s Encrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
- Use a domain name instead of an IP
- Use MongoDB Atlas for cloud-hosted DB
- Add GitHub Actions for CI/CD deployments
FAQ: Host a Node.js app on AWS EC2 with Nginx, PM2, and MongoDB
1. I’m completely new — is EC2 a good option for me?
Definitely! AWS EC2 is fantastic for new learners trying to learn backend deployment. EC2 gives you complete control of your server and it is free for 12 months (with the t2.micro instance). Just don’t forget to shut it down when you finish testing.
2. Why PM2? Isn’t the npm start command enough?
Yes, npm start works, but as soon as you close your terminal (or the server restarts), the application dies. PM2 allows the application to consistently run in the background, even after crashes or restarts. It’s a babysitter for your Node.js app.
3. Why do we have Nginx if the app is running on port 3000?
Nginx is a reverse proxy, which means it directs incoming web traffic (port 80) to your Node.js app (port 3000).
Bonus: It will performance help, security help and you’ll need it to configure later for HTTPS with a domain.
4. MongoDB won’t start. What should I do?
First, run:
sudo systemctl status mongodb
If it’s not running, start it:

DevOps Associate with a strong passion for automation, cloud technologies, and streamlining deployment workflows to enhance efficiency and reliability