Installing Nginx on Debian with SSL renewal can be done smoothly using the Certbot tool from the Electronic Frontier Foundation (EFF). Certbot is an easy-to-use client that fetches SSL/TLS certificates from Let’s Encrypt and automates their configuration and renewal.
Here's a step-by-step guide to install Nginx and set up SSL certificates with automatic renewal on a Debian-based system:
Step 1: Update Your System
Ensure all your packages are up-to-date to avoid any compatibility issues.
sudo apt update
sudo apt upgrade -y
Step 2: Install Nginx
Install Nginx using the package manager.
sudo apt install nginx -y
Enable Nginx to start on boot and start the Nginx service.
sudo systemctl enable nginx
sudo systemctl start nginx
Step 3: Allow HTTPS Through the Firewall
Ensure the firewall allows HTTP and HTTPS traffic. If you are using ufw:
sudo ufw allow 'Nginx Full'
Step 4: Install Certbot and the Nginx Plugin
Certbot will help you obtain the SSL certificate and configure it automatically.
sudo apt install certbot python3-certbot-nginx -y
Step 5: Obtain an SSL Certificate
Use Certbot to obtain and install the SSL certificate. Certbot will also modify your Nginx configuration to use this certificate.
sudo certbot --nginx
You will be prompted to provide your email address and agree to the terms of service. Certbot will then automatically configure SSL for your domain and set up HTTP to HTTPS redirection.
Step 6: Automatic Renewal of Certificates
Certbot takes care of automatic renewal by installing a cron job or system timer. You can verify that the renewal process is set up correctly by running:
sudo certbot renew --dry-run
This simulates the renewal process and checks that everything will work correctly when the certificate is due for renewal.
Optional: Manually Verifying the Renewal Cron Job or Timer
To see the renewal job, you can list the cron jobs or check systemd timers.
For cron jobs:
sudo crontab -l
For systemd timers:
systemctl list-timers
Look for a timer for certbot.
Step 7: Nginx Configuration File Example (Optional)
If you want to manually inspect or configure your Nginx site configuration, you can find it in /etc/nginx/sites-available/ or /etc/nginx/sites-enabled/.
An example configuration for /etc/nginx/sites-available/example.com might look like:
server {
listen 80;
server_name example.com www.example.com;
# Redirect all HTTP requests to HTTPS.
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# Additional SSL settings (optional)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers HIGH:!aNULL:!MD5;
# Root directory and other setttings
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
After making any changes:
sudo nginx -t
sudo systemctl reload nginx
Conclusion
By following these steps, you will have Nginx installed with SSL/TLS certificates from Let’s Encrypt, automatically renewed by Certbot. This setup ensures your website is secure and minimizes the manual effort required to maintain your SSL certificates.