前言

  很早就听说有免费的通配符证书了,一直也懒得弄,今天就抽一点时间配置一下,顺便水一篇文章。本文就以博主域名v2ex.cc为例来演示如何申请免费的通配符证书。

  Let's Encrypt 宣布ACME v2正式支持通配符证书,并将继续清除Web上采用HTTPS的障碍,让每个网站轻松获取管理证书,我们普通用户终于可以拥有免费Wildcard Certificate,俗称野卡。

  通配型证书(wildcard certificate)是在一个域及其所有子域上应用的数字证书。通配型符号由一个星号和域名前的一段时间构成。安全套接层(SSL)证书常运用通配符来扩展子域的SSL加密。

  通配符SSL证书可以同时保护一个域名下的2级子域名网站,比如*.example.com,对子域名网站是没有数量限制,用户可以随时添加自己的子域名网站。

注:为*.example.com域名申请的通配符证书不适用于example.com,如有需要则需要单独申请。

什么是 Let’s Encrypt?

  部署HTTPS网站的时候需要证书,证书由CA机构签发,大部分传统CA机构签发证书是需要收费的,这不利于推动HTTPS协议的使用。

  Let’s Encrypt 也是一个CA机构,但这个CA机构是免费的。也就是说签发证书不需要任何费用。Let’s Encrypt 由于是非盈利性的组织,需要控制开支,他们搞了一个非常有创意的事情,设计了一个ACME协议。

  那为什么要创建ACME协议呢,传统的CA机构是人工受理证书申请、证书更新、证书撤销,完全是手动处理的。而ACME协议规范化了证书申请、更新、撤销等流程,只要一个客户端实现了该协议的功能,通过客户端就可以向 Let’s Encrypt 申请证书,也就是说Let’s Encrypt CA完全是自动化操作的。

  任何人都可以基于ACME协议实现一个客户端,官方推荐的客户端是Certbot 。

什么是通配符证书

在没有出现通配符证书之前,Let’s Encrypt 支持两种证书。

1)单域名证书:证书仅仅包含一个主机。

2)SAN 证书:域名通配符证书类似DNS解析的泛域名概念,通配符证书就是证书中可以包含一个通配符(*.exmaple.com)。主域名签发的通配符证书可以在所有子域名中使用,比如www.example.combbs.example.com

申请通配符证书

  Let’s Encrypt 上的证书申请是通过ACME协议来完成的。ACME协议规范化了证书申请、更新、撤销等流程,实现了Let’s Encrypt CA自动化操作。解决了传统的CA机构是人工手动处理证书申请、证书更新、证书撤销的效率和成本问题。

  ACME v2 是ACME 协议的更新版本,通配符证书只能通过ACME v2获得。要使用ACME v2协议申请通配符证书,只需一个支持该协议的客户端就可以了,官方推荐的客户端是Certbot

  客户在申请 Let’s Encrypt 证书的时候,需要校验域名的所有权,证明操作者有权利为该域名申请证书,目前支持三种验证方式:

dns01:给域名添加一个DNS TXT记录。
http01:在域名对应的Web服务器下放置一个HTTP well-known URL资源文件。
tls-sni01:在域名对应的Web服务器下放置一个HTTPS well-known URL资源文件。

申请通配符证书,只能使用dns验证的方式

下面开始申请

获取Certbot客户端

[root@www ~]# wget -c https://dl.eff.org/certbot-auto -P /usr/local/bin/     #下载 Certbot 客户端
[root@www ~]# chmod a+x /usr/local/bin/certbot-auto     #设为可执行权限
[root@www ~]# certbot-auto --version     #安装Certbot
[root@www ~]# certbot 1.6.0     #安装成功后显示的版本号(2020-07-27)

申请证书

[root@www ~]# certbot-auto certonly  -d *.v2ex.cc --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
  • certonly[安装模式],Certbot有安装模式和验证模式两种类型的插件。
  • -d[为哪些域名申请证书],如果是通配符,输入*.example.com
  • --manual[手动安装],Certbot有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择。
  • --preferred-challenges dns[DNS验证],使用DNS方式校验域名所有权。
  • --server[指定ACME版本],Let's Encrypt ACME v2版本使用的服务器不同于v1版本,需要显示指定。
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): manager@v2ex.cc     #用于安全以及续约通知的邮箱

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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     #是否同意相关协议条款

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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     #是否订阅相关的邮件
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for v2ex.cc

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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     #询问是否对域名和机器(IP)进行绑定

