DEV Community

Ramaiah Kethana
Ramaiah Kethana

Posted on

Setting Up a Node.js Server on AWS EC2 with Nginx and SSL

Amazon EC2 (Elastic Compute Cloud) is widely used for hosting applications because it is scalable and flexible. In this tutorial, we will install a Node.js server on an EC2 instance, configure a static IP address, install Nginx as a reverse proxy, configure security groups, and install an SSL certificate with Certbot.

Step 1: Launch an EC2 Instance

  1. Go to the AWS Management Console and navigate to the EC2 dashboard.
  2. Click Launch Instance and choose an Amazon Machine Image (AMI), such as Ubuntu 22.04 LTS.
  3. Select an instance type (e.g., t2.micro, which is free-tier eligible).
  4. Configure instance details and select Create new key pair for SSH access.
  5. Launch the instance and download the key file (.pem).

Step 2: Assign a Static IP (Elastic IP)

  1. In the AWS EC2 dashboard, navigate to Elastic IPs under Network & Security.
  2. Allocate a new Elastic IP and associate it with your running instance.
  3. Update your domain’s DNS settings if needed to point to the static IP.

Step 3: Connect to Your EC2 Instance

Use SSH to connect to the instance:

ssh -i your-key.pem ubuntu@your-ec2-ip
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Node.js and PM2

Update the system and install Node.js:

sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode

Verify installation:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

Install PM2 for managing Node.js processes:

npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy Your Node.js Application

  1. Clone or copy your Node.js project into the instance:
git clone https://github.com/your-repo.git
cd your-repo
npm install
Enter fullscreen mode Exit fullscreen mode
  1. Start the Node.js server using PM2:
pm start
# OR
pm run start:prod
# To keep it running in the background
pm2 start app.js --name "node-server"
pm2 save
Enter fullscreen mode Exit fullscreen mode

Step 6: Configure Security Groups

  1. In the AWS EC2 dashboard, go to Security Groups.
  2. Edit inbound rules to allow:
    • SSH (port 22) – only for your IP.
    • HTTP (port 80) – open to the world.
    • HTTPS (port 443) – open to the world.
    • Custom (port 3000) – if your Node.js app runs on port 3000 (for internal access only).

Step 7: Install and Configure Nginx as a Reverse Proxy

  1. Install Nginx:
sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode
  1. Configure Nginx:
sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Replace the contents with:

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;
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Restart Nginx:
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Step 8: Install SSL Certificate using Certbot

  1. Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Enter fullscreen mode Exit fullscreen mode
  1. Generate and install an SSL certificate:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
Enter fullscreen mode Exit fullscreen mode
  1. Auto-renew SSL:
sudo certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

Conclusion

You have successfully installed a Node.js server on AWS EC2 with Nginx as the reverse proxy and SSL encryption through Certbot. This installation provides a secure and scalable deployment for your application. Happy coding!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay