在Debian系統上,使用Nginx和Let’s Encrypt(現在稱為Certbot)來配置SSL證書是一個相對簡單的過程。以下是詳細步驟:
首先,你需要安裝Certbot及其Nginx插件。打開終端并運行以下命令:
sudo apt update
sudo apt install certbot python3-certbot-nginx
Certbot會自動檢測你的Nginx配置文件,并提示你輸入域名。運行以下命令開始獲取證書:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
將 yourdomain.com
和 www.yourdomain.com
替換為你的實際域名。
Certbot會自動修改你的Nginx配置文件以啟用SSL。你需要確保你的Nginx配置文件中有以下內容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# 其他配置...
}
在重新加載Nginx之前,測試配置文件是否有語法錯誤:
sudo nginx -t
如果沒有錯誤,重新加載Nginx以應用更改:
sudo systemctl reload nginx
Certbot會自動設置一個cron任務來定期檢查并續期你的SSL證書。你可以手動測試續期過程:
sudo certbot renew --dry-run
如果一切正常,Certbot會輸出續期成功的信息。
確保你的防火墻允許HTTP(端口80)和HTTPS(端口443)流量。如果你使用的是UFW,可以運行以下命令:
sudo ufw allow 'Nginx Full'
通過以上步驟,你可以在Debian系統上使用Nginx和Let’s Encrypt來配置SSL證書。Certbot會簡化整個過程,并自動處理證書的獲取和續期。