介绍
一个 wildcard 证书是一种 SSL 证书,可以通过单个证书保护任何数量的子域,在需要支持多个子域的情况下,您可能需要一个 wildcard 证书,但不希望单独配置它们。
Let's Encrypt是一个SSL证书授予免费证书,使用自动化API。 在本教程中,您将按照以下步骤创建一个Let's Encrypt wildcard证书:
- 确保您的 DNS 设置正确 2. 安装需要完成基于 DNS 的挑战所需的 Certbot 插件 3. 授权 Certbot 访问您的 DNS 提供商 4. 检索您的证书
这些信息旨在用于任何Linux发行版和任何服务器软件,但您可能需要用进一步的文档填补一些空白,我们将链接到它。
前提条件
本教程假设您已经拥有以下内容:
- 安装的Certbot公用设备,`0.22.0'版本或以后版本。 如果您需要安装 Certbot 的帮助, 请访问我们的 [), 您可以在这里找到各种 Linux 分发和服务器的安装指南 。 一些常见的设置如下:
- [如何用Ubuntu 20.04加密方式保护Nginx(https://andsky.com/tech/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04)
- [如何用Ubuntu 20.04加密方式保护Apache(https://andsky.com/tech/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-20-04)
- [如何使用 Certbot 独立模式检索,让我们在 Ubuntu 18.04 上加密 SSL 证书] (https://andsky.com/tech/tutorials/how-to-use-certbot-standalone-mode-to-retrieve-let-s-encrypt-ssl-certificates-on-ubuntu-1804)
- 一个域名,以及Certbot支持的DNS供应商. 支持提供者列表参见 [Certbot's DNS 插件列表] (https://certbot.eff.org/docs/using.html# dns-plugins)
让我们从设置和测试我们的DNS记录开始。
步骤 1 – 设置 Wildcard DNS
在我们收集我们的 wildcard SSL 证书之前,我们应该确保我们的服务器响应多个子域上的请求。
1*.example.com. 3600 IN A 203.0.113.1
这个示例的 DNS 记录将匹配 one.example.com
和 two.example.com
. 它不会匹配 example.com
或 one.two.example.com
,因为 *
wildcard 只会扩展到一个主机名,而不会扩展到多个名称级别。
此外,一张 wildcard DNS 记录只能具有 one wildcard 字符,因此* .* .example.com
不允许。
请参阅您的 DNS 提供商的文档以设置正确的 DNS 条目. 您将在继续之前添加一个A
或CNAME
wildcard 记录。
如果您正在使用 DigitalOcean 来管理您的 DNS,请参阅 如何创建、编辑和删除 DNS 记录在我们的产品文档中获取更多信息。
要测试您的 wildcard DNS 是否按预期运行,请使用主机
命令查询几个主机名称:
1host one.example.com
此外,请记住,DNS记录有时需要几分钟才能传播到系统中。如果您刚刚添加了DNS记录并收到错误,请等待几分钟再试一次。
当您输入的主机名正确解决时,您将输出类似于以下:
1[secondary_label Output]
2one.example.com has address 203.0.113.1
否则,你会看到一个NXDOMAIN
错误:
1[secondary_label Output]
2Host one.example.com not found: 3(NXDOMAIN)
一旦您验证了多个子域正在解决到您的服务器,您可以继续到下一步,在那里您将配置Certbot连接到您的DNS提供商。
步骤2 — 安装正确的Certbot DNS插件
在发行证书之前,Let's Encrypt 执行一个挑战,以验证您是否控制您正在请求证书的主机。在 wildcard 证书的情况下,我们需要证明我们控制整个域. 我们通过响应基于 DNS 的挑战来做到这一点,Certbot 通过在目标域创建一个特殊的 DNS 记录来回答这个挑战。
为了连接到您的 DNS 提供商,Certbot 需要一个插件。 请参阅 Certbot 的 DNS 插件列表以获取 DNS 提供商的适当插件名称。
例如,DigitalOcean提供商被称为certbot-dns-digitalocean
。我们可以通过安装以下包在Ubuntu和Debian上安装certbot-dns-digitalocean
插件:
1sudo apt install python3-certbot-dns-digitalocean
其他插件应该遵循相同的命名格式. 如果您正在使用不同的服务,请将您的提供商的名称更换为上面的命令。
在 CentOS 和其他基于 RPM 的发行版中,安装命令可能是dnf
:
1dnf install python3-certbot-dns-digitalocean
或者YUM
:
1yum install python3-certbot-dns-digitalocean
您可能还需要在这些发行版上安装额外的包存储库,以便访问 Certbot 插件包。
要验证插件是否正确安装,您可以要求Certbot列出其当前的插件:
1certbot plugins
1[secondary_label Output]
2- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3* dns-digitalocean
4Description: Obtain certs using a DNS TXT record (if you are using DigitalOcean
5for DNS).
6Interfaces: IAuthenticator, IPlugin
7Entry point: dns-digitalocean =
8certbot_dns_digitalocean.dns_digitalocean:Authenticator
9
10* standalone
11Description: Spin up a temporary webserver
12Interfaces: IAuthenticator, IPlugin
13Entry point: standalone = certbot.plugins.standalone:Authenticator
14
15* webroot
16Description: Place files in webroot directory
17Interfaces: IAuthenticator, IPlugin
18Entry point: webroot = certbot.plugins.webroot:Authenticator
19- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
在上述输出中,第一个列出dns-digitalocean
插件,以及默认的standalone
和webroot
插件。
当您确认正确的插件已安装时,继续到下一步来配置它。
步骤 3 – 配置 Certbot 插件
由于Certbot需要连接到您的DNS提供商,并代表您创建DNS记录,您需要授权它这样做。
由于每个提供商都有不同的身份验证流程,请参阅您的特定Certbot DNS插件的文档(https://certbot.eff.org/docs/using.html# dns-plugins)有关您需要获取哪些代币或密钥的更多信息。
对于这个例子,我们将继续使用dns-digitalocean
插件,并将我们的身份证存储在~/certbot-creds.ini
文件中。
我们将使用nano
文本编辑器创建此文件:
1nano ~/certbot-creds.ini
这将打开一个新的空文本文件. 您将根据特定DNS提供商的指示添加您的信息. DigitalOcean需要一个单一的API代码,所以它将看起来像这样:
1[label ~/certbot-creds.ini]
2dns_digitalocean_token = 235dea9d8856f5b0df87af5edc7b4491a92745ef617073f3ed8820b5a10c80d2
请确保用您自己的信息替换上面的示例代币。
如果您正在使用),按ENTER
,然后按CTRL+X
退出。
创建文件后,您需要限制其权限,以便您的秘密不会泄露给其他用户。
1chmod 600 ~/certbot-creds.ini
一旦您已设置了您的凭证文件,您已经准备好实际要求证书。
步骤4 - 获取证书
在这一点上,获取您的 Let’s Encrypt 野卡证书与正常
非野卡证书类似。该过程的主要变化是指定基于 DNS 的挑战,并指向我们的 DNS 凭证文件。
1sudo certbot certonly \
2 --dns-digitalocean \
3 --dns-digitalocean-credentials ~/certbot-creds.ini \
4 -d '*.example.com'
请注意,您不能使用--nginx
或--apache
插件来自动配置这些服务器使用 wildcard 证书,而我们则使用certonly
命令来仅下载证书。
在运行上述命令时,您可能会收到一些问题来回答,如果这是您第一次运行Certbot。在回答这些问题后,Cerbot将执行挑战,Let's Encrypt服务器将验证它,您的新证书将下载并保存到 /etc/letsencrypt/
。
1[secondary_label Output]
2IMPORTANT NOTES:
3 - Congratulations! Your certificate and chain have been saved at:
4 /etc/letsencrypt/live/example.com/fullchain.pem
5 Your key file has been saved at:
6 /etc/letsencrypt/live/example.com/privkey.pem
7 Your cert will expire on 2021-09-27. To obtain a new or tweaked
8 version of this certificate in the future, simply run certbot
9 again. To non-interactively renew *all* of your certificates, run
10 "certbot renew"
11 - If you like Certbot, please consider supporting our work by:
12
13 Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
14 Donating to EFF: https://eff.org/donate-le
您已成功生成一张 wildcard SSL 证书!您的下一步是配置您的服务器应用程序以使用它. 我们将在下一节中链接到一些可以帮助此方面的资源。
结论
在本教程中,您配置了 Certbot 并从 Let's Encrypt 证书授权机构下载了 wildcard SSL 证书,您现在已经准备好配置您的服务器软件以使用该证书来保护其连接。
有关下载了哪些证书文件的更多信息,以及如何在Certbot自动更新您的证书时优雅地重新启动应用程序,请参阅本教程的步骤3和4 How To Use Certbot Standalone Mode to Retrieve Let's Encrypt SSL Certificates on Ubuntu 18.04。