時間:2023-02-23 07:03:01 | 來源:建站知識
時間:2023-02-23 07:03:01 來源:建站知識
用acme.sh幫你免費且自動更新的HTTPS證書,省時又省力:Certbot
來管理Let's Encrypt
的證書,使用前需要安裝一堆庫,覺得不太友好。所謂條條大路通羅馬,肯定還有其他方法可以做這個事情。curl https://get.acme.sh | sh
具體參考這幾篇:HTTPS 加密通信挺重要的,之前也聽說過 MITM(中間人攻擊),不過這還是第一次聽說證書被劫持的……
https://www.v2ex.com/t/656367
https://www.v2ex.com/t/656394
https://m.cnbeta.com/view/960295.htm一些網(wǎng)站https證書出現(xiàn)問題的情況分析
(最后這篇說很多大站都受影響了)
當(dāng)然,你還可以嘗試用 Let’s Encrypt 的 certbot
工具來簽發(fā)證書,不過要裝一堆庫吧,我也不記得了……
下面的內(nèi)容涉及 acme.sh 的安裝,證書的簽發(fā)及認(rèn)證,如何安裝到 nginx,以及自動更新證書、更新 acme.sh 等。curl https://get.acme.sh | sh
或者wget -O - https://get.acme.sh | sh
執(zhí)行上面的命令,它會:~/.acme.sh
目錄下acme.sh
的 alias 別名.bashrc
文件(source ~/.bashrc
)。$ acme.sh -hhttps://github.com/acmesh-official/acme.shv2.8.6Usage: acme.sh command ...[parameters]....Commands:--help, -h Show this help message.--version, -v Show version info.--install Install acme.sh to your system.--uninstall Uninstall acme.sh, and uninstall the cron job.--upgrade Upgrade acme.sh to the latest code from https://github.com/acmesh-official/acme.sh.--issue Issue a cert.--signcsr Issue a cert from an existing csr.--deploy Deploy the cert to your server.--install-cert Install the issued cert to apache/nginx or any other server.# ......
acme.sh
會自動在你的網(wǎng)站根目錄下放置一個文件,來驗證你的域名所有權(quán),驗證之后就簽發(fā)證書,最后會自動刪除驗證文件。example.com
,根目錄為 /home/wwwroot/example.com
。那么只需要執(zhí)行下面這條語句就行。acme.sh --issue -d example.com -w /home/wwwroot/example.com
2.1.2 Apache / Nginx modeacme.sh --issue -d example.com --apache # Apacheacme.sh --issue -d example.com --nginx # Nginx
如果找不到配置文件的話可以自行配置。acme.sh --issue -d example.com --nginx /etc/nginx/nginx.conf # 指定nginx的confacme.sh --issue -d example.com --nginx /etc/nginx/conf.d/example.com.conf # 指定網(wǎng)站的conf
2.1.3 Standalone modeacme.sh --issue -d example.com --standalone
如果用了反代之類的不是 80 端口,則可以手動指定。acme.sh --issue -d example.com --standalone --httpport 88
當(dāng)然它還支持 tls 模式,不是 443 端口的話也可以自行指定。acme.sh --issue -d example.com --alpnacme.sh --issue -d example.com --alpn --tlsport 8443 # 自行指定tls端口
export CF_Token="sdfsdfsdfljlbjkljlkjsdfoiwje"export CF_Account_ID="xxxxxxxxxxxxx"
為了限制權(quán)限,可以新建一個區(qū)域的 API Key. 這里只需要 Zone.DNS 的編輯權(quán)限(restrict the API Token only for write access to Zone.DNS for a single domain)就行。export CF_Token="sdfsdfsdfljlbjkljlkjsdfoiwje"export CF_Account_ID="xxxxxxxxxxxxx"export CF_Zone_ID="xxxxxxxxxxxxx"
Account_ID
和 Zone_ID
在域名的管理頁面右下方可以得到。acme.sh --issue --dns dns_cf -d example.com
之后這些配置信息會保存到 ~/.acme.sh/account.conf
這個文件里,在證書續(xù)期或者其他利用 CF 進(jìn)行驗證的時候會自動調(diào)用。export DP_Id="1234"export DP_Key="sADDsdasdgdsf"acme.sh --issue --dns dns_dp -d example.com -d www.example.com
(這個其實就是多域名簽發(fā)了)acme.sh --issue -d example.com --dns -d www.example.com
更多請參考官方教程 DNS manual mode。-d <YourDomainHere>
參數(shù)就行。acme.sh --issue -d example.com -w /home/wwwroot/example.com -d www.example.comacme.sh --issue -d example.com --standalone -d www.example.comacme.sh --issue -d example.com --dns -d www.example.com
也可以多個域名指定不同的驗證方式,例如acme.sh --issue /-d aa.com -w /home/wwwroot/aa.com /-d bb.com --dns dns_cf /-d cc.com --apache /-d dd.com -w /home/wwwroot/dd.com
*
就好。不過好像只適用于 DNS 驗證的方式。acme.sh --issue -d example.com -d '*.example.com' --dns dns_cf
Let's Encrypt
提供了 ECDSA 證書的簽發(fā),且 acme.sh 也支持。ec-
為前綴的 --keylength
參數(shù)(或 -k
)就可以了。理論上上面的各種驗證方式都適用。acme.sh --issue -w /home/wwwroot/example.com -d example.com --keylength ec-256 # 單域名acme.sh --issue -w /home/wwwroot/example.com -d example.com -d www.example.com --keylength ec-256 # 多域名
支持以下長度的證書,一般就用 ec-256
就行了。~/.acme.sh/
目錄下的證書文件,因為那只能內(nèi)部使用,且未來目錄結(jié)構(gòu)可能會更改。--installcert
命令,指定目標(biāo)位置,然后證書文件就會被 copy 到相應(yīng)的位置了。acme.sh --installcert -d example.com /--key-file /path/to/keyfile/in/nginx/key.pem /--fullchain-file /path/to/fullchain/nginx/cert.pem /--reloadcmd "service nginx force-reload"
比如你可以在 nginx 的目錄下新建一個 ssl
目錄,然后把證書安裝 / copy 過去。acme.sh --installcert -d example.com /--key-file /etc/nginx/ssl/example.com.key /--fullchain-file /etc/nginx/ssl/example.com.fullchain.cer /--reloadcmd "service nginx force-reload"
這里用的是service nginx force-reload
, 不是service nginx reload
, 據(jù)測試,reload
并不會重新加載證書, 所以用的force-reload
。
Nginx 配置ssl_certificate
使用/etc/nginx/ssl/fullchain.cer
,而非/etc/nginx/ssl/.cer
,否則 SSL Labs 的測試會報Chain issues Incomplete
錯誤。
acme.sh --install-cert -d example.com /--cert-file /path/to/certfile/in/apache/cert.pem /--key-file /path/to/keyfile/in/apache/key.pem /--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem /--reloadcmd "service apache2 force-reload"
在命令中的 reloadcmd
參數(shù)很重要! 這個用來指定證書更新(Renew)后執(zhí)行的命令,從而使續(xù)期后的證書生效。openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
這一步是為了增強(qiáng) SSL 的安全性。這里生成一個更強(qiáng)壯的 DHE 參數(shù)。前向安全性(Forward Secrecy)的概念很簡單:客戶端和服務(wù)器協(xié)商一個永不重用的密鑰,并在會話結(jié)束時銷毀它。服務(wù)器上的 RSA 私鑰用于客戶端和服務(wù)器之間的 Diffie-Hellman 密鑰交換簽名。從 Diffie-Hellman 握手中獲取的預(yù)主密鑰會用于之后的編碼。因為預(yù)主密鑰是特定于客戶端和服務(wù)器之間建立的某個連接,并且只用在一個限定的時間內(nèi),所以稱作短暫模式(Ephemeral)。更多參考這里 Guide to Deploying Diffie-Hellman for TLS 吧。
使用了前向安全性,如果一個攻擊者取得了一個服務(wù)器的私鑰,他是不能解碼之前的通訊信息的。這個私鑰僅用于 Diffie Hellman 握手簽名,并不會泄露預(yù)主密鑰。Diffie Hellman 算法會確保預(yù)主密鑰絕不會離開客戶端和服務(wù)器,而且不能被中間人攻擊所攔截。
nginx 依賴于 OpenSSL 給 Diffie-Hellman (DH)的輸入?yún)?shù)。不幸的是,這意味著 Diffie-Hellman Ephemeral(DHE)將使用 OpenSSL 的默認(rèn)設(shè)置,包括一個用于密鑰交換的1024位密鑰。因為我們正在使用2048位證書,DHE 客戶端就會使用一個要比非 DHE 客戶端更弱的密鑰交換。
server {server_name example.com;listen 443 ssl http2 default_server;listen [::]:443 ssl http2 default_server;# ...# ssl 相關(guān)配置ssl_certificate /etc/nginx/ssl/example.com.fullchain.cer;ssl_certificate_key /etc/nginx/ssl/example.com.key;ssl_dhparam /etc/nginx/ssl/dhparam.pem;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers '[ECDHE-ECDSA-AES128-GCM-SHA256|ECDHE-ECDSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256|ECDHE-RSA-CHACHA20-POLY1305]:ECDHE+AES128:RSA+AES128:ECDHE+AES256:RSA+AES256:ECDHE+3DES:RSA+3DES';ssl_prefer_server_ciphers on;# ...}
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
是在 0x04 步驟中生成的,可選。ssl_ciphers
用于指定加密套件,這里采用的是 CloudFlare 家的,具體也不是很清楚 emmm??梢詤⒖家幌?Mozilla 的 Wiki:Security/Server Side TLSadd_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
接下來的一年(即31536000秒)中,瀏覽器看到 header 中包含 Strict-Transport-Security
的話就會自動切換到 https。nginx -tsystemctl restart nginx
之后就可以試一下能不能通過 https 來訪問自己的網(wǎng)站啦!# crontab -l43 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
也可以試著用上面這條命令執(zhí)行看一下相關(guān)的配置是否正確。acme.sh --renew -d example.com --forceacme.sh --renew -d example.com --force --ecc # 如果用的是ECC證書
acme.sh --list
停止 Renewacme.sh --remove -d example.com [--ecc]
之后手動把目錄下的證書移除就行。acme.sh --upgrade # 手動升級acme.sh --upgrade --auto-upgrade # 自動升級acme.sh --upgrade --auto-upgrade 0 # 停止自動升級
關(guān)鍵詞:證書,更新,免費
客戶&案例
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。