Introduction
Deploying a Node.js application to production can feel confusing at first, especially if you are new to cloud platforms like AWS. But once you understand the flow, it becomes a repeatable and simple process.
In this guide, you will learn how to deploy a Node.js app on AWS EC2 using Nginx as a reverse proxy and PM2 as a process manager. We will use simple language, real-world examples, and practical steps so that you can follow along easily.
This setup is commonly used in real-world production environments because it is reliable, scalable, and cost-effective.
What You Will Learn
How to launch an EC2 instance on AWS
How to install Node.js, Nginx, and PM2
How to deploy your Node.js application
How to configure Nginx as a reverse proxy
How to keep your app running using PM2
Real-World Analogy
Think of your application like a restaurant:
Node.js → The kitchen (where food is prepared)
PM2 → The manager (ensures kitchen keeps running)
Nginx → The waiter (handles customer requests and serves responses)
AWS EC2 → The building (where everything runs)
Without Nginx and PM2, your system becomes unstable and hard to manage.
Step 1: Launch an AWS EC2 Instance
First, you need a virtual server.
Steps:
Go to AWS Console → EC2
Click on "Launch Instance"
Choose Ubuntu Server (recommended for beginners)
Select instance type (t2.micro is free tier eligible)
Configure security group:
Allow SSH (port 22)
Allow HTTP (port 80)
Allow HTTPS (port 443)
After launching, download your .pem key file.
Step 2: Connect to EC2 Server
Use SSH to connect:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
Example:
ssh -i mykey.pem [email protected]
Step 3: Update Server and Install Node.js
Update packages:
sudo apt update && sudo apt upgrade -y
Install Node.js (using NodeSource):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Check installation:
node -v
npm -v
Step 4: Upload Your Node.js Application
You can upload your app in multiple ways:
Option 1: Using Git
git clone https://github.com/your-repo.git
cd your-repo
npm install
Option 2: Using SCP
scp -i your-key.pem -r ./your-app ubuntu@your-ip:/home/ubuntu/
Step 5: Run Your App (Test)
Start your app manually:
node app.js
Visit:
http://your-ec2-ip:3000
Join the conversation! Your thoughts help the community grow.