3 min read
Shahid Monowar

Deploying Node.js Applications with NGINX and PM2 on EC2

#AWS#EC2#Node.js#NGINX#PM2#DevOps
Deploying Node.js Applications with NGINX and PM2 on EC2

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! 🚀

About the Author

Shahid Monowar

Shahid Monowar

Full Stack Developer passionate about building modern web applications with React, Next.js, and TypeScript. Always exploring new technologies and sharing knowledge through writing.

Enjoyed this article? Follow me for more insights on web development