This guide will walk you through the steps to deploy a Node.js application on an AWS EC2 Linux instance.
- An AWS account
- Basic knowledge of Node.js
- A Node.js application to deploy
- Log in to your AWS account.
- Navigate to the EC2 service by typing "EC2" in the services search bar.
- Select Launch Instance.
- Name your instance, for example:
nodejs_AWS. - Choose the appropriate AMI (Amazon Machine Image). In this case, select Ubuntu or any Linux-based AMI.
- Configure your instance, select t2.micro or as per your needs.
- In the Key pair section, create or choose a key pair to access the instance later.
- Allow HTTP access from the internet.
- Ensure your security group allows port 3000 (or whatever port your Node.js application uses) for TCP traffic.
Once your instance is running:
-
Open the terminal and SSH into the instance:
ssh -i your-key-file.pem ubuntu@your-ec2-public-ip
Switch to the superuser:
sudo su- Install Node.js and Git
- Install Git:
sudo apt updatesudo apt install git- Install Node.js using Node Version Manager (nvm):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bashsource ~/.nvm/nvm.shnvm install 16Verify the installation:
node -v
git --version- Clone the Repository Clone your Node.js application from GitHub:
git clone https://github.com/your-username/your-nodejs-repo.gitcd your-nodejs-repo- Install Dependencies Install the required dependencies:
npm install- Start the Application Run the application:
node index.jsYour application should now be running. Verify by visiting http://your-ec2-public-ip:3000 in your browser.
- Configure Security Group for Port 3000 If you haven't already configured security groups:
Go to EC2 Dashboard.
- Select your running instance
- Navigate to Security Groups.
- Edit Inbound Rules and allow Custom TCP on Port 3000 from Anywhere.
- Run the Application with PM2
To keep your application running even after disconnecting from the terminal, install and use PM2:
Install PM2 globally:
npm install pm2 -gStart your app with PM2:
pm2 start index.jsVerify that the app is running with:
pm2 listNow, even if you disconnect from the SSH session, your application will continue running.

