Sitemap

Deploying a Node.js Backend on AWS EC2 with PM2, Nginx, and SSL (Complete Guide)

3 min readSep 13, 2025

If you’re running a Node.js backend, deploying it properly on a production server is one of the most important steps. In this article, we’ll walk through deploying a Node.js API on AWS EC2 (Ubuntu), running it with PM2, configuring Nginx as a reverse proxy, pointing your custom domain, and securing everything with free SSL (HTTPS).

By the end, your backend will be accessible at: https://api.yourdomain.com

Why AWS EC2, PM2, and Nginx?

  • AWS EC2 → Flexible, scalable, reliable cloud server
  • PM2 → Keeps your Node.js process alive even after reboots, crashes, or SSH disconnects
  • Nginx → Acts as a reverse proxy to handle traffic, improves performance, and allows domain + SSL integration
  • Certbot (SSL) → Free HTTPS certificates from Let’s Encrypt
Press enter or click to view image in full size

🔑 Step 1: Launch an EC2 Instance

  1. Go to AWS EC2 Dashboard.
  2. Launch an instance with:
  • OS: Ubuntu 20.04 / 22.04
  • Type: t2.micro (Free Tier) or higher

3. Download the .pem key (keep it safe).

4. Open Security Groups → Add inbound rules:

  • 22 (SSH)
  • 80 (HTTP)
  • 443 (HTTPS)

⚙️ Step 2: Connect to Server via SSH

Open terminal and connect:

ssh -i ~/Desktop/deploy.pem ubuntu@YOUR_SERVER_IP

Example:
ssh -i ~/Desktop/deploy.pem ubuntu@78.171.190.95

📦 Step 3: Update System Packages

sudo apt update -y
sudo apt upgrade -y

🟢 Step 4: Install Node.js, npm, and PM2

sudo apt install curl git nginx -y
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install -g pm2

Check versions:

node -v
npm -v
pm2 -v

📥 Step 5: Clone Your Backend Project

cd ~
git clone https://github.com/your-repo/backend.git
cd backend

📚 Step 6: Install Dependencies

npm install

🔐 Step 7: Configure Environment Variables

Create .env:

nano .env

Example:

PORT=5000
DB_HOST=localhost
DB_USER=root
DB_PASS=secret

Save → CTRL + O, Enter → Exit CTRL + X

▶️ Step 8: Run Backend with PM2

Start the backend:

pm2 start index.js --name backend --watch
pm2 save
pm2 startup systemd

Follow PM2’s startup command (example):

sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u ubuntu --hp /home/ubuntu

Check processes:

pm2 list

🌐 Step 9: Configure Nginx as Reverse Proxy

Create a new config file:

sudo nano /etc/nginx/sites-available/api

Paste this:

server {
listen 80;
server_name api.yourdomain.com;


location / {
proxy_pass http://127.0.0.1:5000;
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 config:

sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx

🌍 Step 10: Point Domain to AWS

Go to your domain registrar (GoDaddy, Namecheap, Cloudflare, etc.) → Add DNS record:

🔒 Step 11: Enable HTTPS (SSL Certificate)

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Generate SSL:

sudo certbot --nginx -d api.yourdomain.com

Choose redirect HTTP → HTTPS

Now your backend is secured with SSL.

✅ Step 12: Verify Deployment

Visit:

https://api.yourdomain.com

Your backend is now running live with HTTPS, powered by Node.js, PM2, Nginx, and AWS EC2 🎉

🛠 Useful PM2 Commands

  • Restart app:
  • pm2 restart backend
  • Stop app:
  • pm2 stop backend
  • Delete app:
  • pm2 delete backend
  • View logs:
  • pm2 logs backend

🎯 Final Thoughts

Deploying a Node.js backend on AWS EC2 may sound complex, but once you’ve gone through these steps, the process becomes straightforward. With PM2 ensuring uptime, Nginx handling requests, and SSL certificates securing communication, your backend is production-ready.

This setup is scalable, reliable, and cost-effective for small to medium projects.

👉 Save this guide for your future deployments, and share it with fellow developers who want a simple A to Z deployment tutorial.

--

--

Muhammad Huzaifa
Muhammad Huzaifa

Written by Muhammad Huzaifa

Full-stack developer sharing insights on code, startups, and learning in public.