溫馨提示×

Nginx SSL在Debian如何配置Let's Encrypt

小樊
51
2025-05-06 17:56:45
欄目: 云計算

在Debian系統上配置Nginx以使用Let’s Encrypt SSL證書,可以按照以下步驟進行:

1. 安裝Certbot

Certbot是一個自動化的工具,用于獲取和續訂Let’s Encrypt證書。你可以使用以下命令安裝Certbot:

sudo apt update
sudo apt install certbot python3-certbot-nginx

2. 獲取SSL證書

使用Certbot獲取SSL證書。運行以下命令并按照提示操作:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • -d yourdomain.com-d www.yourdomain.com 是你的域名。
  • Certbot會自動檢測Nginx配置文件并提示你進行必要的更改。

3. 配置Nginx

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;
    }
}

4. 測試Nginx配置

在重新加載Nginx之前,測試配置文件是否有語法錯誤:

sudo nginx -t

如果一切正常,你會看到類似以下的輸出:

nginx: configuration file /etc/nginx/nginx.conf test is successful

5. 重新加載Nginx

重新加載Nginx以應用新的配置:

sudo systemctl reload nginx

6. 自動續訂證書

Certbot會自動設置一個cron作業來定期續訂證書。你可以手動測試續訂過程:

sudo certbot renew --dry-run

如果沒有錯誤,證書將會被續訂,并且Nginx配置會自動更新。

7. 配置防火墻

確保你的防火墻允許443端口的流量:

sudo ufw allow 'Nginx Full'

8. 驗證SSL配置

使用瀏覽器訪問你的域名,確保HTTPS連接正常,并且證書有效。

通過以上步驟,你應該能夠在Debian系統上成功配置Nginx以使用Let’s Encrypt SSL證書。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女