AWS Builder Center
Deploy a Node.js Application on AWS EC2: A Step-by-Step Guide

Deploy a Node.js Application on AWS EC2: A Step-by-Step Guide

Learn how to launch an Amazon EC2 instance, configure a Linux server, install Node.js, deploy an Express application, and keep it running using PM2. A practical introduction to cloud deployment for beginners. Copy

Deploy a Node.js Application on AWS EC2: A Step-by-Step Guide

Introduction

Building a web application is only the first step in software development. The next challenge is making it accessible to users over the internet.
When developing applications using Node.js and Express.js, we usually run them on  localhost . However, localhost only allows us to access the application from our own computer.
To make our application publicly accessible, we need to deploy it to a server.
Amazon Web Services (AWS) provides cloud computing services that allow developers to deploy and manage applications without purchasing physical servers.
In this tutorial, we will deploy a simple Node.js application using Amazon Elastic Compute Cloud (EC2).

What Is Amazon EC2?

Amazon EC2 is a cloud computing service that provides virtual servers called instances.
These instances allow developers to run applications in the cloud with configurable computing resources.
Some important features of EC2 include:
  • Scalability: Increase or decrease computing resources according to application requirements.
  • Flexibility: Choose operating systems, instance types, and storage configurations.
  • Security: Control network access using security groups and identity-based permissions.
  • Pay-as-you-go pricing: Pay according to the resources and pricing model you select.
EC2 is commonly used for hosting websites, backend APIs, databases, and other applications.

Prerequisites

Before starting, make sure you have:
  1. An AWS account.
  1. Basic knowledge of JavaScript and Node.js.
  1. A simple Node.js application.
  1. An SSH client.
  1. Git installed, if you plan to clone a GitHub repository.
Important: AWS resources may incur charges. Check the current AWS Free Tier eligibility and pricing, configure billing alerts, and terminate resources when they are no longer required.

Step 1: Create an EC2 Instance

First, sign in to the AWS Management Console.
  1. Search for EC2 in the AWS Console.
  1. Navigate to the EC2 dashboard.
  1. Click Launch instance.
  1. Enter an instance name, such as  nodejs-server .
  1. Select Ubuntu Server as the Amazon Machine Image (AMI).
  1. Choose an instance type suitable for your application and budget.
  1. Create a new key pair or select an existing one.
  1. Configure network settings and storage.
  1. Click Launch instance.
AWS will create a virtual server based on your configuration.
Wait until the instance is running and its status checks have passed.

Step 2: Configure the Security Group

A security group acts as a virtual firewall for your EC2 instance.
For this tutorial, configure the following inbound rules:
Type
Port
Source
SSH
22
Your IP address
Custom TCP
3000
Your IP address for testing
Avoid allowing SSH access from  0.0.0.0/0 , as it unnecessarily exposes your server to the internet.
For a production deployment, use HTTPS through a reverse proxy or load balancer rather than exposing the Node.js application port directly.

Step 3: Connect to Your EC2 Instance

After launching the instance, locate its public IPv4 address.
Open a terminal and navigate to the folder containing your downloaded key pair.
Run:
ssh -i "my-key.pem" ubuntu@YOUR_PUBLIC_IP
Replace:
  •  my-key.pem  with your actual private key filename.
  •  YOUR_PUBLIC_IP  with your instance's public IP address.
On Linux or macOS, you may need to restrict the private key's permissions:
chmod 400 my-key.pem
Once connected, you can execute Linux commands on your cloud server.

Step 4: Update the Server and Install Node.js

First, update the package information and installed packages:
sudo apt update
sudo apt upgrade -y
Install curl:
sudo apt install curl -y
Install a supported Node.js LTS release using the NodeSource repository. The following example uses Node.js 24:
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
For security-sensitive environments, review installation scripts before executing them.
Verify the installation:
node -v
npm -v
If both commands display version numbers, Node.js and npm are installed successfully.

