6 min readBy Iftekhar Priyo
Deploying Your First Node.js App to AWS EC2
Learn how to deploy your first Node.js application to an Ubuntu EC2 server. We'll launch an instance, install Node.js, configure environment variables, and keep the app running with PM2.
- AWS
- Cloud
- EC2
- Linux

Introduction
You've finished building your application.
On your computer, you probably start it with something like:
npm install
npm run devEverything works perfectly.
But your computer isn't meant to stay online all day, every day. If you shut it down or lose internet access, nobody can use your application.
That's where cloud servers come in.
In this guide, we'll deploy our application to an AWS EC2 instance, which is simply another computer running in the cloud.
In this article we'll learn
- Launch an EC2 instance
- Connect to it
- Install Node.js
- Clone our project
- Configure environment variables
- Run the application
- Keep it running with PM2
We won't cover Nginx, domains, HTTPS, or CI/CD yet. Those deserve their own articles.
What is an EC2 Instance?
Think of an EC2 instance as another computer. The only difference is that instead of sitting on your desk, it runs inside an AWS data center and stays connected to the internet 24/7.
If you know how to use a Linux computer, you're already halfway to understanding EC2.
π What's happening behind the scenes?
AWS creates a virtual machine for you. You choose its operating system, CPU, memory, and storage, then connect to it over the internet just like a remote computer.
Before We Start
We'll assume:
- You already have a Node.js project.
- Your code is on GitHub.
- Your application runs locally.
- You know how to run
npm installandnpm run dev.
Step 1 β Launch an Ubuntu EC2 Instance
Create a new Ubuntu EC2 instance from the AWS Console.
While creating it, you'll choose:
- Instance name
- Ubuntu image
- Instance type
- Key Pair
- Security Group
Security Group
A Security Group is like the firewall of your EC2 server. It decides who can communicate with your server and through which ports.
For now, allow:
- SSH (22)
- Your application port (for example, 3000)
We'll make this more secure later when we introduce Nginx.
Step 2 β Connect to Your Server
You can connect using EC2 Instance Connect or SSH.
Connecting to an EC2 instance is similar to remotely controlling another computer. Instead of sitting in front of it, you open a secure terminal and execute commands from wherever you are.
Example:
ssh -i key.pem ubuntu@YOUR_EC2_PUBLIC_IPNote that, the next steps that are described down below, are inside the SSH tunnel.
Step 3 β Update Ubuntu
sudo apt update
sudo apt upgrade -yIf you're new to Linux:
sudomeans "run this command as administrator."aptis Ubuntu's package manager.-yautomatically answers "Yes" when Ubuntu asks for confirmation.
Step 4 β Install Git & Node.js
A new EC2 instance starts almost empty.
Just like a new laptop, we need to install the software our project depends on before it can run.
Install:
- Git
- Node.js (using NVM)
Verify the installation:
node -v
npm -vStep 5 β Clone Your Project
git clone YOUR_REPOSITORY_URL
cd your-projectInstead of copying files manually, we simply download our project from GitHub. This also makes future updates much easier.
Step 6 β Install Dependencies
npm installYou already ran this on your own computer, but this EC2 instance is a completely different machine.
Since it doesn't have your project's packages yet, we need to install them again.
π What's happening behind the scenes?
npm reads your
package.json, downloads every required package from the npm registry, and creates thenode_modulesfolder on your server.
Step 7 β Configure Environment Variables
Create a .env file.
nano .envExample:
PORT=3000
DATABASE_URL=...
JWT_SECRET=...Your application may need secrets such as database passwords or API keys.
We keep them inside .env instead of GitHub because every environment (local, staging, production) usually has different values.
Step 8 β Test the Application
Start the application normally.
npm run startThen visit:
http://YOUR_PUBLIC_IP:3000
Before introducing extra tools, it's always a good idea to make sure the application works normally.
If it doesn't work here, PM2 won't magically fix it.
Why Isn't npm run start Enough?
When you run:
npm run startthe application stays attached to your current terminal.
If you disconnect from SSH or close the terminal, the application usually stops running too.
That's not ideal for a production server.
Step 9 β Install PM2
PM2 is like a supervisor for your Node.js application.
Instead of simply starting your application, it keeps it running in the background and makes it easy to restart or monitor later.
Install PM2:
npm install -g pm2Start your project:
pm2 start npm --name my-app -- run startUseful commands:
pm2 list
pm2 logs
pm2 restart my-app
pm2 stop my-app
pm2 delete my-appSave the process list:
pm2 saveEnable PM2 after server reboot:
pm2 startupRun the command PM2 gives you, then execute:
pm2 savenpm run vs PM2
npm run start | PM2 |
|---|---|
| Starts the application | Manages the application |
| Stops when the terminal closes | Keeps running in the background |
| Best for development | Best for servers |
Think of npm run start as starting your car yourself every time.
PM2 is like someone who keeps the engine running and starts it again automatically if it stops.
Common Beginner Questions
Why did my application stop?
Because you closed the terminal or disconnected from SSH before using PM2.
Where should I put my .env file?
Inside your project's root directory.
Why do I need to run npm install again?
Because your EC2 server is a brand-new computer.
Why can't I open my application?
Check:
- Is the application running?
- Did you open the correct port?
- Is the Security Group configured correctly?
Wrapping Up
Congratulations!
Your application is now running on an AWS EC2 server.
Right now, people still need to visit something like:
http://YOUR_PUBLIC_IP:3000
It worksβbut it doesn't look very professional yet.
In the next article, we'll install Nginx so visitors can access your application through a proper domain without typing a port number. Later on, I will explain how I configured my blog site's infrastructure on cloud, so we get a broader perspective.
Share this article
Send it to someone who might find it useful.