在Debian系統上配置Nginx以使用Let’s Encrypt SSL證書,可以按照以下步驟進行:
Certbot是一個自動化的工具,用于獲取和續訂Let’s Encrypt證書。你可以使用以下命令安裝Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
使用Certbot獲取SSL證書。運行以下命令并按照提示操作:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
-d yourdomain.com
和 -d www.yourdomain.com
是你的域名。Certbot會自動修改你的Nginx配置文件以啟用SSL。通常,配置文件位于 /etc/nginx/sites-available/yourdomain.com
和 /etc/nginx/sites-enabled/yourdomain.com
。
你可以手動檢查這些文件,確保它們包含以下內容:
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;
root /var/www/html;
index index.html index.htm;
location ~ /\.ht {
deny all;
}
}
在重新加載Nginx之前,測試配置文件是否有語法錯誤:
sudo nginx -t
如果一切正常,你會看到類似以下的輸出:
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新加載Nginx以應用新的配置:
sudo systemctl reload nginx
Certbot會自動設置一個cron作業來定期續訂證書。你可以手動測試續訂過程:
sudo certbot renew --dry-run
如果沒有錯誤,證書將會被續訂,并且Nginx配置會自動更新。
確保你的防火墻允許443端口的流量:
sudo ufw allow 'Nginx Full'
使用瀏覽器訪問你的域名,確保HTTPS連接正常,并且證書有效。
通過以上步驟,你應該能夠在Debian系統上成功配置Nginx以使用Let’s Encrypt SSL證書。