How to Deploy a Node.js App on AWS EC2 Using Docker, Nginx, and SSL (Certbot)
May 21, 2025
Deploying your Node.js app to the cloud doesn’t have to be complicated. In this comprehensive guide, you’ll learn how to launch a virtual machine using AWS EC2, containerize your app with Docker, set up a reverse proxy using Nginx, and secure it with SSL using Certbot.
What is AWS?
Amazon Web Services (AWS) is the world’s most widely adopted cloud platform. It offers on-demand infrastructure like compute power (EC2), storage (S3), databases (RDS), and networking tools — all scalable and pay-as-you-go.
Instead of setting up and managing physical servers, AWS allows you to :
- Launch virtual machines in seconds (EC2)
- Store files securely (S3)
- Use databases without server management (RDS)
- Deploy globally with minimal latency
What is EC2?
Amazon EC2 (Elastic Compute Cloud) is a virtual server hosted on the AWS cloud. It allows you to run applications just like you would on your own physical machine — but with flexibility, scalability, and global reach.
What is Docker and Why Use It?
Docker allows you to bundle your application and its dependencies into a single package, called a container. This means you don't need to worry about whether certain libraries or software are installed on the server; the container has everything it needs.
Benefits:
- Consistency across development and production
- Easy to deploy and scale
- Isolated environments for running apps
In this guide, we’ll focus on launching a secure and scalable web server using EC2, Docker, Nginx, and Certbot.
Why Use Nginx?
Nginx is a high-performance web server that is commonly used as a reverse proxy.
In this setup:
- Your Node.js app will run on port 3000 inside a Docker container.
- Nginx listens on the default web ports (80 for HTTP and 443 for HTTPS).
- Nginx forwards incoming requests to your app — this keeps your infrastructure clean and makes adding SSL easy.
What is SSL and Why Use Certbot?
SSL (Secure Sockets Layer) encrypts data between the user’s browser and your server, preventing interception or tampering.
Certbot is a free and automated tool by Let’s Encrypt that generates and renews SSL certificates.
Even though SSL is optional, it’s highly recommended:
- It ensures user data is safe.
- It builds trust for your website.
Architecture Diagram

Step 1: Launch an EC2 Instance
- Log in to the AWS Management Console.
- Region: Choose the closest to your users (e.g., ap-south-1 for Mumbai)
- Navigate to EC2 > Launch Instance.
Set the following configuration:
- Name: nodejs-server (or any identifier)
- AMI: Ubuntu Server 22.04 LTS (a stable and popular Linux distribution)
- Instance Type: t2.micro (Free Tier eligible – good for beginners and basic apps)
- Key Pair: Create or use an existing one (essential for SSH access)
Configure the Security Group
This step opens necessary ports on your server:
- Port 22 (SSH) – Required for connecting via terminal
- Port 80 (HTTP) – To serve web traffic via Nginx
- Port 443 (HTTPS) – For SSL (secure) traffic
If these ports are not open, you won’t be able to connect or access your app publicly.
- Storage: 8 GB (General Purpose SSD - gp3)
- Network: Default VPC and Subnet
Click Launch Instance.
Connect to Your EC2 Instance
Once the instance status shows “running”:
- Go to your Downloads folder (or wherever your .pem key was downloaded).
- Run the following command to set proper permissions on the key file:
- Now connect to your server using SSH:
If you share your public IP with someone, they can also connect to your EC2 (assuming they have the key and SSH access enabled).
Install Docker, Nginx, and Certbot
Once connected to your EC2 server, install the required packages:
1. Update the server
2. Install Docker
3. Install Nginx
4. Install Certbot (for SSL)
Deploy Node.js App with Docker
Make sure your Node.js app contains :
- Dockerfile
- server.js
- package.json
Dockerfile:
Deploy your app on EC2:
Then visit this
http://<your-ec2-public-ip>:3000
You should see your Node.js app running!
Set Up Nginx as a Reverse Proxy
To expose your app on port 80 with a clean URL:
1. Create a new Nginx configuration file :
Create a new Nginx site config:
2. Paste the following (replace your-domain.com with your actual domain or EC2 public IP) :
3. Enable the site and reload Nginx:
Test via:
http://<domain>
Enable SSL with Certbot (If You Have a Domain)
if you have a domain (e.g., myapp.com) pointed to your EC2 public IP:
- Make sure DNS propagation is complete.
- Run the following command to automatically install an SSL certificate and configure Ng
Follow the interactive prompts. Certbot will:
- Detect your Nginx config
- Obtain a free SSL certificate from Let’s Encrypt
- Configure Nginx to redirect HTTP → HTTPS
- Set up auto-renewal of SSL certificates
After setup:
https://your-domain.com
You’ll now see a secure 🔒 padlock in the browser
What If You Don’t Have a Domain?
No problem! You can still test the app:
- http://<your-ec2-public-ip>:3000 → Direct access to Docker app
- http://<your-ec2-public-ip> → Access via Nginx reverse proxy
SSL will not work on public IPs (requires a valid domain), but HTTP will still function properly.
Final Notes
- Stop your EC2 instance when not in use to avoid unnecessary charges.
- For production, assign an Elastic IP so your server's IP address doesn’t change on reboot.
- You can further explore
- IAM roles and policies for secure access
- CloudWatch for monitoring
- Automatic backups and scaling solutions
Conclusion
By following this guide, you’ve successfully:
- Launched a secure EC2 instance
- Installed and used Docker to run a Node.js app
- Configured Nginx as a reverse proxy
- Secure your site using Certbot SSL
This setup gives you a production-ready environment using powerful AWS services and industry best practices.
Every expert was once a beginner. Keep building, experimenting, and learning.