How to Deploy a Node.js App on AWS EC2 with PM2 and Nginx

How to Deploy a Node.js App on AWS EC2 with PM2 and Nginx
AuthorBy Atisolve Team· 3 min readDevOps  👁️ 149 views

Deploying a production-grade Node.js app doesn’t have to be complicated. In this guide, we’ll walk you through how we at Atisolve deploy scalable, secure, and high-performance Node.js applications on AWS EC2 using PM2 for process management and Nginx as a reverse proxy.

Whether you’re launching a startup MVP or scaling an enterprise system, this tutorial will get your app live and resilient.

🧰 Prerequisites

Before we begin, make sure you have:

  • An AWS EC2 instance running Ubuntu
  • SSH access to the server
  • Your Node.js app ready (e.g., in GitHub)
  • A domain (optional, for Nginx setup)

🛠️ Step 1: Connect to Your EC2 Instance

 ssh -i your-key.pem ubuntu@your-ec2-ip

Update and install dependencies:

sudo apt update && sudo apt upgrade -y

🛠️ Step 2: Install Node.js and npm

We recommend using NodeSource:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v

🛠️ Step 3: Install Git and Clone Your Project

sudo apt install git -y
git clone https://github.com/your-username/your-node-app.git
cd your-node-app
npm install

🛠️ Step 4: Install and Configure PM2

PM2 is a production-ready process manager for Node.js.

npm install -g pm2
pm2 start index.js --name my-app
pm2 save
pm2 startup

Copy the command it outputs and run it (to auto-start on reboot).

You can also view logs:

pm2 logs my-app

🛠️ Step 5: Install and Configure Nginx

sudo apt install nginx -y

Create a new Nginx site config:

sudo nano /etc/nginx/sites-available/my-app

Paste the following (update your domain/IP):

server {
  listen 80;
  server_name yourdomain.com;

  location / {
    proxy_pass http://localhost:3000; # or your app's port
    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 the site and restart Nginx:

sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

🔐 (Optional) Enable HTTPS with Let’s Encrypt

Install Certbot:

 sudo apt install certbot python3-certbot-nginx -y
 sudo certbot --nginx -d yourdomain.com

Auto-renew:

sudo crontab -e

Add this line:

0 0 * * * /usr/bin/certbot renew --quiet

✅ You’re Live!

Your Node.js app is now:

  • Managed by PM2
  • Served through Nginx
  • (Optionally) secured with HTTPS

🧠 Pro Tips from Atisolve

  • Use .env files for secrets and configs
  • Use MongoDB Atlas or Amazon RDS for database
  • Enable PM2 monitoring dashboard:
 pm2 monitor
  • Use fail2ban or UFW for added security

If you want to deploy your app securely, with CI/CD, autoscaling, and monitoring — we at Atisolve can help. Contact us for a free deployment consultation.

👉 Get in touch

WhatsApp Icon

Have a vision? Let’s bring it to life.

Whether it’s an app, website, or platform—your idea deserves expert execution. Let us help you build it from the ground up with clarity, care, and speed.