Page cover
From localhost to production -
deploy with confidence,
secure by default &
scale when ready.

How to Deploy a Node.js Application to AWS EC2

Intermediate 30-45 minutes December 20, 2024
How to Deploy a Node.js Application to AWS EC2

Learn how to deploy your Node.js application to an AWS EC2 instance with PM2 process manager and Nginx reverse proxy.

Prerequisites

Essential knowledge and skills you should have before starting

Basic understanding of Node.js and Express framework
Familiarity with Linux command line operations
AWS account with appropriate permissions

Things You'll Need

  • AWS account with EC2 access
  • Node.js application ready to deploy
  • SSH client installed on your computer
  • Domain name (optional)

Requirements

  • Basic understanding of Linux commands
  • AWS account credentials
  • Node.js application with package.json

Steps

Follow these 10 steps to complete this guide

1

Launch an EC2 Instance

Log into your AWS Console and navigate to EC2. This step involves several configuration choices that we'll walk through in detail.

AWS EC2 Launch Instance Dashboard
The AWS EC2 Launch Instance wizard
1.1

Navigate to EC2 Dashboard

From the AWS Console home, search for 'EC2' in the search bar or find it under 'Compute' services. Click to open the EC2 Dashboard.

AWS EC2 Dashboard
The EC2 Dashboard showing your instances overview
1.2

Click Launch Instance

On the EC2 Dashboard, click the orange 'Launch Instance' button to start the instance creation wizard.

  • You can also use the 'Launch Instance' dropdown to launch from a template
1.3

Choose an Amazon Machine Image (AMI)

Select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type. This is a free tier eligible image that works well for Node.js applications.

AMI selection decision flow
text
Recommended AMI Details:
- Name: Ubuntu Server 22.04 LTS (HVM)
- Architecture: 64-bit (x86)
- Root device type: EBS
- Virtualization: HVM
1.4

Select Instance Type

Choose t2.micro for the free tier (750 hours/month free for 12 months). This provides 1 vCPU and 1 GiB memory, which is sufficient for small applications.

t2.micro (Free Tier Eligible) vCPUs: 1 | Memory: 1 GiB | Storage: EBS only ✓ 750 hours/month free for 12 months
t2.micro instance specifications
  • Going beyond free tier limits will incur charges
  • Monitor your usage in the AWS Billing Dashboard
1.5

Configure Key Pair

Create a new key pair or select an existing one. This key pair is essential for SSH access to your instance.

bash
# After downloading the key pair, set correct permissions:
chmod 400 your-key-pair.pem

# Store it in a secure location like ~/.ssh/
mv your-key-pair.pem ~/.ssh/
  • Use ED25519 key type for better security
  • Store your private key in ~/.ssh/ directory
  • You can only download the private key once during creation
  • If you lose the key, you'll need to create a new instance

Tips

  • Start with t2.micro to stay within free tier limits
  • Keep your .pem key file safe - you won't be able to download it again
2

Configure Security Groups

Edit the security group to allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). This ensures your application will be accessible from the internet.

Video walkthrough of security group configuration
bash
# Security group rules needed:
# SSH (22) - Your IP
# HTTP (80) - Anywhere
# HTTPS (443) - Anywhere
# Custom TCP (3000) - Anywhere (for Node.js, can be removed after Nginx setup)

Warnings

  • Don't expose port 22 to 0.0.0.0/0 in production - limit it to your IP address
3

Connect to Your Instance via SSH

Use your terminal to connect to the EC2 instance using the downloaded key pair. Replace 'your-key.pem' with your key file name and the IP address with your instance's public IP.

bash
chmod 400 your-key.pem
ssh -i "your-key.pem" ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com
4

Install Node.js and npm

Update the package manager and install Node.js using NodeSource repository. This ensures you get the latest LTS version of Node.js.

Terminal showing Node.js installation
Node.js installation process in the terminal
bash
# Update system packages
sudo apt update && sudo apt upgrade -y

# Install Node.js 18.x LTS
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

# Verify installation
node --version
npm --version

Tips

  • Use Node.js LTS version for production deployments
  • Consider using nvm for managing multiple Node.js versions
5

Install PM2 Process Manager

PM2 is a production process manager for Node.js applications. It keeps your app running, restarts it if it crashes, and manages logs.

PM2 manages multiple Node.js application instances
bash
# Install PM2 globally
sudo npm install -g pm2

# Verify installation
pm2 --version
6

Clone and Set Up Your Application

Clone your application from your repository and install dependencies. If using environment variables, create a .env file with your configuration.

bash
# Clone your repository
git clone https://github.com/yourusername/your-app.git
cd your-app

# Install dependencies
npm install

# Create environment file (if needed)
nano .env
# Add your environment variables, save with Ctrl+X, Y, Enter

Warnings

  • Never commit .env files to version control
  • Make sure all production environment variables are set correctly
7

Start Your Application with PM2

Use PM2 to start your Node.js application. PM2 will keep it running in the background and automatically restart it if it crashes.

bash
# Start your app with PM2
pm2 start npm --name "my-app" -- start

# Or if you have an index.js or server.js:
pm2 start server.js --name "my-app"

# View running processes
pm2 list

# View logs
pm2 logs

# Save PM2 configuration
pm2 save

# Set PM2 to start on system boot
pm2 startup

Tips

  • Use descriptive names for your PM2 processes
  • Set up PM2 to auto-start on server reboot
8

Install and Configure Nginx

Nginx will act as a reverse proxy, forwarding requests from port 80 to your Node.js application running on port 3000.

Internet :80/:443 Nginx Reverse Proxy Node.js :3000 DB
Request flow: Internet → Nginx (port 80) → Node.js (port 3000)
bash
# Install Nginx
sudo apt install nginx -y

# Create Nginx configuration
sudo nano /etc/nginx/sites-available/my-app

# Add this configuration:
server {
    listen 80;
    server_name your-domain.com www.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;
    }
}

# Enable the site
sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/

# Test Nginx configuration
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx
9

Set Up SSL Certificate (Optional)

Use Let's Encrypt to add free SSL certificates to your application for HTTPS support.

bash
# Install Certbot
sudo apt install certbot python3-certbot-nginx -y

# Obtain SSL certificate
sudo certbot --nginx -d your-domain.com -d www.your-domain.com

# Certbot will automatically configure Nginx for HTTPS
# Certificates auto-renew, but you can test renewal with:
sudo certbot renew --dry-run

Tips

  • Make sure your domain DNS is pointing to your EC2 instance's IP
  • SSL certificates from Let's Encrypt are free and auto-renew
10

Test Your Deployment

Visit your domain or EC2 public IP in a browser to verify your application is running correctly. Check PM2 logs if you encounter any issues.

bash
# Check if your app is running
pm2 status

# View real-time logs
pm2 logs my-app

# Check Nginx status
sudo systemctl status nginx

# Test from command line
curl http://your-domain.com

Tips

  • Keep PM2 logs open in a separate terminal while testing
  • Use browser developer tools to check for any errors
Tags: AWS Node.js EC2 Deployment DevOps

Was this guide helpful?

Check out more step-by-step guides for development, deployment, debugging, and configuration.

1 Launch an EC2 Instance
0/10