Back to Blog
Node.jsAWSEC2UbuntuPM2

How to Deploy Node Express API to an AWS EC2 Ubuntu Instance

August 12, 2021 4 min read Read on Medium

WWYL (what will you learn):

  1. Host a NODE API on AWS EC2 instance
  2. Access NODE API from a web application

Watch the video tutorial

Pre-requirements:

  • Node Express API in a GitHub repository
  • AWS account (free tier available at aws.amazon.com/free)

Step 1: Create EC2 Instance in AWS

Launch Instance

  • Navigate to AWS EC2 and click "Launch an instance"

Name and Select Ubuntu, free tier

  • Give your instance a name and select Ubuntu as the OS
  • Confirm it's Free tier eligible
  • Create a new Key Pair — this is the security certificate file you'll need for SSH access

Click Create New Key Pair

Create a new key pair prompt

Create new key pair if needed

IMPORTANT: Download and securely save your Key Pair file (.cer or .pem) — you cannot download it again after creation. If you lose it, you won't be able to SSH into the instance.

  • Leave the rest of the settings at defaults and click "Launch instance"

Find the new instance

Step 2: Connect to EC2 Instance via SSH

Connect to Ubuntu EC2

  1. Locate your running instance in the EC2 console
  2. Click "Connect" and select the SSH client tab

Select SSH client instructions

  1. AWS will show you the exact command to run — follow those instructions:

First, set correct permissions on your key file:

chmod 400 new-key-pair.cer

chmod 400 on key pair

Then connect via SSH:

ssh -i "new-key-pair.cer" ubuntu@ec2-54-210-98-114.compute-1.amazonaws.com
  1. When prompted with "Are you sure you want to continue connecting?", type yes

Connected - Ubuntu prompt

Step 3: Install Node and NPM on the EC2 Instance

Once connected, run these commands on the EC2 instance:

# Install curl sudo apt-get install curl # Download the Node.js setup script curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - # Install Node.js sudo apt-get install nodejs # Verify installation node -v npm -v

Step 4: Clone Your API from GitHub

# Use HTTPS — not SSH — for cloning git clone 'your-repo-link' # Go into the cloned folder cd your-repo-folder # Install dependencies npm install

Step 5: Configure Security Group to Allow API Traffic

Custom TCP security group rule

  1. In the EC2 console, select your instance
  2. Go to the Security tab and click on the security group
  3. Click "Edit inbound rules" → "Add rule"
  4. Set type to Custom TCP, port to whatever your API uses (e.g., 4000), source to Anywhere (0.0.0.0/0)
  5. Save the rule

Public IP address of EC2

Find your instance's Public IPv4 address in the instance details panel. Your API will be reachable at:

http://[public-ip]:[port]/

Step 6: Test Your API

node your-folder/index.js

Open http://[public-ip]:4000/ in a browser to confirm it's responding.

API running on port 4000

Important: Keep Your API Running with PM2

The problem: The moment you close your terminal or disconnect from SSH, your node process dies. The API goes offline.

The solution: PM2 is a process manager that keeps your Node app running in the background — even after you disconnect, and even after the server reboots.

Watch the PM2 setup video

# Install PM2 globally npm install pm2@latest -g # Start your API with PM2 pm2 start your-folder/index.js # Check that it's running pm2 list # Optional: configure PM2 to restart on server reboot pm2 startup pm2 save

[Screenshot: Terminal showing pm2 list output with the API process running and status "online"]

Once PM2 is managing your process, you can safely close your terminal. The API stays up.