AskHandle Blog
How to Deploy a Simple Node.js App to AWS EC2?
- NodeJS
- EC2
- Deploying

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:
1chmod 400 your-key.pemConnect:
Amazon Linux
1ssh -i your-key.pem ec2-user@your-ec2-public-dnsUbuntu
1ssh -i your-key.pem ubuntu@your-ec2-public-dns3. Install Node.js and npm
We install Node 18 LTS using NodeSource.
Amazon Linux 2
Update packages:
1sudo yum update -yInstall Node.js 18:
1curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
2sudo yum install -y nodejsUbuntu
Update:
1sudo apt update -yInstall Node.js 18:
1curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
2sudo apt install -y nodejsVerify Installation
1node -v
2npm -v4. Upload Your Node.js App to EC2
You have 3 options:
Option A — Upload via SCP
1scp -i your-key.pem -r /local/path/to/app ec2-user@your-ec2-public-dns:~/appOption B — Clone From GitHub
1git clone https://github.com/your/repo.git appOption C — Use SFTP GUI Tools
(e.g., FileZilla, WinSCP)
5. Install Dependencies
Navigate into the project:
1cd ~/appInstall packages:
1npm installEnsure package.json has a start script:
1"scripts": {
2 "start": "node app.js"
3}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
1sudo npm install -g pm2Start your app
1pm2 start app.jsEnable auto-start on OS reboot
1pm2 startup systemd -u ec2-user --hp /home/ec2-user
2pm2 save(If using Ubuntu, replace user with ubuntu.)
Check running processes
1pm2 status7. 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:
1sudo amazon-linux-extras install nginx1 -y
2sudo systemctl start nginx
3sudo systemctl enable nginxUbuntu:
1sudo apt install nginx -y
2sudo systemctl enable nginx
3sudo systemctl start nginxCreate the Proxy Configuration
Amazon Linux
Create a new config file:
1sudo nano /etc/nginx/conf.d/nodeapp.confUbuntu
Edit the default site:
1sudo nano /etc/nginx/sites-available/defaultPaste this updated configuration:
1server {
2 listen 80;
3 server_name _;
4
5 location / {
6 proxy_pass http://localhost:3000;
7 proxy_http_version 1.1;
8 proxy_set_header Upgrade $http_upgrade;
9 proxy_set_header Connection keep-alive;
10 proxy_set_header Host $host;
11 proxy_cache_bypass $http_upgrade;
12 }
13}Save and exit.
Test Nginx configuration:
1sudo nginx -tRestart Nginx:
1sudo systemctl restart nginxNow open: http://your-ec2-public-ip
Your app is live 🎉
8. Optional but Recommended: Enable HTTPS (Let’s Encrypt)
Install Certbot:
Amazon Linux:
1sudo yum install certbot python3-certbot-nginx -yUbuntu:
1sudo apt install certbot python3-certbot-nginx -yRun:
1sudo certbot --nginxFollow the prompts to enable HTTPS.
9. Hardening & Maintenance Tips
Firewall (Ubuntu only)
1sudo ufw allow OpenSSH
2sudo ufw allow 'Nginx Full'
3sudo ufw enableKeep server updated
1sudo yum update -y # Amazon Linux
2sudo apt upgrade -y # UbuntuLog rotation
PM2 handles logs automatically:
1pm2 logs
2pm2 flushFinal 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