猛火Fierflame

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

关于 Let’s Encrypt

我们在部署 HTTPS 网站的时候需要证书,证书可以自行签发,也可以由 CA 机构签发签发,但是一般使用由个人或非可信的 CA 机构签发签发的证书默认都会被浏览器组织访问。
大部分传统的 CA 机构签发证书是收费的,目前可以提供免费的正数主要有:

Let’s Encrypt 是非盈利性的组织,设计了一个 ACME 协议,通过 ACME 协议使得证书申请、证书更新、证书撤销等过程可以完全由程序自动完成。目前 ACME 协议有两个版本 v1 和 v2。其中支持通配符证书的只有 v2 版本。

通配符证书

从可以支持的域名的角度分类,证书可以分为以下三类:

安装 certbot-auto

certbot-auto 本身只是一个 shell 脚本,通过此脚本,我们可以实现证书的申请等操作。关于 certbot-auto 的介绍,可以查看 https://certbot.eff.org/

通过以下命令,可以下载certbot-auto脚本并为其添加执行权限:

  1. wget https://dl.eff.org/certbot-auto
  2. chmod a+x certbot-auto

下载完成后,可以将其移动到/usr/bin/usr/local/bin等环境路径下使用,也可以直接使用。此教程按照将其移动到环境路径下编写。如果您不移动,记得在命令前加入.\才能使用。

第一次使用 certbot-auto 时,其会调用系统安装工具(如yumapt)安装一些必要的工具,安装过程中会有相应指令的交互操作,请配合安装。在必要工具安装完成后(或长时间未用,有更新的情况下),certbot-auto会下载用到的最新的模块已进行证书申请。两次下载安装,会因为用户所用网络、安装包大小等原因,需要的时间不同。

certbot-auto 教程

certbot-auto命令示例:

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

参数说明

  1. Saving debug log to /var/log/letsencrypt/letsencrypt.log
  2. Plugins selected: Authenticator manual, Installer None
  3. Enter email address (used for urgent renewal and security notices) (Enter 'c' to
  4. cancel): www@fierflame.com # <---- 这个不一定有,如果有,则输入邮箱,然后回车
  5. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6. Please read the Terms of Service at
  7. https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
  8. agree in order to register with the ACME server at
  9. https://acme-v02.api.letsencrypt.org/directory
  10. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  11. (A)gree/(C)ancel: A # <---- 必须是 A, 表示同意服务协议
  12. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. Would you be willing to share your email address with the Electronic Frontier
  14. Foundation, a founding partner of the Let's Encrypt project and the non-profit
  15. organization that develops Certbot? We'd like to send you email about our work
  16. encrypting the web, EFF news, campaigns, and ways to support digital freedom.
  17. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. (Y)es/(N)o: N # <---- 问你要不要接收电子前沿基金的一些推送邮件,这个Y N 都可以,此项不一定会出现
  19. Plugins selected: Authenticator manual, Installer None
  20. Obtaining a new certificate
  21. Performing the following challenges:
  22. dns-01 challenge for fierflame.com
  23. dns-01 challenge for fierflame.com # <------ 这里显示的是认证的域名
  24. # <!----- 如果设置了多个域名,会显示多条,特别的,对于通配符域名会显示父域名 ---------->
  25. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  26. NOTE: The IP of this machine will be publicly logged as having requested this
  27. certificate. If you're running certbot in manual mode on a machine that is not
  28. your server, please ensure you're okay with that.
  29. Are you OK with your IP being logged?
  30. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  31. (Y)es/(N)o: y # <---- 问是否要绑定当前机器的IP,现在必须绑定,也就是必须是 Y
  32. # <!----- 从这里开始需要验证域名所有权,采用的是验证 DNS TXT 记录的方式验证 ---------->
  33. # <!----- 如果设置了多个域名,对于每一个都会对需要单独设置(通配符域名视为一个域名) ---------->
  34. # <!----- 设置的 TXT 记录在全部被验证后才可以删除,当然你也可以不删除 ---------->
  35. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  36. Please deploy a DNS TXT record under the name
  37. _acme-challenge.fierflame.com with the following value: # <------ 需要设置 TXT 记录的命名
  38. bY1cQdzRXC4GD-0TPYV3gSnNI2fReB4CPAJHU9QJ1QA # <------ TXT 记录值
  39. # 特别说明,对于同一个域名可以添加多个相同优先级的 TXT 记录
  40. Before continuing, verify the record is deployed.
  41. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  42. Press Enter to Continue # <------ 设置完成后点击回车,必要的话,可以先按照后文提到的方法进行验证后再点击回车
  43. # <!--- 如果还有域名没有设置,上面部分要重复操作,但是每次的域名和 TXT 记录值一般都不同 -------->
  44. Waiting for verification... # <------ 进行最后的统一验证
  45. Cleaning up challenges
  46. # <------ 如果没有其他问题,则已经生成证书,此时 TXT 记录可以删除了 --------->
  47. IMPORTANT NOTES:
  48. - Congratulations! Your certificate and chain have been saved at:
  49. /etc/letsencrypt/live/fierflame.com/fullchain.pem # <---- 证书生成的位置
  50. Your key file has been saved at:
  51. /etc/letsencrypt/live/fierflame.com/privkey.pem # <---- 密钥生成的位置
  52. # <------ 实际上除了生产以上两个文件外,还会生成其他的几个文件,但所给的文件的所属目录一定相同 ------->
  53. # <------ 在这里的目录为`/etc/letsencrypt/live/fierflame.com`,后面介绍将用到此目录路径 ------->
  54. Your cert will expire on 2019-06-27. To obtain a new or tweaked # <---- 有效期截止日期
  55. version of this certificate in the future, simply run certbot-auto
  56. again. To non-interactively renew *all* of your certificates, run
  57. "certbot-auto renew"
  58. - If you like Certbot, please consider supporting our work by:
  59. Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
  60. Donating to EFF: https://eff.org/donate-le

