溫馨提示×

Debian Nginx SSL證書安裝

小樊
42
2025-03-20 01:05:42
欄目: 云計算

在 Debian 系統上安裝 Nginx SSL 證書的步驟如下:

1. 安裝必要的軟件包

首先,確保你已經安裝了 openssl、curl、gnupg2、ca-certificateslsb-release。如果沒有安裝,可以使用以下命令進行安裝:

sudo apt update
sudo apt install openssl curl gnupg2 ca-certificates lsb-release -y

2. 導入 Nginx 官方密鑰

導入 Nginx 官方的 GPG 密鑰以驗證軟件包的正確性:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

驗證密鑰是否正確導入:

gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

3. 安裝 Nginx

使用以下命令安裝 Nginx:

sudo apt update
sudo apt install nginx -y

4. 安裝 Certbot

推薦使用 Snap 安裝 Certbot,因為它提供了更好的兼容性和功能:

sudo apt update
sudo apt install snapd -y
sudo snap install core -y
sudo snap refresh core
sudo ln -s /snap/bin/certbot /usr/bin/certbot

5. 使用 Certbot 獲取 SSL 證書

申請單域名 SSL 證書

certbot certonly --webroot -w /path/to/your/webroot -d yourdomain.com -m youremail@example.com --agree-tos

申請泛域名 SSL 證書

certbot certonly --manual --preferred-challenges dns -d "*.yourdomain.com" -w /path/to/your/webroot -m youremail@example.com

按照提示完成 DNS 記錄添加。

6. 配置 Nginx 使用 SSL 證書

創建或編輯 Nginx 配置文件,例如 /etc/nginx/sites-available/yourdomain.com,填入以下內容:

server {
    listen 443 ssl http2;
    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;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'HIGH:!aNULL:!MD5';
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

創建符號鏈接以啟用該配置:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

測試 Nginx 配置并重啟服務:

sudo nginx -t
sudo systemctl restart nginx

7. 設置自動續簽

Certbot 支持自動續簽 SSL 證書,你可以通過以下命令設置:

sudo certbot renew --quiet

為了確保 Certbot 在證書到期前自動續簽,將其添加到 crontab:

sudo crontab -e

添加以下行:

0 0,12 * * * certbot renew --quiet && systemctl reload nginx

這樣,Certbot 將在每天的兩個零點和中午十二點檢查證書的有效性,并在需要時自動續簽。

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