Setting Up Nginx with SSL on EC2
Setting Up Nginx with SSL and Reverse Proxy to Node.js on EC2
Step 1: Install Nginx on EC2
Connect to your EC2 instance:
ssh -i your-key.pem ubuntu@your-ec2-public-ipUpdate package lists and install Nginx:
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginxStart and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginxAllow HTTP/HTTPS traffic in the firewall: (skip )
sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw enableif u cannot ssh after this do
sudo ufw allow OpenSSH
sudo ufw reloadVerify Nginx is running:
- Open your browser and go to
<span>http://your-ec2-public-ip</span> - You should see the Nginx welcome page.
Step 2: Install Certbot
Install Certbot and the Nginx plugin:
sudo apt install -y certbot python3-certbot-nginxStep 3: Obtain SSL Certificate
Run Certbot to get an SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comReplace <span>yourdomain.com</span> with your actual domain.
Follow the prompts:
- Enter your email.
- Agree to the terms.
- Certbot will automatically configure Nginx for HTTPS.
Verify SSL Certificate Renewal:
sudo certbot renew --dry-runThis ensures that the certificate auto-renewal works correctly.
Step 4: Configure Automatic Renewal
Check if Certbot is already in crontab:
sudo systemctl list-timersCertbot installs a systemd timer by default, but if you want a manual cron job:
Add a cron job to renew SSL automatically:
sudo crontab -eAdd this line to run renewal twice a day:
0 */12 * * * certbot renew --quietStep 5: Configure Nginx as a Reverse Proxy for Node.js
Open the Nginx configuration file:
sudo nano /etc/nginx/sites-available/defaultReplace the contents with:
server {
listen 80;
server_name yourdomain.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;
}
}
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
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;
}
}
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}Enable and Restart Nginx:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxStep 6: Start Your Node.js Server
Ensure your Node.js app is running on port 3000:
node server.jsOr use PM2 to keep it running:
npm install -g pm2
pm2 start server.js
pm2 save
pm2 startupStep 7: Verify HTTPS
- Open your browser and go to
<span>https://yourdomain.com</span>. - Your site should now be secure with HTTPS and proxied to your Node.js app.
Warning: Do NOT Delete <span><strong>/etc/nginx/sites-enabled/</strong></span> Folder ⚠️
Why? Deleting <span>/etc/nginx/sites-enabled/</span> can remove important configurations, including those for SSH, which may block you from connecting to your server.
How to Modify Nginx Configuration Safely
If you need to change Nginx settings:
- Open the main Nginx configuration file:
sudo nano /etc/nginx/nginx.conf - Make necessary changes.
- Test the new configuration:
sudo nginx -t - Restart Nginx safely:
sudo systemctl restart nginx
Following these steps ensures that your server remains accessible and avoids accidental lockouts.
Step 1: Install Nginx on EC2
Connect to your EC2 instance:
ssh -i your-key.pem ubuntu@your-ec2-public-ipUpdate package lists and install Nginx:
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginxStart and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginxAllow HTTP/HTTPS traffic in the firewall:
sudo ufw allow 'Nginx Full'
sudo ufw enableVerify Nginx is running:
- Open your browser and go to
http://your-ec2-public-ip - You should see the Nginx welcome page.
Step 2: Install Certbot
Install Certbot and the Nginx plugin:
sudo apt install -y certbot python3-certbot-nginxStep 3: Obtain SSL Certificate
Run Certbot to get an SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comReplace yourdomain.com with your actual domain.
Follow the prompts:
- Enter your email.
- Agree to the terms.
- Certbot will automatically configure Nginx for HTTPS.
Verify SSL Certificate Renewal:
sudo certbot renew --dry-runThis ensures that the certificate auto-renewal works correctly.
Step 4: Configure Automatic Renewal
Check if Certbot is already in crontab:
sudo systemctl list-timersCertbot installs a systemd timer by default, but if you want a manual cron job:
Add a cron job to renew SSL automatically:
sudo crontab -eAdd this line to run renewal twice a day:
0 */12 * * * certbot renew --quietStep 5: Restart Nginx
sudo systemctl restart nginxStep 6: Verify HTTPS
- Open your browser and go to
https://yourdomain.com. - Your site should now be secure with HTTPS.