Deploy Node.js App to AWS with PM2, Nginx & Free SSL (Let's Encrypt)
🚀 Deploy Node.js App to AWS EC2 (PM2 + Nginx + SSL)
This guide walks you through deploying a Node.js backend on an AWS EC2 Ubuntu server, managing it with PM2, exposing it via Nginx, and securing it with free SSL using Let’s Encrypt.
✅ Prerequisites
- AWS EC2 instance (Ubuntu)
- A domain/subdomain pointing to your EC2 public IP
- Node.js project (with
index.jsor equivalent entry file) - SSH access to the server
1️⃣ Update Server Packages
sudo apt update
2️⃣ Install NVM (Node Version Manager)
Install NVM to manage Node.js versions safely.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
Load NVM into the current shell (or reopen terminal):
. "$HOME/.nvm/nvm.sh"
3️⃣ Install Node.js (v24.13.0)
nvm install 24.13.0
nvm use 24.13.0
Verify installation:
node -v
npm -v
4️⃣ Clone Your Project
git clone <your-repo-link>
cd <project-folder>
Install dependencies:
npm install
5️⃣ Install PM2 (Process Manager)
PM2 keeps your Node.js app alive and restarts it on crashes.
npm install pm2 -g
pm2 --version
Start your app:
pm2 start index.js
6️⃣ Common PM2 Commands
| Command | Description |
|---|---|
pm2 start <script> | Start the application |
pm2 list | List running applications |
pm2 stop <id/name> | Stop an application |
pm2 restart <id/name> | Restart an application |
pm2 reload <name> | Reload with zero downtime |
pm2 delete <id/name> | Delete an application |
pm2 logs <id/name> | View logs |
pm2 monit | Live monitoring dashboard |
In cluster mode, multiple processes share the same name. Use id to control a single instance.
7️⃣ Install & Configure Nginx (Reverse Proxy)
Install Nginx:
sudo apt install nginx -y
Edit the Nginx configuration:
sudo nano /etc/nginx/nginx.conf
Example Nginx Configuration: (Assuming your app runs on port 8080)
events {
# Event directives...
}
http {
server {
listen 80;
server_name api-brainly.suryakant.run.place;
location / {
proxy_pass http://localhost:8080;
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 reload Nginx:
sudo nginx -t
sudo nginx -s reload
✅ At this point, your app should be live at: http://api-brainly.suryakant.run.place
8️⃣ Add Free SSL (Let’s Encrypt)
Install Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
Generate SSL certificate:
sudo certbot --nginx
Test auto-renewal:
sudo certbot renew --dry-run
🎉 Done! Your API is now secured with HTTPS.