• AWS
  • EC2
  • Node.js
  • NGINX
  • PM2
  • DevOps

Deploying Node.js Applications with NGINX and PM2 on EC2

Complete guide to deploying robust Node.js applications using NGINX reverse proxy and PM2 process manager on AWS EC2, including free SSL setup with Certbot

Shahid Monowar

· 3 min read

What you'll learn

  • Put NGINX in front of your Node.js app as a reverse proxy
  • Keep the app running through crashes and reboots with PM2
  • Add free HTTPS with Certbot, including automatic renewal
  • A final checklist and fixes for the most common errors

Introduction to EC2 Deployment

Deploying Node.js applications on AWS EC2 with NGINX and PM2 creates a production-ready environment that's both scalable and reliable. This guide covers:

  • Setting up NGINX as a reverse proxy
  • Managing Node processes with PM2
  • Securing your domain with free SSL certificates
  • Automating service startup

Prerequisites

Before starting, ensure you have:

  • An AWS account with EC2 access
  • A launched EC2 instance (Ubuntu recommended)
  • Node.js installed on your instance
  • A registered domain name (optional but recommended)

Step 1: NGINX Installation and Setup

Installing NGINX

sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx

Basic Configuration

Verify NGINX is running:

sudo systemctl status nginx

You should now be able to access your server's public IP in a browser and see the NGINX welcome page.

Step 2: Configuring NGINX as Reverse Proxy

Edit the default configuration file:

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

Replace the server block content with:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
 
    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;
    }
}

Test and restart NGINX:

sudo nginx -t
sudo systemctl restart nginx

Step 3: PM2 Application Management

Installing and Running Your App

npm install -g pm2
cd /path/to/your/app
pm2 start app.js

Common PM2 Commands

CommandDescription
pm2 listView running processes
pm2 logsShow application logs
pm2 restart app_nameRestart application
pm2 delete app_nameStop and remove application

Setting Up Startup Script

pm2 startup
pm2 save

This ensures your Node application restarts automatically if the server reboots.

Step 4: Securing with SSL (Certbot)

Installation

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Obtaining Certificate

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

Certbot will automatically:

  • Obtain a certificate from Let's Encrypt
  • Configure NGINX to use HTTPS
  • Set up automatic certificate renewal

Automatic Renewal Verification

Test the renewal process:

sudo certbot renew --dry-run

Final Checklist

✅ NGINX installed and running
✅ Reverse proxy properly configured
✅ Node application running under PM2
✅ PM2 startup configured
✅ SSL certificate installed
✅ Automatic renewal working

Troubleshooting Tips

  • 502 Bad Gateway: Check if your Node app is running on the correct port
  • SSL Not Working: Verify DNS records point to your EC2 instance
  • PM2 Not Persisting: Ensure you ran both pm2 startup and pm2 save

Conclusion

You've now set up a production-grade Node.js environment on EC2 with:

  • NGINX handling HTTP/HTTPS traffic
  • PM2 ensuring application availability
  • Free SSL certificates from Let's Encrypt

For further optimization, consider:

  • Setting up a CI/CD pipeline
  • Configuring load balancing
  • Implementing monitoring

🚀 Happy Deployment! 🚀

Available for freelance & contract work

Need this built, or fixed, for your product?

I help teams ship and run production web apps: CRMs, ERPs, booking platforms and SaaS with Next.js, Node.js and PostgreSQL, from the first commit to deployment.

Shahid Monowar

Written by

Shahid Monowar

Full-Stack Engineer building CRMs, ERPs and SaaS products with Next.js, Node.js and PostgreSQL. Based in Dhaka, working with teams worldwide.