Deploying a Node.js Application on AWS EC2
A practical guide to deploying a Node.js and Express application on Amazon EC2, including server setup, security groups, process management, and basic production configuration.
Deploying a Node.js Application on AWS EC2
Deploying a web application for the first time can feel complicated, especially when moving from a local development environment to a cloud server. In this guide, we will deploy a simple Node.js and Express application on Amazon EC2.
The goal is to understand the basic deployment workflow rather than relying on platform-specific deployment services.
Architecture
The deployment will use:
- Amazon EC2 — Hosts the Node.js application
- Node.js — Runs the backend
- Express.js — Web framework
- Security Groups — Control incoming network traffic
- PM2 — Keeps the Node.js application running
The basic flow is:
1
2
3
4
5
6
7
8
9
10
User
|
v
Internet
|
v
Amazon EC2
|
v
Node.js + Express Application
1. Create an EC2 Instance
Open the AWS Management Console and navigate to EC2.
Create a new instance with:
- Amazon Linux
- A suitable instance type for your workload
- An SSH key pair
- A security group
For a learning project, a small instance is usually sufficient.
When configuring the security group, allow:
- SSH — Port
22 - HTTP — Port
80 - HTTPS — Port
443
Avoid opening unnecessary ports to the public internet.
2. Connect to the Instance
After launching the instance, connect using SSH.
1
ssh -i my-key.pem ec2-user@YOUR_EC2_PUBLIC_IP
Once connected, update the system packages:
1
sudo dnf update -y
3. Install Node.js
Install Node.js on the EC2 instance.
Verify the installation:
1
2
node -v
npm -v
You should see the installed Node.js and npm versions.
4. Upload the Application
You can clone your application directly from GitHub:
1
2
git clone https://github.com/USERNAME/PROJECT.git
cd PROJECT
Install the dependencies:
1
npm install
If your application uses environment variables, create the required
.env configuration on the server.For example:
1
2
PORT=3000
MONGODB_URI=your_database_connection_string
Do not commit sensitive credentials to GitHub.
5. Start the Application
Start the application:
1
npm start
If your Express application listens on port
3000, it should now be running on the EC2 instance.However, directly running the application from the terminal is not a good production setup because the application can stop when the session ends.
6. Use PM2
PM2 is a process manager for Node.js applications.
Install it:
1
sudo npm install -g pm2
Start the application:
1
pm2 start app.js --name my-node-app
Check the application:
1
pm2 status
PM2 can automatically restart the application if it crashes.
Save the current process list:
1
pm2 save
You can also configure PM2 to start automatically when the server reboots.
7. Configure the Application Port
Your Node.js application might currently listen on:
1
2
3
app.listen(3000, () => {
console.log("Server running on port 3000");
});
For a simple setup, you could expose port
3000 through the EC2 security group.However, a more production-oriented architecture is to use a reverse proxy such as Nginx.
The architecture then becomes:
1
2
3
4
5
6
7
Internet
|
v
Nginx :80
|
v
Node.js :3000
This allows the public-facing web server to handle incoming HTTP requests while Node.js runs internally.
8. Test the Deployment
Find your EC2 instance's public IPv4 address and open:
1
http://YOUR_EC2_PUBLIC_IP
If everything is configured correctly, your Express application should respond.
If the application does not load, check:
- EC2 instance status
- Security group rules
- Node.js process
- Application logs
- Correct port configuration
For PM2 logs:
1
pm2 logs
9. Common Deployment Problems
Application works locally but not on EC2
Make sure the server listens on an accessible interface:
1
2
3
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on port ${PORT}`);
});
Connection timeout
Check the EC2 security group's inbound rules.
Application stopped
Check PM2:
1
2
pm2 status
pm2 logs
Environment variables are missing
Make sure the required variables exist on the EC2 instance and are loaded correctly by the application.
Conclusion
Deploying a Node.js application on EC2 introduces several important concepts that are hidden by managed deployment platforms.
The basic workflow is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Develop locally
↓
Push code to GitHub
↓
Create EC2 instance
↓
Install Node.js
↓
Clone application
↓
Install dependencies
↓
Configure environment variables
↓
Run with PM2
↓
Configure networking
↓
Access application through the internet
Once this workflow is understood, it becomes much easier to move toward more advanced AWS architectures involving Application Load Balancers, Auto Scaling, RDS, VPCs, CloudWatch, and CI/CD pipelines.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article