使用Certbot申请SSL证书

阿里云的免费SSL证书的时长从一年下调到三个月了,而且还没得免费申请通配符证书,这样每三个月就得换一批证书,而且一个三级域名就要一个证书,有点太麻烦了,而且不够用。

因此一个不限量申请证书,可以续签的证书申请方式变成了刚需,网上冲浪刚刚好了解到Let’s Encrypt这个CA,正好解决了阿里云免费SSL证书不足的问题,让我们能够更加方便地为多个域名申请和续签SSL证书。

安装Certbot

先在服务器上下载安装对应版本的Certbot

安装完毕后,运行certbot --version​检查安装是否成功。

申请证书

certbot申请证书使用certbot certonly(仅申请证书)命令,有两个模式:

  • standalone模式

    需要关停服务器当前运行的web服务,腾出80端口给Certbot使用

  • webroot模式

    会在当前web的80端口的静态资源文件夹创建Certbot的校验文件

为了不影响到网站的访问,这里我们选择使用webroot模式:

1
certbot certonly --webroot
​​​​​

运行命令后,certbot会提示输入要申请证书的域名,然后输入当前域名80端口对应的根目录即可,certbot会在目录下创建一个校验文件,供CA进行校验。

校验成功后就申请成功了。申请成功后,会显示申请到的证书存放的位置:

​​​​​

可以看到都是快捷方式来的,指向的是certbot目录下的archive文件夹。

里面的README文件的内容:

This directory contains your keys and certificates.

privkey.pem​​ : the private key for your certificate.(私钥)
fullchain.pem​​: the certificate file used in most server software.(证书)
chain.pem​​ : used for OCSP stapling in Nginx >=1.3.7.(OCSP装订证书)
cert.pem​​ : will break many server configurations, and should not be used
without reading further documentation (see link below).(域名证书?应该没什么用,readme里也提示不应该用这个)

WARNING: DO NOT MOVE OR RENAME THESE FILES!
Certbot expects these files to remain in this location in order
to function properly!

文件里提示我们也不要移动或删除这些文件,那就不用管了。

Nginx下使用SSL证书,只需要用到前两个文件就好了。我们把这两个文件路径复制下来,修改一下Nginx配置文件,找到对应的server,修改一下证书路径即可:

1
2
ssl_certificate #certbot的证书路径;
ssl_certificate_key #certbot的私钥路径;

修改完成后重启Nginx,再打开网站,已经发现证书已经变成Let’s Encrypt的证书了:

​​​​​​

certbot也已经启动了定时任务,会对证书自动续签。

申请通配符证书

前面我们申请的是普通证书,只能服务一个三级域名,如果三级域名很多,那申请的数量也很多。解决这个问题,我们需要申请通配符证书。

Let’s Encrypt对域名进行校验有两种模式,一种是http模式,一种是dns模式,http模式就是在网站根目录下生成校验文件,让CA请求来进行校验。dns模式就是添加一条TXT记录。

根据Certbot文档的说明:

This category of plugins automates obtaining a certificate by modifying DNS records to prove you have control over adomain. Doing domain validation in this way is the only way to obtain wildcard certificates from Let’sEncrypt.

为了证明你有对域名的所有控制权,因此DNS模式是唯一可以申请通配符证书的模式。(参考Certbot文档Getting certificates (and choosing plugins)章节)。

申请通配符证书其实也很简单,使用下面的命令即可:

1
2
3
4
5
6
certbot certonly -d "*.example.com" -d example.com --manual --preferred-challenges dns-01 # 注意要改成自己的域名
参数的说明:
certonly:仅申请证书
-d:申请证书的域名
--manual:手动模式,只有手动模式才支持--preferred-challenges,自定义校验模式,这里如果不写manual,后面的参数会直接无视
--preferred-challenges dns-01:自定义校验模式,使用dns模式进行校验
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
# certbot certonly -d "*.example.com" -d example.com --manual --preferred-challenges dns-01
Saving debug log to C:\Certbot\log\letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
It contains these names: example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:
# 会提示给域名解析加一个TXT记录,照着提示加即可
_acme-challenge.******.com.

with the following value:

obhvT0vzlPQANt0XsCHj5xGOj2YUacKnlprinGpfwCg

# 加完可以在 https://toolbox.googleapps.com/apps/dig/ 谷歌管理员工具箱验证一下添加成功没
Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.diaoan.xyz.
Look for one or more bolded line(s) below the line ANSWER. It should show the
value(s) you ve just added.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

Successfully received certificate.
Certificate is saved at: \Certbot\live\******\fullchain.pem
Key is saved at: \Certbot\live\******\privkey.pem
This certificate expires on 2024-04-05.
These files will be updated when the certificate renews.
NEXT STEPS:
This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an au
thentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same cert
bot command before the certificate's expiry date.

最后的提示说明,manual模式申请的的证书是不会自动续签的,但是我们也可以钩子来实现续签。其实这里的申请也还算比较简单,每三个月重新申请一下就好了,也就几分钟的事。

申请成功后,和普通证书一样,替换一下证书路径,重启服务器就大功告成了。

问题

现在有一个问题,在证书还没到期前,重复运行上面的命令对证书进行重申,发现是不用进行添加DNS解析记录的,可以重申成功,可是理论上应该是需要更新解析记录的。具体情况怎么样还是三个月后再看吧。。。