Step 5: Create a Simple Node.js Application

Create a project directory:
mkdir nodejs-app
cd nodejs-app
Initialize a Node.js project:
npm init -y
Install Express.js:
npm install express
Create a file named  server.js :
const express = require("express");

const app = express();
const PORT = process.env.PORT || 3000;

app.get("/", (req, res) => {
res.send("Hello from AWS EC2!");
});

app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on port ${PORT}`);
});
This application creates a simple Express server that responds with a message when someone visits the root URL.
Start the application:
node server.js
The application will start listening on port 3000.

Step 6: Access the Application

Open a browser on a computer whose IP address is allowed by the security group.
Enter:
http://YOUR_PUBLIC_IP:3000
You should see:
Hello from AWS EC2!
Congratulations! Your Node.js application is now running on an AWS EC2 instance.
However, there is one issue.
If you stop the Node.js process or terminate the SSH session, your application may stop running.
To address this, we can use a process manager.

Step 7: Keep the Application Running Using PM2

PM2 is a production process manager for Node.js applications.
Install PM2 globally:
sudo npm install -g pm2
Start your application:
pm2 start server.js --name nodejs-app
Check the application status:
pm2 status
View application logs:
pm2 logs nodejs-app
Configure PM2 to restart your application after a system reboot:
pm2 startup
Follow the command displayed by PM2 to enable the startup service.
Then save the current process list:
pm2 save
PM2 can now manage your application process and restart it when configured to do so.

Step 8: Deploy an Existing GitHub Project

If you already have a Node.js application stored on GitHub, you can deploy it instead of creating a new project.
Install Git:
sudo apt install git -y
Clone your repository:
git clone https://github.com/USERNAME/REPOSITORY.git
Navigate into the project:
cd REPOSITORY
Install dependencies:
npm ci
This command expects a compatible  package-lock.json  file. If your project does not have one, use  npm install .
Start the application using the appropriate entry point:
pm2 start server.js --name my-app
Replace  server.js  with your application's actual entry point.
Remember to configure required environment variables securely and never commit passwords, API keys, or other secrets to GitHub.

Step 9: Production Deployment Considerations

Running an application on EC2 is only the beginning. A production deployment requires additional configuration.

1. Configure HTTPS

Use Nginx as a reverse proxy and configure a TLS certificate to enable HTTPS.
This protects data transmitted between users and your application.

2. Configure a Domain Name

Instead of accessing the application through an IP address, you can configure a domain using Amazon Route 53 or another DNS provider.
For a stable endpoint, consider an Elastic IP address or a load balancer. Check the associated AWS charges.

3. Manage Environment Variables

Store application configuration and secrets outside your source code.
For sensitive values, consider AWS Systems Manager Parameter Store or AWS Secrets Manager.

4. Monitor the Application

Amazon CloudWatch can help monitor infrastructure metrics, logs, and operational events.
Configure alerts for important issues such as high CPU usage or application failures.

5. Maintain Server Security

Regularly update system packages, restrict inbound network access, use least-privilege IAM permissions, and avoid running application processes as root.

Understanding the Deployment Architecture

The basic application flow is:
User Browser
|
v
Internet
|
v
EC2 Security Group
|
v
Amazon EC2 Instance
|
v
Node.js + Express
|
v
Application Response
For a production architecture, an HTTPS reverse proxy or load balancer typically sits in front of the Node.js application.

Conclusion

Deploying a Node.js application on AWS EC2 is a practical way to understand cloud computing and backend application hosting.
Through this tutorial, we learned how to launch an EC2 instance, connect using SSH, install Node.js, configure an Express application, and manage its process using PM2.
We also explored important considerations for production deployments, including HTTPS, monitoring, security, and environment configuration.
The next step is to extend this basic setup with automated deployment using GitHub Actions, a reverse proxy, and monitoring.
Happy coding and happy cloud building!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article