AskHandle

AskHandle Blog

How to Deploy a Simple Node.js App to AWS EC2?

November 18, 2025Elise Taylor3 min read
  • 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

  1. Click Launch Instance

  2. Choose an OS:

    • Amazon Linux 2 (recommended)
    • Ubuntu 22.04 LTS
  3. Instance type: t2.micro (Free Tier eligible)

  4. 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:

PortProtocolPurpose
22SSHFor remote login
80HTTPWeb access
443HTTPSSecure web access
3000 (optional)HTTPIf 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
1chmod 400 your-key.pem

Connect:

Amazon Linux

bash
1ssh -i your-key.pem ec2-user@your-ec2-public-dns

Ubuntu

bash
1ssh -i your-key.pem ubuntu@your-ec2-public-dns

3. Install Node.js and npm

We install Node 18 LTS using NodeSource.


Amazon Linux 2

Update packages:

bash
1sudo yum update -y

Install Node.js 18:

bash
1curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
2sudo yum install -y nodejs

Ubuntu

Update:

bash
1sudo apt update -y

Install Node.js 18:

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

Verify Installation

bash
1node -v
2npm -v

4. Upload Your Node.js App to EC2

You have 3 options:

Option A — Upload via SCP

bash
1scp -i your-key.pem -r /local/path/to/app ec2-user@your-ec2-public-dns:~/app

Option B — Clone From GitHub

bash
1git clone https://github.com/your/repo.git app

Option C — Use SFTP GUI Tools

(e.g., FileZilla, WinSCP)

5. Install Dependencies

Navigate into the project:

bash
1cd ~/app

Install packages:

bash
1npm install

Ensure package.json has a start script:

json
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

bash
1sudo npm install -g pm2

Start your app

bash
1pm2 start app.js

Enable auto-start on OS reboot

bash
1pm2 startup systemd -u ec2-user --hp /home/ec2-user
2pm2 save

(If using Ubuntu, replace user with ubuntu.)

Check running processes

bash
1pm2 status

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
1sudo amazon-linux-extras install nginx1 -y
2sudo systemctl start nginx
3sudo systemctl enable nginx

Ubuntu:

bash
1sudo apt install nginx -y
2sudo systemctl enable nginx
3sudo systemctl start nginx

Create the Proxy Configuration

Amazon Linux

Create a new config file:

bash
1sudo nano /etc/nginx/conf.d/nodeapp.conf

Ubuntu

Edit the default site:

bash
1sudo nano /etc/nginx/sites-available/default

Paste this updated configuration:

nginx
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:

bash
1sudo nginx -t

Restart Nginx:

bash
1sudo systemctl restart nginx

Now open: http://your-ec2-public-ip

Your app is live 🎉

Install Certbot:

Amazon Linux:

bash
1sudo yum install certbot python3-certbot-nginx -y

Ubuntu:

bash
1sudo apt install certbot python3-certbot-nginx -y

Run:

bash
1sudo certbot --nginx

Follow the prompts to enable HTTPS.

9. Hardening & Maintenance Tips

Firewall (Ubuntu only)

bash
1sudo ufw allow OpenSSH
2sudo ufw allow 'Nginx Full'
3sudo ufw enable

Keep server updated

bash
1sudo yum update -y  # Amazon Linux
2sudo apt upgrade -y # Ubuntu

Log rotation

PM2 handles logs automatically:

bash
1pm2 logs
2pm2 flush

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