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

Sangam MundheSangam Mundhe
4 min read

🧩 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 optionally 3000

πŸ› οΈ 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 port 80

  • HTTPS on port 443

βœ… 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 πŸ‘‹


0
Subscribe to my newsletter

Read articles from Sangam Mundhe directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sangam Mundhe
Sangam Mundhe