πHow I Deployed My Node.js Backend to EC2 with Nginx, SSL & PM2


π§© Introduction
I recently deployed my Node.js backend to an AWS EC2 instance. It runs with Nginx as a reverse proxy, SSL for secure HTTPS, and PM2 to keep the server always running in the background.
In this post, Iβll walk you through the exact steps I followed β so you can do it too.
π§° What You Need Before You Start
β An AWS account with an EC2 instance (Ubuntu recommended)
β A backend Node.js app that listens on port
3000
β A domain name (I used
chatifybackend.sangammundhe.site
)β Your EC2 security group should allow ports:
80
,443
, and optionally3000
π οΈ Step-by-Step Guide (15 Steps)
Hereβs what I did in brief β youβll find the full commands and explanations below π
β Step 1: Launch and Connect to EC2
Spin up an EC2 instance (Ubuntu) and SSH into it:
ssh -i path/to/your-key.pem ubuntu@your-ec2-ip
β Step 2: Install NVM & Node.js
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
β Step 3: Clone Your Project
sudo apt install git -y
git clone https://github.com/your-username/your-backend-repo.git
cd your-backend-repo
npm install
β Step 4: Confirm Port 3000 is Open (Dev Only)
In your EC2 Security Group, allow inbound traffic on port 3000 or any other port as my backend running on port 3000 so i expose it for testing backend directly (optional in production).
β Step 5: Install Nginx
sudo apt update
sudo apt install nginx -y
sudo systemctl status nginx
β Step 6: Open Ports 80 & 443
Go to AWS EC2 β Security Groups β Inbound rules β Add:
HTTP
on port80
HTTPS
on port443
β Step 7: Configure Nginx
Edit the Nginx default config:
sudo nano /etc/nginx/sites-available/default
Replace content with:
server {
listen 80;
server_name chatifybackend.sangammundhe.site;
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;
}
}
Test & restart:
sudo nginx -t
sudo systemctl restart nginx
β Step 8: Map Your Domain to EC2
Go to your domain provider (e.g., GoDaddy, Namecheap) and:
- Add an A record pointing your domain to your EC2 IP.
β Step 9: Install SSL (Letβs Encrypt + Certbot)
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo certbot --nginx -d chatifybackend.sangammundhe.site
Follow the prompts β it will automatically update your Nginx config.
β Step 10: Install PM2
sudo npm install -g pm2
β Step 11: Start the Backend App
pm2 start server.js # or your app's main entry file
β Step 12: Use PM2 for Process Management
pm2 status # See process status
pm2 logs # View logs
pm2 restart <id> # Restart app
pm2 stop <id> # Stop app
Enable auto-start on reboot:
pm2 startup
pm2 save
β Step 13: Your Server Is Now Live
Visit:
https://chatifybackend.sangammundhe.site
Your app should be live, secure, and managed in the background!
π§± Common Issues I Faced
β Couldnβt see Nginx welcome page β fixed by opening port 80 in EC2.
β Backend not accessible β port 3000 wasn't exposed (for dev testing).
β SSL didn't work instantly β had to wait for domain DNS propagation.
β Nginx config error β missed semicolon caused a restart failure.
π§ What I Learned
How to deploy a backend app to production on real infrastructure.
Nginx reverse proxying allows you to use clean domains.
Certbot makes SSL easy and automatic.
PM2 is essential for background process management.
π¬ Final Thoughts
This deployment gave me real-world DevOps experience. Now I can confidently host, secure, and maintain a production Node.js backend. Next up β CI/CD pipeline and auto-deploys!
π Explore More from Me
π Learning Journey & Projects
π https://www.sangammundhe.site/learning-journey
π» GitHub
π https://github.com/Sangam5756
π LinkedIn
π https://www.linkedin.com/in/sangammundhe
π¦ Twitter (X)
π https://twitter.com/sangammundhe
If you enjoyed this post or learned something new, feel free to connect, drop feedback, or just say hi π
Subscribe to my newsletter
Read articles from Sangam Mundhe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