解析测试

可以使用dig命令测试
具体格式为:

  1. dig -t txt 域名 @DNS

其中:

例如执行dig -t txt _acme-challenge.fierflame.com @8.8.8.8得到的结果是:

  1. ; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> -t txt _acme-challenge.fierflame.com @8.8.8.8
  2. ;; global options: +cmd
  3. ;; Got answer:
  4. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5608
  5. ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
  6. ;; OPT PSEUDOSECTION:
  7. ; EDNS: version: 0, flags:; udp: 512
  8. ;; QUESTION SECTION:
  9. ;_acme-challenge.fierflame.com. IN TXT
  10. ;; ANSWER SECTION:
  11. _acme-challenge.fierflame.com. 599 IN TXT "bY1cQdzRXC4GD-0TPYV3gSnNI2fReB4CPAJHU9QJ1QA"
  12. _acme-challenge.fierflame.com. 599 IN TXT "a0vjK4_laS7vnGJNEglqzpbG3UagxCjAoD2hmPszNYw"
  13. ;; Query time: 95 msec
  14. ;; SERVER: 8.8.8.8#53(8.8.8.8)
  15. ;; WHEN: Fri Mar 29 10:14:19 CST 2019
  16. ;; MSG SIZE rcvd: 171

我们将以;开头的部分和空行过滤掉,得到:

  1. _acme-challenge.fierflame.com. 238 IN TXT "bY1cQdzRXC4GD-0TPYV3gSnNI2fReB4CPAJHU9QJ1QA"
  2. _acme-challenge.fierflame.com. 238 IN TXT "a0vjK4_laS7vnGJNEglqzpbG3UagxCjAoD2hmPszNYw"

其中_acme-challenge.fierflame.com.及为我们查询的域名,最后引号内的为设置的TXT记录,如果其记录复合,则表示设置成功

证书的说明

上面示例中的目录路径为/etc/letsencrypt/live/fierflame.com
但是此目录中所包含的文件不只是以上几个文件,在此目录下执行ls我们可以看到,有以下几个文件:

对于这几个文件,不要删除或移动,使用时,建议直接配置为此文件或者做一软连接后使用。

上一篇:PWA: 渐进式 Web 应用
下一篇:HTML5 协议处理: Web-based Protocol Handlers