Deploying to AWS

Node js 12 min min read Updated: Mar 30, 2026 Advanced
Deploying to AWS
Advanced Topic 7 of 8

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.

Key Concept: AWS EC2 allows you to run your Node.js application on a cloud-based virtual server with full control over setup, deployment, and scaling.

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.micro for learning or small apps
  • Create or choose a key pair for SSH access
  • Allow necessary ports in the security group such as 22, 80, and 443

Step 2: Connect to the EC2 Server

Use SSH to connect to the instance from your local machine:

bash ssh -i my-key.pem ubuntu@your-ec2-public-ip

In this command:

  • my-key.pem is your private key file
  • ubuntu is the default user for Ubuntu-based instances
  • your-ec2-public-ip is the public IP of your EC2 instance

Step 3: Update the Server

Once connected, update the server packages:

bash sudo apt update && sudo apt upgrade -y

Step 4: Install Node.js and npm

Install Node.js on the server:

bash curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs

Verify the installation:

bash node -v npm -v

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:

bash git clone https://github.com/your-username/your-repo.git cd your-repo

Step 6: Install Project Dependencies

Inside the project folder, install dependencies:

bash npm install

Step 7: Configure Environment Variables

Production applications should use environment variables for secrets and configuration values.

Create a .env file:

env PORT=3000 DB_URL=mongodb://localhost:27017/mydb JWT_SECRET=your_secret_key NODE_ENV=production

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:

bash node app.js

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.

bash sudo npm install -g pm2 pm2 start app.js --name my-node-app

Check running processes:

bash pm2 list

Save the process list:

bash pm2 save

Enable startup on reboot:

bash pm2 startup

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:

bash sudo apt install nginx -y

Start and enable Nginx:

bash sudo systemctl start nginx sudo systemctl enable nginx

Step 12: Configure Nginx as Reverse Proxy

Create or edit the Nginx config:

nginx server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }

Save the configuration and restart Nginx:

bash sudo systemctl 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.

bash sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d your-domain.com

This sets up SSL certificates and configures Nginx for HTTPS.

Step 15: Monitor Logs and Health

After deployment, monitor application and server logs:

bash pm2 logs sudo journalctl -u nginx

This helps you track errors, crashes, and request behavior.

Basic Deployment Workflow Summary

  1. Launch EC2 instance
  2. Connect via SSH
  3. Install Node.js and npm
  4. Upload project code
  5. Install dependencies
  6. Configure environment variables
  7. Run app with PM2
  8. Set up Nginx reverse proxy
  9. 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.js in 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.

Quick Summary: AWS EC2 lets you deploy Node.js applications on a cloud server where you can install dependencies, run the app with PM2, and serve it securely using Nginx.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators