This guide provides step-by-step instructions to set up SSL using Certbot on an Ubuntu EC2 instance. It covers installing Certbot, configuring Nginx as a reverse proxy, obtaining an SSL certificate, and enabling HTTPS for your domain. It also includes support for WebSocket connections.
- Ubuntu EC2 Instance(or any other distribution): Ensure you have an EC2 instance running Ubuntu or any(only some command will change if using other than Ubuntu).
- Domain Name: A registered domain name (e.g.,
example.com) pointing to your EC2 instance's public IP. - Open Ports: Ensure ports
80(HTTP) and443(HTTPS) are open in your EC2 security group.
- Use SSH to connect to your EC2 instance:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
- Update the system:
sudo apt update && sudo apt upgrade -y
- Install Nginx:
sudo apt install nginx -y
- Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx -y
- Create a new Nginx configuration file for your domain:
sudo nano /etc/nginx/sites-available/example.com
- Add the following configuration (replace
example.comwith your domain and3000with your app's port):server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # WebSocket support (optional) location /ws/ { 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_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
- Enable the configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- Test the Nginx configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
- Run Certbot to obtain an SSL certificate:
sudo certbot --nginx -d example.com
- Follow the prompts:
- Provide an email address for urgent renewal and security notices.
- Agree to the terms of service.
- Choose whether to redirect HTTP traffic to HTTPS (recommended:
2).
Certbot will automatically configure Nginx to use the SSL certificate.
- Check the Nginx configuration file:
You should see SSL-related directives like:
sudo nano /etc/nginx/sites-available/example.com
listen 443 ssl; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
- Test the configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
- Open your browser and visit:
https://example.com - Verify that the connection is secure (look for the padlock icon in the address bar).
Certbot automatically sets up a cron job to renew certificates. You can manually test the renewal process:
sudo certbot renew --dry-runIf your application uses WebSocket, ensure the Nginx configuration includes the following in the relevant location block:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";-
Nginx Fails to Restart:
- Check for syntax errors:
sudo nginx -t
- Ensure no other service is using ports
80or443.
- Check for syntax errors:
-
Certbot Fails to Obtain a Certificate:
- Ensure your domain's DNS points to the EC2 instance's public IP.
- Ensure ports
80and443are open in the EC2 security group.
-
WebSocket Not Working:
- Verify the WebSocket path in the Nginx configuration matches the client-side path.
- Check server logs for errors.
You have successfully set up SSL with Certbot on your Ubuntu EC2 instance. Your application is now accessible over HTTPS, and WebSocket connections are supported if configured. Certbot will automatically handle certificate renewals, ensuring your site remains secure.
For further assistance, refer to the Certbot documentation or the Nginx documentation.