Let's Encrypt 免费通配符 SSL 证书申请教程

在3月份的时候,Let’s Encrypt宣布支持通配符证书。见这里

什么是 Let’s Encrypt

Let’s Encrypt 是国外一个公共的免费 SSL 项目,由 Linux 基金会托管。它的来头不小,由 Mozilla、思科、Akamai、IdenTrust 和 EFF 等组织发起,目的就是向网站自动签发和管理免费证书。以便加速互联网由 HTTP 过渡到 HTTPS,目前 Facebook 等大公司开始加入赞助行列。

Let’s Encrypt 已经得了 IdenTrust 的交叉签名,这意味着其证书现在已经可以被 Mozilla、Google、Microsoft 和 Apple 等主流的浏览器所信任。用户只需要在 Web 服务器证书链中配置交叉签名,浏览器客户端会自动处理好其它的一切,Let’s Encrypt 安装简单,使用非常方便。

什么是通配符证书

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

申请通配符证书

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

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

获取 Certbot 客户端

这里
以nginx ubuntu 为例

安装

1
2
3
4
5
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx

使用

1
sudo certbot --nginx

申请通配符证书

原理

客户在申请 Let’s Encrypt 证书的时候,需要校验域名的所有权,证明操作者有权利为该域名申请证书。常见的两种验证方式:

  • example.com 下提供一个DNS TXT 记录
  • 提供一个http资源在一个众所周知的地址在http://example.com

通配符证书只支持第一种

1
./certbot-auto certonly  -d "*.example.com" --manual --preferred-challenges dns-01  --server https://acme-v02.api.letsencrypt.org/directory

参数说明

certonly 表示插件,Certbot 有很多插件。不同的插件都可以申请证书,用户可以根据需要自行选择。

  • -d 为哪些主机申请证书。如果是通配符,输入 *.example.com (根据实际情况替换为你自己的域名)。
  • --preferred-challenges dns-01: 使用 DNS 方式校验域名所有权。
  • --server: Let’s Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。

执行结果示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Requesting to rerun ./certbot-auto with root privileges...
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): xxx@mail.com

-------------------------------------------------------------------------------
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 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 EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y
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

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

Ndpg7dP38wrE3DZqlksBCbT_LDGNFgilPUoZcOdOdd4

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

Press Enter to Continue这里

在去DNS解析那里添加一条TXT记录,内容为上面那串乱码。子域名为_acme-challenge

添加之后用 dig命令检测下

运行

1
dig  -t txt _acme-challenge.example.com @8.8.8.8    

可以看到相关乱码记录,就算OK,可以按 Enter下一步

证书保存位置

1
2
3
4
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
...

这样证书就保存在/etc/letsencrypt/live/example.com/

证书续期

Let’s encrypt 的免费证书默认有效期为 90 天,到期后如果要续期可以执行:

1
$ certbot-auto renew

常用命令

1
2
./certbot-auto delete
./certbot-auto certificates

参考

其它

补充在2022_08_14。
这个时间点certbot已经代替certbot-auto

1
2
sudo apt install certbot
sudo certbot certonly -d "*.example.com" --manual --preferred-challenges dns-01

其它保持一样


Let's Encrypt 免费通配符 SSL 证书申请教程
https://blog.fengcl.com/2018/05/04/letsencrypt-wildcard-certificate/
作者
frank
发布于
2018年5月4日
许可协议