時間:2023-02-21 23:54:01 | 來源:建站知識
時間:2023-02-21 23:54:01 來源:建站知識
Let's Encrypt SSL 泛域名證書申請和配置:為了在網(wǎng)站上啟用 HTTPS,我們需要從證書頒發(fā)機構(gòu)(CA)申請 SSL 證書。Let’s Encrypt 是一個證書頒發(fā)機構(gòu),向 Let’s Encrypt 申請證書是免費的。Let's Encrypt 支持泛域名證書,不需要為每個子域名單獨申請證書。本文以申請泛域名證書為例,詳細介紹安裝和配置 SSL 證書的過程。certbot
、acme.sh
、acme-tiny
,本文使用的是 acme.sh
。acme.sh
申請和安裝泛域名 SSL 證書相對來說是比較方便的。socat
模塊,它是一個多功能的網(wǎng)絡(luò)小工具。dnf install socat -y
通過下面命令安裝 acme.sh
,Email 用來接收重要重要通知,如證書快到期未更新會收到通知。curl https://get.acme.sh | sh -s email=my@example.com
執(zhí)行命令后幾秒就安裝好了,如果半天沒有反應(yīng)請 Ctrl+C
后重新執(zhí)行命令。acme.sh
安裝在 ~/.acme.sh
目錄下,并自動創(chuàng)建了一個 cronjob,每天 0:00 點自動檢測所有的證書,如果快過期了, 則會自動更新。acme.sh
全局應(yīng)用別名,但有時候會 command not found
,需要手動執(zhí)行以下命令:source ~/.bashrc
或 source ~/.bash_profile
,或關(guān)掉終端重新打開,然后再繼續(xù)下一步。acme.sh
實現(xiàn)了 acme 協(xié)議支持的所有驗證協(xié)議,一般有三種驗證方式:HTTP 方式、手動 DNS 方式和 DNS API 方式。推薦使用 DNS API 方式。acme.sh --issue -d example.com --webroot /path/to/example.com/
acme.sh
會自動的生成驗證文件,并放到網(wǎng)站的根目錄,然后自動完成驗證,最后會自動刪除驗證文件。acme.sh
還可以智能的從 Nginx 的配置中自動完成驗證,你不需要指定網(wǎng)站根目錄。acme.sh --issue -d example.com --nginx
注意,HTTP 驗證方式不支持生成泛域名證書。acme.sh --issue -d example.com -d "*.example.com" --dns /--yes-I-know-dns-manual-mode-enough-go-ahead-please
到 DNS 解析中,新增一條 TXT 記錄,域名前綴為 _acme-challenge
,記錄值為終端輸出的 TXT value
的值。然后再次執(zhí)行:acme.sh --renew -d example.com -d "*.example.com" /--yes-I-know-dns-manual-mode-enough-go-ahead-please
注意:這種方式的壞處是,如果不同時配置 DNS API,將無法自動更新證書,每次都需要手動再次重新解析驗證域名所有權(quán)。# 騰訊云export DP_Id="YourId"export DP_Key="YourToken"# 阿里云export Ali_Key="YourAccessKeyId"export Ali_Secret="YourAccessKeySecret"
再通過下面命令生成證書:# 騰訊云acme.sh --issue --dns dns_dp -d example.com -d *.example.com# 阿里云acme.sh --issue --dns dns_ali -d example.com -d *.example.com
注意:這里第一個域名為頂級域名,后面一個為泛域名。這種方式將自動為你的域名添加一條 TXT 解析,驗證成功后,這條解析記錄會被刪除,對你來說是無感的。~/.acme.sh/example.com/
目錄中。請不要直接使用 ~/.acme.sh/
目錄下的文件,這里面的文件都是內(nèi)部使用的,而且目錄結(jié)構(gòu)可能會變化,我們需要把證書復(fù)制到需要用的地方去。~/.acme.sh/example.com/
目錄生成的證書文件中,我們主要需要用到兩個文件:fullchain.cer
和 example.com.key
。下面以 Nginx 為例,來看看如何安裝證書。/etc/ginx/
目錄下,創(chuàng)建一個為名 ssl-options.conf
的 SSL 通用配置文件,內(nèi)容參考如下:ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;ssl_prefer_server_ciphers off;ssl_session_cache shared:le_nginx_SSL:10m;ssl_session_timeout 1200m;ssl_session_tickets on;ssl_stapling on;
參數(shù)說明:ssl_protocols
:加密協(xié)議;ssl_ciphers
:加密算法;ssl_prefer_server_ciphers
:服務(wù)端加密算法優(yōu)先;ssl_session_cache
:會話緩存;ssl_session_timeout
:用戶會話緩存失效時間,對安全性有高要求的站點需要降低該值;ssl_stapling
:啟用 OCSP 可減少用戶驗證證書的時間;ssl_session_tickets
:為復(fù)用會話創(chuàng)建或加載 Ticket Key。dhparam.pem
文件:openssl dhparam -out /etc/nginx/dhparam.pem 2048
mkdir -p /etc/nginx/cert/example.com
目錄中的文件我們后面通過腳本復(fù)制進來,這里先不管。/etc/nginx/conf.d/example.com.conf
,參考編輯其內(nèi)容如下:# /etc/nginx/conf.d/example.com.confserver { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri;}server { listen 443 ssl; server_name example.com www.example.com; server_tokens off; # 禁止在響應(yīng)報文中包含Nginx版本信息 ssl_certificate /etc/nginx/cert/example.com/fullchain.cer; ssl_certificate_key /etc/nginx/cert/example.com/example.com.key; include /etc/nginx/ssl-options.conf; ssl_dhparam /etc/nginx/dhparam.pem; if ($host != 'example.com' ) { return 301 https://example.com$request_uri; } location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }}
-install-cert
命令安裝。在命令中指定目標(biāo)位置,證書文件會被復(fù)制到相應(yīng)的位置。并且,可以指定 reloadcmd
命令,當(dāng)證書更新以后,reloadcmd
命令會被自動調(diào)用,讓新的配置生效。acme.sh --install-cert -d example.com /--key-file /etc/nginx/cert/example.com/example.com.key /--fullchain-file /etc/nginx/cert/example.com/fullchain.cer /--reloadcmd "systemctl force-reload nginx"
這里指定的所有參數(shù)都會被自動記錄下來,并在將來證書自動更新以后, 被再次自動調(diào)用。證書在到期之前會自動更新,你無需任何操作。acme.sh
也經(jīng)常更新以保持同步。acme.sh --upgrade
如果你不想手動升級,可以開啟自動升級:acme.sh --upgrade --auto-upgrade
你也可以隨時關(guān)閉自動更新:acme.sh --upgrade --auto-upgrade 0
acme.sh 官方文檔:https://github.com/acmesh-official/acme.sh/wiki
關(guān)鍵詞:證書,申請,配置
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。