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-ipExample:
ssh -i mykey.pem [email protected]Step 3: Update Server and Install Node.js
Update packages:
sudo apt update && sudo apt upgrade -yInstall Node.js (using NodeSource):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejsCheck installation:
node -v
npm -vStep 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 installOption 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.jsVisit:
http://your-ec2-ip:3000If 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 pm2Start your app with PM2:
pm2 start app.jsSave process:
pm2 saveEnable auto-start on reboot:
pm2 startupStep 7: Install Nginx
Nginx will handle incoming traffic.
Install Nginx:
sudo apt install nginx -yStart Nginx:
sudo systemctl start nginx
sudo systemctl enable nginxCheck in browser:
http://your-ec2-ipStep 8: Configure Nginx as Reverse Proxy
Edit default config:
sudo nano /etc/nginx/sites-available/defaultReplace 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 nginxNow open:
http://your-ec2-ipNo need to use port 3000 anymore.
Step 9: (Optional) Setup Domain and SSL
If you have a domain:
Point domain to EC2 public IP
Install Certbot:
sudo apt install certbot python3-certbot-nginx -yRun:
sudo certbot --nginxNow your site runs on HTTPS.
Before vs After Deployment
Before:
App runs on local machine only
Stops if terminal closes
No domain or HTTPS
Direct port access (unsafe)
After:
App runs on cloud (AWS EC2)
Always running (PM2)
Clean URL without port (Nginx)
Secure with HTTPS
Common Issues and Fixes
App not loading
Check PM2 logs:
pm2 logsNginx error
Check status:
sudo systemctl status nginxPort not accessible
Check security group rules
Permission issues
Use:
sudo chown -R ubuntu:ubuntu /your-appAdvantages
Reliable production setup
Auto restart using PM2
Improved performance with Nginx
Supports domain and SSL
Scalable architecture
Disadvantages
Initial setup takes time
Requires basic Linux knowledge
Debugging can be tricky for beginners
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.


Join the conversation! Your thoughts help the community grow.