Node.js Deployment with Nginx SSL
1. Create EC2 Instance
Launch an Ubuntu EC2 instance in AWS.
Connect:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
2. Update Packages
sudo apt-get update
3. Install Latest Node.js
Install Node and NPM
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install nodejs -y
Check version:
node --version
npm --version
4. Clone Project
git clone <your-repository-url>
Enter project:
cd project
Install dependencies:
npm install
Go back if needed:
cd ..
5. Install PM2 (Process Manager)
sudo npm install pm2 -g
Start application
(Do this inside project folder)
cd project
pm2 start app.js
If changes are made:
pm2 restart all
Check running processes:
pm2 list
6. Test App
Copy EC2 public IP and open:
http://your-ec2-ip:8001
(Use your actual app port)
If not accessible:
Check EC2 Security Group inbound rules:
- SSH → Port 22
- HTTP → Port 80
- Custom TCP → App Port (Example 8001)
7. Install Nginx
sudo apt install nginx -y
8. Configure Nginx Reverse Proxy
Edit default config:
sudo vim /etc/nginx/sites-available/default
Update server block:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:8001;
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;
}
}
Replace:
8001
with your app port.
9. Test Nginx Config
sudo nginx -t
Expected:
syntax is ok
test is successful
10. Reload Nginx
sudo nginx -s reload
or
sudo service nginx reload
11. Point Domain to EC2
In DNS provider:
Create:
A Record → yourdomain.com → EC2 Public IP
and
A Record → www.yourdomain.com → EC2 Public IP
12. Add SSL with Let's Encrypt
Install Certbot:
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python3-certbot-nginx -y
Generate SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
13. Test SSL Renewal
Certificates are valid for 90 days.
Test renewal:
certbot renew --dry-run
Final Result
Your app should be available at:
https://yourdomain.com
with:
- Node.js running via PM2
- Nginx reverse proxy
- SSL via Let's Encrypt
Architecture
User
↓
HTTPS (443)
↓
Nginx
↓
Proxy Pass
↓
Node.js App (8001)
↓
PM2