最近個人有10多個二級域名需要簽發(fā)證書,而Let's encrypt 提供期限為三個月的免費SSL證書,到期之后需要renew,官方還提供自動renew的工具certbot。這里記錄下使用Let's Encrypt生成泛域名的通用證書巩步。
什么是 Let’s Encrypt?
部署 HTTPS 網(wǎng)站的時候需要證書桦踊,證書由 CA 機(jī)構(gòu)簽發(fā)椅野,大部分傳統(tǒng) CA 機(jī)構(gòu)簽發(fā)證書是需要收費的,這不利于推動 HTTPS 協(xié)議的使用籍胯。
Let’s Encrypt 也是一個 CA 機(jī)構(gòu)竟闪,但這個 CA 機(jī)構(gòu)是免費的。也就是說簽發(fā)證書不需要任何費用芒炼。Let’s Encrypt 由于是非盈利性的組織瘫怜,需要控制開支,他們搞了一個非常有創(chuàng)意的事情本刽,設(shè)計了一個 ACME 協(xié)議鲸湃。
那為什么要創(chuàng)建 ACME 協(xié)議呢,傳統(tǒng)的 CA 機(jī)構(gòu)是人工受理證書申請子寓、證書更新暗挑、證書撤銷,完全是手動處理的斜友。而 ACME 協(xié)議規(guī)范化了證書申請炸裆、更新、撤銷等流程鲜屏,只要一個客戶端實現(xiàn)了該協(xié)議的功能烹看,通過客戶端就可以向 Let’s Encrypt 申請證書,也就是說 Let’s Encrypt CA 完全是自動化操作的洛史。
任何人都可以基于 ACME 協(xié)議實現(xiàn)一個客戶端惯殊,官方推薦的客戶端是Certbot 。
什么是通配符證書
在沒有出現(xiàn)通配符證書之前也殖,Let’s Encrypt 支持兩種證書土思。
1)單域名證書:證書僅僅包含一個主機(jī)。
2)SAN 證書:域名通配符證書類似 DNS 解析的泛域名概念忆嗜,通配符證書就是證書中可以包含一個通配符(*.exmaple.com)己儒。主域名簽發(fā)的通配符證書可以在所有子域名中使用,比如 www.example.com
捆毫、bbs.example.com
闪湾。
申請通配符證書
Let’s Encrypt 上的證書申請是通過 ACME 協(xié)議來完成的。ACME 協(xié)議規(guī)范化了證書申請绩卤、更新响谓、撤銷等流程损合,實現(xiàn)了 Let’s Encrypt CA 自動化操作省艳。解決了傳統(tǒng)的 CA 機(jī)構(gòu)是人工手動處理證書申請娘纷、證書更新、證書撤銷的效率和成本問題跋炕。
ACME v2 是 ACME 協(xié)議的更新版本赖晶,通配符證書只能通過 ACME v2 獲得。要使用 ACME v2 協(xié)議申請通配符證書辐烂,只需一個支持該協(xié)議的客戶端就可以了遏插,官方推薦的客戶端是 Certbot
客戶在申請 Let’s Encrypt 證書的時候,需要校驗域名的所有權(quán)纠修,證明操作者有權(quán)利為該域名申請證書胳嘲,目前支持三種驗證方式:
- dns01:給域名添加一個 DNS TXT 記錄。
- http01:在域名對應(yīng)的 Web 服務(wù)器下放置一個 HTTP well-known URL 資源文件扣草。
- tls-sni01:在域名對應(yīng)的 Web 服務(wù)器下放置一個 HTTPS well-known URL 資源文件了牛。
申請通配符證書,只能使用 dns驗證的方式辰妙,下面開始申請
獲取certbot 客戶端
# 下載 Certbot 客戶端
$ wget -c https://dl.eff.org/certbot-auto -P /usr/local/bin/
# 設(shè)為可執(zhí)行權(quán)限
$ chmod a+x /usr/local/bin/certbot-auto
$ certbot-auto --version
certbot 0.34.2
注: Certbot 0.22.0 以上版本支持ACME v2 協(xié)議.
開始申請證書
certbot-auto certonly -d *.example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
- certonly鹰祸,表示安裝模式,Certbot 有安裝模式和驗證模式兩種類型的插件密浑。
- -d 為那些主機(jī)申請證書蛙婴,如果是通配符,輸入 *.example.com
- --manual 表示手動安裝插件尔破,Certbot 有很多插件街图,不同的插件都可以申請證書,用戶可以根據(jù)需要自行選擇
- --preferred-challenges dns懒构,使用 DNS 方式校驗域名所有權(quán)
- --server餐济,Let's Encrypt ACME v2 版本使用的服務(wù)器不同于 v1 版本,需要顯示指定痴脾。
執(zhí)行完以上命令之后颤介,就是命令行的輸出,根據(jù)提示輸入相應(yīng)內(nèi)容:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): fate1028@163.com #用于安全以及續(xù)約通知的郵箱
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A #是否同意相關(guān)協(xié)議條款
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N #是否訂閱相關(guān)的郵件
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 #詢問是否對域名和機(jī)器(IP)進(jìn)行綁定
以上確認(rèn)后繼續(xù)赞赖,以下提示要求配置 DNS TXT 記錄滚朵,從而校驗域名所有權(quán),也就是判斷證書申請者是否有域名的所有權(quán)前域。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
OGWxK87UxvB5iMp2tn-tfnB7r_kICS3oSRHyo0ouwzU
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue ## 這里切記自己驗證生效后再回車執(zhí)行
上面輸出要求給 _acme-challenge.example.com 配置一條 TXT 記錄辕近,在沒有確認(rèn) TXT 記錄生效之前不要回車執(zhí)行。請自行登錄域名管理系統(tǒng)匿垄,比如我在namecheap買的域名移宅,以下是我在域名解析里面配置的TXT記錄.
然后輸入下列命令確認(rèn) TXT 記錄是否生效:
$ dig -t txt _acme-challenge.example.com @8.8.8.8
; <<>> DiG 9.10.6 <<>> -t txt _acme-challenge.example.com @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20013
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.chat186.com. IN TXT
;; ANSWER SECTION:
_acme-challenge.example.com. 1798 IN TXT "OGWxK87UxvB5iMp2tn-tfnB7r_kICS3oSRHyo0ouwzU"
;; Query time: 258 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Fri May 31 20:33:01 CST 2019
;; MSG SIZE rcvd: 112
確認(rèn)生效后归粉,回車執(zhí)行,輸出如下信息
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2019-08-29. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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
此時我們的證書已經(jīng)申請成功了,證書都保存在如下目錄中:
$ tree /etc/letsencrypt/live/example.com/
/etc/letsencrypt/live/example.com/
├── cert.pem -> ../../archive/example/cert1.pem
├── chain.pem -> ../../archive/example.com/chain1.pem
├── fullchain.pem -> ../../archive/example.com/fullchain1.pem
├── privkey.pem -> ../../archive/example.com/privkey1.pem
└── README
校驗證書信息漏峰,輸入如下命令:
$ openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -text
# 可以看到證書包含了 SAN 擴(kuò)展糠悼,該擴(kuò)展的值就是 *.example.com
...
Authority Information Access:
OCSP - URI:http://ocsp.int-x3.letsencrypt.org
CA Issuers - URI:http://cert.int-x3.letsencrypt.org/
X509v3 Subject Alternative Name:
DNS:*.example.com
...
到此,我們就演示了如何在 Let’s Encrypt 申請免費的通配符證書浅乔。
證書續(xù)約
上個環(huán)節(jié)最后一步最重要事項中提示Let’s encrypt 的免費證書默認(rèn)有效期為 90 天倔喂,到期后如果要非交互式續(xù)期所有證書,可以執(zhí)行如下命令:
$ certbot-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certs are not due for renewal yet:
/etc/letsencrypt/live/example.com/fullchain.pem expires on 2019-08-29 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
由于還沒到期所以被忽略掉了,以上的非交互式續(xù)約的前提是存在續(xù)期的賬號憑證靖苇,默認(rèn)第一次是保存在/etc/letsencrypt目錄中席噩,建議定期備份。
Nginx 配置
$ cp chain.pem example.com.chain.crt
$ cp privkey.pem example.com.key
$ more ssl.conf
ssl_certificate certs/example.com.chain.crt;
ssl_certificate_key certs/example.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE
-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA
256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES
256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4;
$ more example.com.conf
server {
listen 80;
listen 443 ssl;
server_name app.example.com;
default_type text/html;
return 200 "are you ok ?";
include ssl.conf;
.....
reload nginx之后贤壁,瀏覽器訪問:
參考文檔
https://www.google.com
http://www.reibang.com/p/c5c9d071e395