Deploying a Node.js App to AWS EC2
Deploying a Node.js application to the cloud is an important step in backend development. One of the most common and beginner-friendly ways to host a Node.js app on AWS is by using Amazon EC2.
EC2 gives you a virtual server in the cloud where you can install Node.js, upload your application, and run it in a production environment. It gives you more control over the server compared to many managed platforms, which is why it is widely used for APIs, web applications, and backend services.
What is AWS EC2?
Amazon EC2 stands for Elastic Compute Cloud. It is a service provided by AWS that allows you to create virtual machines, called instances, in the cloud.
These instances behave like regular servers. You can install software, configure networking, deploy your application, and manage your backend environment just like on a normal Linux server.
Why Use EC2 for Node.js Deployment?
- Full control: You manage the operating system, Node.js version, and deployment flow
- Flexible: Suitable for APIs, full-stack apps, WebSocket servers, and microservices
- Scalable: Can be combined with load balancers and auto-scaling
- Widely used: Common in startups and enterprise setups
- Production-friendly: Works well with PM2, Nginx, SSL, Docker, and CI/CD
Before You Start
To deploy a Node.js app on EC2, you should have:
- An AWS account
- A Node.js application ready for deployment
- Basic understanding of Linux commands
- SSH access to the server
Step 1: Launch an EC2 Instance
In the AWS console, go to EC2 and launch a new instance. Common choices include Ubuntu or Amazon Linux as the operating system.
While launching the instance, make sure to:
- Select an appropriate instance type such as
t2.microfor learning or small apps - Create or choose a key pair for SSH access
- Allow necessary ports in the security group such as
22,80, and443
Step 2: Connect to the EC2 Server
Use SSH to connect to the instance from your local machine:
In this command:
my-key.pemis your private key fileubuntuis the default user for Ubuntu-based instancesyour-ec2-public-ipis the public IP of your EC2 instance
Step 3: Update the Server
Once connected, update the server packages:
Step 4: Install Node.js and npm
Install Node.js on the server:
Verify the installation:
Step 5: Upload Your Node.js Application
There are multiple ways to move your project to the EC2 server:
- Clone from GitHub
- Upload files using SCP
- Use CI/CD pipelines
A common method is to clone the application from Git:
Step 6: Install Project Dependencies
Inside the project folder, install dependencies:
Step 7: Configure Environment Variables
Production applications should use environment variables for secrets and configuration values.
Create a .env file:
Make sure the application reads these values using dotenv or the system environment.
Step 8: Run the Application
For a quick test, you can run the app using Node:
But in production, it is better to use a process manager like PM2.
Step 9: Install and Use PM2
PM2 helps keep the application running even if it crashes.
Check running processes:
Save the process list:
Enable startup on reboot:
Step 10: Open Port in AWS Security Group
If your app is running directly on port 3000, you must allow that port in the EC2 security group to access it externally.
However, in production it is better to keep Node.js on an internal port and expose the app using Nginx on port 80 or 443.
Step 11: Install Nginx
Install Nginx on the EC2 instance:
Start and enable Nginx:
Step 12: Configure Nginx as Reverse Proxy
Create or edit the Nginx config:
Save the configuration and restart Nginx:
Step 13: Point Domain to EC2
If you have a domain, point its DNS A record to the public IP of the EC2 instance. After DNS propagation, the domain will reach your server.
Step 14: Enable HTTPS with SSL
In production, you should secure the application with HTTPS. Letβs Encrypt is a common free option.
This sets up SSL certificates and configures Nginx for HTTPS.
Step 15: Monitor Logs and Health
After deployment, monitor application and server logs:
This helps you track errors, crashes, and request behavior.
Basic Deployment Workflow Summary
- Launch EC2 instance
- Connect via SSH
- Install Node.js and npm
- Upload project code
- Install dependencies
- Configure environment variables
- Run app with PM2
- Set up Nginx reverse proxy
- Configure domain and HTTPS
Why Use Nginx and PM2 Together?
This combination is very common in production:
- PM2 keeps the Node.js app running
- Nginx handles public web traffic, HTTPS, and reverse proxying
Together, they provide a stable and production-friendly deployment setup.
Best Practices for AWS EC2 Deployment
- Do not run production apps as root user
- Use PM2 or another process manager
- Keep Node.js behind Nginx reverse proxy
- Secure the server with proper security group rules
- Use HTTPS for production traffic
- Store secrets in environment variables
- Monitor CPU, memory, and logs regularly
Common Mistakes
- Running app directly with
node app.jsin production - Exposing port 3000 publicly instead of using Nginx
- Forgetting to configure security groups correctly
- Hardcoding secrets inside the source code
- Not enabling HTTPS
Real-World Use Cases
- Deploying REST APIs
- Hosting Express.js applications
- Running WebSocket servers
- Deploying backend services for full-stack apps
EC2 vs Platform-as-a-Service
| Feature | EC2 | Managed Platform |
|---|---|---|
| Control | High | Lower |
| Setup Complexity | Higher | Lower |
| Flexibility | High | Moderate |
| Server Management | Manual | Mostly handled by provider |
Conclusion
Deploying a Node.js application to AWS EC2 is a practical and important skill for backend developers. It gives you full control over your server, deployment process, and production environment.
When combined with PM2, Nginx, environment variables, and HTTPS, EC2 becomes a powerful platform for running production-ready Node.js applications. It is a strong foundation for learning real-world cloud deployment.

