在Debian上,您可以使用Certbot(Let’s Encrypt的官方客戶端)來實現Nginx的自動續期SSL證書。以下是詳細步驟:
首先,確保您的系統是最新的:
sudo apt update
sudo apt upgrade
然后安裝Certbot和Nginx插件:
sudo apt install certbot python3-certbot-nginx
使用Certbot獲取SSL證書。假設您的域名是example.com,運行以下命令:
sudo certbot --nginx -d example.com -d www.example.com
Certbot會自動配置Nginx以使用SSL,并提示您輸入電子郵件地址和同意服務條款。
Certbot會創建一個定時任務(cron job)來自動續期證書。您可以通過以下命令查看和管理這些任務:
sudo crontab -l
默認情況下,Certbot會在證書到期前30天自動嘗試續期。如果續期成功,它會重新加載Nginx配置以應用新的證書。
您可以手動測試續期過程以確保一切正常:
sudo certbot renew --dry-run
如果一切正常,Certbot會顯示續期成功的信息。
Certbot會創建一個日志文件來記錄續期嘗試的結果。您可以通過以下命令查看日志:
sudo tail -f /var/log/letsencrypt/renewal.log
確保您的Nginx配置文件中包含SSL相關的設置。通常,Certbot會自動修改/etc/nginx/sites-available/default或您自定義的站點配置文件。以下是一個示例配置:
server {
listen 80;
server_name example.com www.example.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
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;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
root /var/www/html;
index index.html index.htm;
}
}
在修改Nginx配置文件后,記得重新加載Nginx以應用更改:
sudo systemctl reload nginx
通過以上步驟,您可以在Debian上使用Certbot實現Nginx的自動續期SSL證書。