Introduction
Deploying a Node.js application to production can feel confusing at first, especially if you are new to cloud platforms like AWS. But once you understand the flow, it becomes a repeatable and simple process.
In this guide, you will learn how to deploy a Node.js app on AWS EC2 using Nginx as a reverse proxy and PM2 as a process manager. We will use simple language, real-world examples, and practical steps so that you can follow along easily.
This setup is commonly used in real-world production environments because it is reliable, scalable, and cost-effective.
What You Will Learn
How to launch an EC2 instance on AWS
How to install Node.js, Nginx, and PM2
How to deploy your Node.js application
How to configure Nginx as a reverse proxy
How to keep your app running using PM2
Real-World Analogy
Think of your application like a restaurant:
Node.js → The kitchen (where food is prepared)
PM2 → The manager (ensures kitchen keeps running)
Nginx → The waiter (handles customer requests and serves responses)
AWS EC2 → The building (where everything runs)
Without Nginx and PM2, your system becomes unstable and hard to manage.
Step 1: Launch an AWS EC2 Instance
First, you need a virtual server.
Steps:
Go to AWS Console → EC2
Click on "Launch Instance"
Choose Ubuntu Server (recommended for beginners)
Select instance type (t2.micro is free tier eligible)
Configure security group:
Allow SSH (port 22)
Allow HTTP (port 80)
Allow HTTPS (port 443)
After launching, download your .pem key file.
Step 2: Connect to EC2 Server
Use SSH to connect:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
Example:
ssh -i mykey.pem [email protected]
Step 3: Update Server and Install Node.js
Update packages:
sudo apt update && sudo apt upgrade -y
Install Node.js (using NodeSource):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Check installation:
node -v
npm -v
Step 4: Upload Your Node.js Application
You can upload your app in multiple ways:
Option 1: Using Git
git clone https://github.com/your-repo.git
cd your-repo
npm install
Option 2: Using SCP
scp -i your-key.pem -r ./your-app ubuntu@your-ip:/home/ubuntu/
Step 5: Run Your App (Test)
Start your app manually:
node app.js
Visit:
http://your-ec2-ip:3000
If it works, you are ready for production setup.
Step 6: Install PM2 (Process Manager)
PM2 keeps your app running even if it crashes.
Install PM2:
sudo npm install -g pm2
Start your app with PM2:
pm2 start app.js
Save process:
pm2 save
Enable auto-start on reboot:
pm2 startup
Step 7: Install Nginx
Nginx will handle incoming traffic.
Install Nginx:
sudo apt install nginx -y
Start Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
Check in browser:
http://your-ec2-ip
Step 8: Configure Nginx as Reverse Proxy
Edit default config:
sudo nano /etc/nginx/sites-available/default
Replace with:
server {
listen 80;
server_name your_domain_or_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;
}
}
Restart Nginx:
sudo systemctl restart nginx
Now open:
http://your-ec2-ip
No need to use port 3000 anymore.
Step 9: (Optional) Setup Domain and SSL
If you have a domain:
sudo apt install certbot python3-certbot-nginx -y
Run:
sudo certbot --nginx
Now your site runs on HTTPS.
Before vs After Deployment
Before:
After:
Common Issues and Fixes
App not loading
pm2 logs
Nginx error
sudo systemctl status nginx
Port not accessible
Permission issues
sudo chown -R ubuntu:ubuntu /your-app
Advantages
Disadvantages
Final Thoughts
Deploying a Node.js application on AWS EC2 with Nginx and PM2 is one of the most practical and industry-used approaches.
Once you understand this setup, you can deploy almost any backend application confidently.
Start simple, follow each step carefully, and soon this entire process will feel like a routine DevOps task.