How to Deploy a Node.js App to AWS EC2
Deploying a Node.js application to AWS EC2 gives you full control over your server environment, enabling scalable, flexible infrastructure for your backend or web applications. This guide walks you through each step—from launching your EC2 instance to making your app publicly accessible with Nginx and PM2.
1. Launching an EC2 Instance
Step 1 — Create/Log in to AWS
Go to the AWS Management Console → open EC2.
Step 2 — Launch a New Instance
-
Click Launch Instance
-
Choose an OS:
- Amazon Linux 2 (recommended)
- Ubuntu 22.04 LTS
-
Instance type: t2.micro (Free Tier eligible)
-
Key pair: Create or select an existing .pem key pair (you’ll need this to SSH into your server).
Step 3 — Configure Security Group
Allow the following inbound rules:
| Port | Protocol | Purpose |
|---|---|---|
| 22 | SSH | For remote login |
| 80 | HTTP | Web access |
| 443 | HTTPS | Secure web access |
| 3000 (optional) | HTTP | If testing app directly |
You can remove port 3000 later after Nginx is set up.
Launch the instance and wait for initialization. Note the Public IPv4 address or Public DNS.
2. Connecting to the EC2 Instance
From your computer terminal:
Set the key file permissions:
Bash
Connect:
Amazon Linux
Bash
Ubuntu
Bash
3. Install Node.js and npm
We install Node 18 LTS using NodeSource.
Amazon Linux 2
Update packages:
Bash
Install Node.js 18:
Bash
Ubuntu
Update:
Bash
Install Node.js 18:
Bash
Verify Installation
Bash
4. Upload Your Node.js App to EC2
You have 3 options:
Option A — Upload via SCP
Bash
Option B — Clone From GitHub
Bash
Option C — Use SFTP GUI Tools
(e.g., FileZilla, WinSCP)
5. Install Dependencies
Navigate into the project:
Bash
Install packages:
Bash
Ensure package.json has a start script:
Json
6. Run the App with PM2 (Process Manager)
Running node app.js manually stops when you close SSH.
So install PM2, a production-ready process manager.
Install PM2
Bash
Start your app
Bash
Enable auto-start on OS reboot
Bash
(If using Ubuntu, replace user with ubuntu.)
Check running processes
Bash
7. Configure Nginx as a Reverse Proxy
Your Node.js app likely runs on port 3000, but websites use port 80/443.
Nginx will:
- Accept requests on port 80
- Forward them to port 3000
Install Nginx:
Amazon Linux:
Bash
Ubuntu:
Bash
Create the Proxy Configuration
Amazon Linux
Create a new config file:
Bash
Ubuntu
Edit the default site:
Bash
Paste this updated configuration:
Nginx
Save and exit.
Test Nginx configuration:
Bash
Restart Nginx:
Bash
Now open: http://your-ec2-public-ip
Your app is live 🎉
8. Optional but Recommended: Enable HTTPS (Let’s Encrypt)
Install Certbot:
Amazon Linux:
Bash
Ubuntu:
Bash
Run:
Bash
Follow the prompts to enable HTTPS.
9. Hardening & Maintenance Tips
Firewall (Ubuntu only)
Bash
Keep server updated
Bash
Log rotation
PM2 handles logs automatically:
Bash
Final Result
You now have:
- ✔ Node.js installed
- ✔ Your app uploaded and dependencies installed
- ✔ PM2 managing your app on startup
- ✔ Nginx reverse proxy routing traffic
- ✔ HTTPS enabled via Let’s Encrypt (optional)
- ✔ A production-ready environment on EC2












