什么是Let's Encrypt?
目前世界上就只有為數不多的幾家域名證書簽發機構得到瀏覽器的認可,而Let‘s Encrypt
就是其中一家,並且你可以申請到免費的證書,當然你如果想要付費也行,很多機構證書動輒幾千幾萬一年。如果我們只想搭建個測試環境有需要https
,我們肯定不會去花這個冤枉錢,當然免費的午餐並沒有那么好吃,Let's Encrypt
申請的證書只有90天有效期,所以到期你得進行續期操作。並且還有各種各樣得條件限制,比如一周你只能申請多少次,同一個ip
一天只能操作多少次之類,詳見
https://letsencrypt.org/docs/rate-limits/
假如你使用jdk
生成的自簽證書能不能用呢?當然是可以用的,只不過瀏覽器會告訴你這個證書我不承認。也就是左上角會給你掛個不安全的警告。
什么是泛域名證書?
例如:*.xxx.cn
也就是這個證書可以給某個域名的所有二級域名使用,就叫做泛域名證書(也稱作通配符證書)。
Let's Encrypt
官方推薦我們使用certbot
腳本申請證書(當然也可以使用acme.sh等方式),以下是申請步驟基於Debian10
python3.7.3
如果你在操作過程中遇到什么報錯,請多考慮python
工具包的版本問題之類的。
Let's Encrypt自2018年開始支持申請泛域名證書,相比於單域名證書,泛域名證書更利於日常的維護。
准備工作
下載 certbot,這個很多發行版的源中都已經自帶了。比如Debian的:
sudo apt install certbot
需要有域名的管理權限,因為申請泛解析證書需要使用 DNS 驗證,這就需要你能夠根據要求操作 DNS 解析記錄,以此證明你對域名的權限。
開始申請泛域名證書
certbot certonly --preferred-challenges dns --manual -d *.xx.cn --server https://acme-v02.api.letsencrypt.org/directory
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for xx.cn
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.
Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.xx.cn with the following value:
nI0DhzH-vn0W7STVuLi2O-oIKuFNlqQx5EnjB-zewvs
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue #讓你的二級域名_acme-challenge.xx.cn用TXT方式解析到nI0DhzH-vn0W7STVuLi2O-oIKuFNlqQx5EnjB-zewvs
#可用dig -t txt _acme-challenge.xx.cn驗證解析是否生效,然后按下Enter通過驗證。 debian可用apt install dnsutils來安裝dig命令。
#也可以用nslookup命令來驗證。
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/xx.cn/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/xx.cn/privkey.pem
Your cert will expire on 2021-04-26. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
#至此證書申請成功
參數說明:
certonly 表示只申請證書。
--no-bootstrap 需要用戶同意的系統級操作直接選N。
--manual 表示交互式申請。
-d 為那些主機申請證書如 *.xxx.cn(此處為泛域名)
--preferred-challenges dns,使用 DNS 方式校驗域名所有權,可以配置多個
--server Let's Encrypt ACME v2 版本使用的服務器不同於 v1 版本(V2版本才支持泛域名解析),需要顯示指定。
證書簽發成功后去Nginx或Apache配置新生成的證書文件即可。
撤銷證書
倘若有不需要的證書了,可撤銷刪除。
sudo certbot revoke --cert-path /etc/letsencrypt/archive/xx.cn/cert1.pem
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you like to delete the cert(s) you just revoked, along with all earlier
and later versions of the cert?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es (recommended)/(N)o: y #是否刪除證書
自動驗證的方式申請證書
上面的方式需要手動去到DNS解析服務商更改解析通過驗證,其實通過Certbot 官方的插件可以實現自動更新DNS通過驗證。https://certbot.eff.org/docs/using.html#manual
支持上面這些服務商。
准備
安裝對應的DNS插件
sudo apt install python3-certbot-dns-cloudflare #sudo apt install python2-certbot-dns-cloudflare
#我用到的服務商是Cloudflare
配置 DNS 插件
在cloudflare獲取KEY/TOKEN
兩種方式均可,只是token的方式需要2.3.1版本以上的python*-certbot-dns-cloudflare插件。出於安全因素,推薦前者的方式。
# Cloudflare API credentials used by Certbot dns_cloudflare_email = cloudflare@example.com dns_cloudflare_api_key = 0123456789abcdef0123456789abcdef01234567
將上面的配置信息寫入~/cloudflare.ini。
申請
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/cloudflare.ini --dns-cloudflare-propagation-seconds 60 --preferred-challenges dns -d *.example.win --server https://acme-v02.api.letsencrypt.org/directory
#--dns-cloudflare-propagation-seconds 60 #等待60秒, 等DNS解析生效
由於這里的泛域名證書之前已申請過,且存在於服務器,Certbot自動進行了續期操作。
至此,泛域名證書申請完成。
續期
若是通過手動更改DNS解析的方式申請的域名,我們執行certbot renew時會報錯。
此時只有重新按照申請證書的步驟再來一遍。
certbot certonly --preferred-challenges dns --manual -d *.xx.cn --server https://acme-v02.api.letsencrypt.org/directory
若是通過API自動驗證的方式申請的,則可以直接用cerbot renew的方式更新證書!