以上确认后继续,以下提示要求配置 DNS TXT 记录,从而校验域名所有权,也就是判断证书申请者是否有域名的所有权。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.v2ex.cc with the following value:

8A20NkS2XkDtylqNERp46QylZHPS_b8WjjQM8OowvLI

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue     #这里切记自己验证生效后再回车执行

上面输出要求给 _acme-challenge.v2ex.cc 添加一条值为 8A20NkS2XkDtylqNERp46QylZHPS_b8WjjQM8OowvLI 的 TXT 记录,在没有确认 TXT 记录生效之前不要回车执行。请自行登录域名管理系统添加,此处不再赘述。

查询TXT解析记录

博主建议直接使用nslookup命令

C:\Users\Administrator>nslookup
默认服务器:  UnKnown
Address:  10.0.100.1

> set type=txt
> _acme-challenge.v2ex.cc
服务器:  UnKnown
Address:  10.0.100.1

非权威应答:
_acme-challenge.v2ex.cc text =

        "8A20NkS2XkDtylqNERp46QylZHPS_b8WjjQM8OowvLI"
>

dig命令查询

[root@www ~]# dig txt _acme-challenge.v2ex.cc @8.8.8.8
-bash: dig: command not found     #如果出现这个提示,说明你的系统未安装dig命令
[root@www ~]# yum install bind-utils     #安装dig命令
[root@www ~]# dig txt _acme-challenge.v2ex.cc @8.8.8.8     #输入dig命令查询

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> txt _acme-challenge.v2ex.cc @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34818
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.v2ex.cc.       IN      TXT

;; ANSWER SECTION:
_acme-challenge.v2ex.cc. 599    IN      TXT     "8A20NkS2XkDtylqNERp46QylZHPS_b8WjjQM8OowvLI"

;; Query time: 28 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jul 27 08:47:35 CST 2020
;; MSG SIZE  rcvd: 108

确认生效后,回车执行,输出如下信息

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/v2ex.cc/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/v2ex.cc/privkey.pem
   Your cert will expire on 2020-10-24. 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"
 - 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

此时我们的证书已经申请成功了,证书都保存在如下目录中:

[root@www ~]# tree /etc/letsencrypt/live/v2ex.cc/
/etc/letsencrypt/live/v2ex.cc/
├── cert.pem -> ../../archive/v2ex.cc/cert.pem
├── chain.pem -> ../../archive/v2ex.cc/chain.pem
├── fullchain.pem -> ../../archive/v2ex.cc/fullchain.pem
├── privkey.pem -> ../../archive/v2ex.cc/privkey.pem
└── README

一般nginx配置需要用到就是fullchain.pemprivkey.pem使用vim查看证书内容即可,各位客官自行取用即可。

证书续签相关

查看证书有效期,到期时间

[root@www ~]# cd /usr/local/bin/
[root@www bin]# certbot-auto certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: v2ex.cc
    Serial Number: ****dfe582****228e0a5c****4164c****
    Domains: *.v2ex.cc
    Expiry Date: 2020-10-24 23:16:18+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/v2ex.cc/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/v2ex.cc/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

证书续约

[root@www ~]# cd /usr/local/bin/
[root@www bin]# certbot-auto renew -v     #手动更新
[root@www ~]# cd /usr/local/bin/
[root@www bin]# certbot-auto renew --dry-run     #更新证书

如果不需要返回的信息,可以用静默方式certbot renew --quiet

[root@www ~]# cd /usr/local/bin/
[root@www bin]# certbot-auto renew --quiet --no-self-upgrade     #自动更新

定时更新,也可以直接放入宝塔计划任务中执行

[root@www ~]# crontab -e     #加入定时任务
0 4 1 */2 *  /usr/bin/certbot-auto  renew --quiet     #每两个月的一号凌晨4点更新一次
本文部分内容转载自简书,作者:YichenWong
如果觉得我的文章对你有用,请随意赞赏