免费https证书

这是搭建 docker registry 的前置文章,用来描述如何简单获取一个安全、可靠、免费的 https 证书。html

虽然你可使用自签署证书来提供 https,可是因为自签署证书的不可靠性,真实用起来受到重重限制,特别是谷歌浏览器对于自签署证书的限制很是大。所以,简单获取免费、可靠 https 证书的手段你必须具有。nginx

近几年来,各大互联网巨头都在推动 https 的使用,谷歌浏览器更是将非 https 网站标记为不安全,可见其决心。可是证书申请也是一笔不小的费用,有些创业公司还不知道能活多久呢,能省则省,这个费用天然不肯意花。git

并且就算你申请到一个证书,一旦谷歌认为你证书的 CA(证书颁发机构)不符合它的要求,你即便是 https,它照样给你标记为不安全。github

基于这样或那样的缘由,免费、可靠的 https 证书势在必行。因而互联网安全研究小组(缩写 ISRG)于 2015 年三季度推出 Let's Encrypt 这样的数字证书认证机构(其实就一 CA),为你免费颁发证书。web

这个小组的背后站着电子前哨基金会、Mozilla 基金会、Akamai 以及思科这些大佬,所以绝对安全可靠。docker

如何作

既然有这种机构了,我应该怎么申请证书呢?会不会很麻烦啊?放心,用起来很是简单。首先,Let's Encrypt 本身确定是有一个根证书的,由于只有根证书才能给你签发证书。shell

而后呢,你须要发送签署请求给它,它验证 ok 以后就给你发证书了,这一切 Let's Encrypt 内部都经过自动化来完成。因为须要自动化完成,因此证书的申请、续期以及销毁都是经过精心设计的 ACME 协议来完成,这个协议你不须要懂,由于客户端工具会自动帮你发起。apache

你们应该都清楚,你申请证书的时候是要提供主机名的,好比 www.ntpstat.com,由于主机名是须要写进证书中的。还有一个种是只提供通配主机名而非具体主机名的,好比 *.ntpstat.com,这种叫泛域名证书或者通配符证书,也就是只要是这个域下的全部二级域名均可以使用这个证书。json

好比说我只要申请了 *.ntpstat.com 这个泛域名证书,那么我下面的 www.ntpstat.com、mail.ntpstat.com、file.ntpstat.com 等二级域名均可以使用这个证书了。相对来讲,泛域名证书用起来更爽。vim

具体怎么作呢?先拿具体的主机名证书申请为例,咱们先将证书签署请求发送到 Let's Encrypt,发送请求很简单,有客户端工具能够给你完成(下面会提到)。咱们要作的就是提供给它验证的方式,Let's Encrypt 得验证你就是申请这个主机名的人。

这个你们应该能理解吧,不能说你要签署个什么证书它就签给你对吧。验证有两种方式,假如我如今申请的主机名是 www.ntpstat.com:

  • 第一种验证方式:Let's Encrypt 会经过访问 http://www.ntpstat.com/.well-known/acme-challenge 由客户端工具生成的验证文件,验证经过会在服务器上生成证书文件。所以你在使用客户端时须要指定你 web 服务器的根目录,这样它才可以在相应目录下生成验证文件;
  • 第二种验证方式:若是你申请的主机名并不对外,只是内部使用,或者你申请的是泛域名证书时,就可使用第二个验证方式了。你须要在公网的 dns 域下面添加一条 TXT 记录,好比我就要在 *.ntpstat.com 这个域下添加一个 TXT 记录,记录的内容客户端工具会给出。Let's Encrypt 解析出来的 TXT 记录若是和客户端工具给出的一致,那么一样生成证书文件。

我的感受第二种用的比较多,由于它可使用泛域名证书以及内部使用。可是无论哪种,申请下来的证书的有效期只有 90 天,到期就要续期了。可是不用担忧,你写个定时任务就能够了,客户端工具会自动帮你续期。

客户端工具

说到这里,想必你对 Let's Encrypt 有了个大体的了解了,接下来就要提到它的客户端工具了。Let's Encrypt 官方的客户端工具是 certbot,用起来挺简单;另外一种是国人写的 acme.sh,听说比官方工具更好用。

只要你懂得 ACME 协议,也能够写一个客户端工具,因此可能还有另外的工具,可是这里就很少提了。

certbot

官方的客户端工具。

对于第一种验证方式,certbot 自己会使用 nginx/apache 启动一个 web 服务器,并自动生成验证文件,对外提供服务。看官方的意思是这个 web 服务器只提供服务于验证请求,其余请求一律不响应。固然我没有看它的 nginx 配置文件,不知道是否是真的如此,这种模式称为 Standalone。若是你要使用这种模式,那么只须要在第一次请求证书以及在后续续期期间将 web 服务器启动便可,平时彻底能够处于关闭状态。而且你得确保系统的 80 和 443 端口没有被其余服务监听。

固然对于一些我的服务器来说,可能就一台服务器,而且 80 端口正在提供服务,不可能为了申请证书就直接将服务关了。即便你如今无所谓,后面续期你又得关,总之是很麻烦的。因此 certbot 容许你使用当前的 web 服务进行验证,只不过你要指定你 web 服务器的根目录,它会建立 .well-known 目录,并在其下生成验证文件。这种模式称为 Webroot

这两种模式都属于第一种验证方式,只不过有两种实现方式而已。

certbot 依赖 Python2.7 或者 Python3.4+,因此确保你的系统符合要求。

OK,咱们先下载。咱们不直接下载 certbot,而是下载 certbot-auto,它是一个 shell 脚本,对 certbot 作了一层封装,它用起来和 certbot 同样,可是执行的时候它会检测本地的 certbot 版本,若是本地没有就安装;本地有可是不是最新版本的话,它会自动将其升级。安装或升级以后,会将传递给 certbot-auto 的全部参数都传递给 certbot 执行。

本人使用的系统是 CentOS7:

# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
复制代码

由于客户端工具须要联网给 Let's Encrypt 发送证书签署请求,所以请确保操做系统有网络链接。假如没有,能够参考我以前的搭建代理服务让内网上网,经过在一台能上网的服务器上搭建代理服务让其余服务器经过它来上网。

# wget https://dl.eff.org/certbot-auto
# chmod a+x ./certbot-auto
# ./certbot-auto --help
复制代码

你能够检测这个脚本的完整性:

# wget -N https://dl.eff.org/certbot-auto.asc
# gpg2 --keyserver pool.sks-keyservers.net --recv-key A2CFB51FA275A7286234E7B24D17C995CD9775F2
# gpg2 --trusted-key 4D17C995CD9775F2 --verify certbot-auto.asc certbot-auto
复制代码

最后一个命令的输出应该是这样:

gpg: Signature made Wed 02 May 2018 05:29:12 AM IST
gpg:                using RSA key A2CFB51FA275A7286234E7B24D17C995CD9775F2
gpg: key 4D17C995CD9775F2 marked as ultimately trusted
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   2  signed:   2  trust: 0-, 0q, 0n, 0m, 0f, 2u
gpg: depth: 1  valid:   2  signed:   0  trust: 2-, 0q, 0n, 0m, 0f, 0u
gpg: next trustdb check due at 2027-11-22
gpg: Good signature from "Let's Encrypt Client Team <letsencrypt-client@eff.org>" [ultimate]
复制代码

第一种验证方式

第一种验证方式是直接在已存在的 web 服务器上生成验证文件让 Let's Encrypt 访问验证,所以你的服务器必需要有外网 ip,且你申请 https 证书的域名可以解析到这个 ip。好比我如今要申请 www.ntpstat.com 的证书,Let's Encrypt 会访问 www.ntpstat.com/.well-known/acme-challenge 进行验证。

这里只介绍使用 Webroot 的方式,Standalone 就很少提了,我也没有弄过。

我如今安装一个 nginx 来表示我本地已经存在 web 服务器了:

# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

# yum install -y nginx
# systemctl enable nginx
# systemctl start nginx
复制代码

由于我用于签署证书的域名是 www.ntpstat.com,此时能够直接访问了。

访问 ok,而且还被浏览器标记为了避免安全,嗯,立刻它就安全了。使用客户端工具发出证书签署请求:

# ./certbot-auto certonly --webroot -w /usr/share/nginx/html -d www.ntpstat.com
复制代码

参数说明:

  • certonly:表示只生成证书而不安装 web 服务器,虽然它也能够经过 nginx 插件来提供 web 服务,可是仍是本身安装来的放心;
  • --webroot:使用 webroot 插件,这是配合 certonly 使用的;
  • -w--webroot-path 的缩写,用来指定 web 服务器的根目录,nginx 默认根目录是 /usr/share/nginx/html;
  • -d:指定用于访问的主机名。

第一次执行上面的命令它会帮你安装 certbot,安装完成以后开始执行,下面是它的输出信息:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): # 让你输入邮箱地址,这个邮箱用来发送续期和安装通知类的邮件,固然你能够输入 c 来取消

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 # 让你赞成注册 ACME server,赞成就是了

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 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:
http-01 challenge for www.ntpstat.com
Using the webroot path /usr/share/nginx/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.ntpstat.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.ntpstat.com/privkey.pem
   Your cert will expire on 2019-04-06. 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"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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 复制代码

OK,这就签署完成了,证书在 /etc/letsencrypt/live/www.ntpstat.com/fullchain.pem,对应的私钥文件为 /etc/letsencrypt/live/www.ntpstat.com/privkey.pem。上面还介绍了这个证书的有效期,而且还告诉你须要只须要执行 certbot-auto renew 就能够了。

既然证书都有了,那就 https 搞起呗。

修改 nginx 配置文件:

# vim /etc/nginx/conf.d/ssl.conf
server {
    listen   *:443;
    server_name  www.ntpstat.com;

    ssl on;
    ssl_certificate      /etc/letsencrypt/live/www.ntpstat.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.ntpstat.com/privkey.pem;

    location / {
        root /usr/share/nginx/html;
    }
}
复制代码

浏览器使用 https 访问就能够看到链接是安全的。

固然,你可能想要 http 访问直接跳转到 https,很简单,在 http 访问中加个跳转就能够了。

# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.ntpstat.com;

    # 新增一行,强制跳转 https
    rewrite ^(.*)$ https://$host$1 permanent;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
复制代码

验证完成后,生成的验证目录都会自动删除掉。

第二种验证方式

能够看到,很简单就完成了针对单个域名的 https 证书获取。这可能在你建本身的网站方面有用,由于你对外提供了 web 服务。可是当你的证书只是内部使用,并无对外提供 web 服务,let's encrypt 服务器根本访问不到,好比 docker registry、ldap https 等等这样的,又或者是签署泛域名证书这样的场景,第一种验证方式就不可用了。固然,Let's Encrypt 确定是了解你们需求的,所以如今就来说讲它的第二种验证方式。

前面也提到了,第二种验证方式就是经过添加 dns 的 TXT 记录来完成。这就要求你的域名必须可以在公网上面被解析,所以你的一级域必须是 com、cn、net、pro 等公网上可以买的到域,不然 Let's Encrypt 发起个 TXT 记录的解析不可能收到结果的。

本身内部搭建的 DNS 就不用想了,由于外部访问不到。

以前的域名咱们先无论,从新签署一个泛域名的吧,这样好验证。

# ./certbot-auto certonly --manual --preferred-challenges dns
复制代码

先说说选项:

  • certonly:这个前面也提到了,只签署证书不须要 http 服务;
  • --manual:用于当前这种经过 dns 验证的方式,它还提供另外一种 http 的方式验证,好比你如今运行 certbot 命令的机器并非你提供 web 服务的那一台,固然这不重要;
  • --preferred-challenges:配合 --manual 使用的,由于我们要经过 dns 验证,因此选择 dns。

这个命令的执行须要花点时间,多是用在检测本地的 certbot 版本。

如下是命令的输出内容:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): *.ntpstat.com # 让你输入你的域,由于签署的是泛域名证书,所以须要加上 *
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for ntpstat.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: n
Cleaning up challenges
Must agree to IP logging to proceed
[root@host ~]# ./certbot-auto certonly --manual --preferred-challenges dns
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): ntpstat.com
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for ntpstat.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 # 意思是这个机器的 ip 已经被公开记录为请求这个证书,有点没搞懂。但你只能输入 y,不然命令执行终止

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.ntpstat.com with the following value: # 给出了 TXT 记录的名称和对应的值

me0g64A6vImOjdeC0yyG26m_LLxtQPELWXdInzqynVk

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
复制代码

先别急着回车,咱们先添加它的 dns 记录。你须要登陆你的域名运营商,而且你的域名下面添加一条记录。根据我上面生成的信息,我须要在个人 ntpstat.com 这个域下面增长一个 TXT 记录,名称是 _acme-challenge(后面的 .ntpstat.com 不要加上去),值为 me0g64A6vImOjdeC0yyG26m_LLxtQPELWXdInzqynVk

而后使用 dig 命令检测一把:

# dig -t TXT _acme-challenge.ntpstat.com

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> -t TXT _acme-challenge.ntpstat.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64655
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1452
;; QUESTION SECTION:
;_acme-challenge.ntpstat.com.	IN	TXT

# 看 ANSWER SECTION 这个字段
;; ANSWER SECTION:
_acme-challenge.ntpstat.com. 3582 IN	TXT	"me0g64A6vImOjdeC0yyG26m_LLxtQPELWXdInzqynVk"

;; Query time: 5 msec
;; SERVER: 1.1.1.1#53(1.1.1.1)
;; WHEN: Sun Jan 13 00:22:13 EST 2019
;; MSG SIZE  rcvd: 112
复制代码

能够查看解析 OK,而后就能够回车了。

Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/ntpstat.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/ntpstat.com/privkey.pem
   Your cert will expire on 2019-04-13. 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 复制代码

签署成功!证书的路径也已给出了。那么泛域名证书就签署完毕了,那么这个域下面的任何三级域均可以使用这个证书,好比 blog.ntpstat.com、www.ntpstat.com、file.ntpstat.com 等。

咱们如今就能够修改 nginx 配置文件,将证书换成新证书进行测试。

# vim /etc/nginx/conf.d/ssl.conf
server {
    listen   *:443;
    server_name  www.ntpstat.com;

    ssl on;
    ssl_certificate      /etc/letsencrypt/live/ntpstat.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/ntpstat.com/privkey.pem;

    location / {
        root /usr/share/nginx/html;
    }
}
复制代码

只须要换个证书便可,其余什么都不须要改。reload nginx 以后,直接访问 https://www.ntpstat.com。固然,你须要换成你的域名。

能够看到,照样是安全的。你其实能够添加个三级域名的 A 记录,好比 file.DOMAIN.com(或者本地修改 hosts 文件),而后使用这个域名进行 https 访问看是否存在问题,这也就可以验证泛域名证书是否有效了。

证书续期

证书默认三个月到期,certbot 提供了证书续期的方法,使用 renew 子命令。

./certbot-auto renew
复制代码

简单的不行!以前使用的插件和选项它会沿用,它不一样于 certonly,它会检查当前机器上使用 certbot 申请的全部证书是否过时,而不是单单只针对一个。

只有当证书有效期不足 30 天后这个命令才工做,所以你能够写个定时任务执行这个命令,能够天天执行也能够每周或每个月执行,随便你了。你如今执行的话,它会提示证书还未到失效期,直接就跳过了。

使用 crontab 执行续期命令的话,必定要注意环境变量的问题,所以你执行的最好将命令写入到一个脚本中,在执行这个脚本时将脚本的输出全重定向到一个文件,这样你就能知道这个脚本的执行状况了。

就像这样:

1 1 * * * sh -x /PATH/TO/certbot-renew.sh &>/tmp/certbot-auto.log
复制代码

renew 这个子命令提供了钩子,能够在执行这个命令以前和以后执行某些命令。好比对于 Standalone 模式续期以前须要启动 web 服务,在验证以后又想关掉时就能够这么用。

./certbot-auto renew --pre-hook "service nginx stop" --post-hook "service nginx start"
复制代码

可是这种证书续期方式只适用于普通的域名,对于泛域名证书,因为须要修改 TXT 记录,这个涉及到域名提供商,Let's Encrypt 就无能为力了,好比下面的错误提示。

Processing /etc/letsencrypt/renewal/ntpstat.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Could not choose appropriate plugin: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.',)
Attempting to renew cert (ntpstat.com) from /etc/letsencrypt/renewal/ntpstat.com.conf produced an unexpected error: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.',). Skipping.
复制代码

固然,如今的域名运营商基本都提供了 api 供你调用来完成自动化操做,就有人提供了对热门域名运营商的 Let's Encrypt 续期操做,Github 地址在此,按照上面的说明进行操做就行。

有个问题就是,证书续期以后证书文件的内容确定会发生改变(还将来得及测试,但应该如此),若是这个证书文件在其余服务器上使用的话,不知道在续期后要不要进行替换。

证书撤销

若是你私钥被盗用,或者你就是想撤销证书的话可使用 revoke 子命令。

certbot revoke --cert-path /etc/letsencrypt/live/CERTNAME/cert.pem
复制代码

注意它要指定的是证书的路径,而非域名。

好了,certbot 要讲到的内容就这么多了,关于 certbot 命令的使用还有不少内容,有兴趣的话能够自行查看官方文档。

acme.sh

acme.sh GitHub star 已经 10k 多了,仍是很是值得一用的。有须要的话,能够自行查看使用,反正有中文文档,看起来一点不费事。这里就很少提了,想必有了前面的基础,使用它应该没有任何问题。这里只是告诉你们有这么个东西,其实我也没有用过 :)

监控证书的有效期

虽然咱们可使用定时任务去自动对证书进行续期,而且快到期时 Let's Encrypt 也会发送邮件,可是仍是有些被动,咱们最好可以主动监控它,当证书有效期不足好比 7 天时,发送告警。

d=`openssl x509 -in /etc/letsencrypt/live/ntpstat.com/fullchain.pem -noout -enddate | awk -F= '{print $2}'`
end_timestamp=`date -d "$d" +"%s"`
current_timestamp=`date +"%s"`
echo $((end_timestamp-current_timestamp))
复制代码

输出的时间戳表示证书过时时间距离当前时间相差多少秒,你就能够经过这个时间戳来制定触发器了,好比 7 天总共 604800 秒,若是上面的时间小于这个值那就表示 7 天后过时,要报警了。

godaddy 续期操做

因为国内的域名须要备案,所以使用 godaddy 购买域名是个很不错的选择。

本人买的就是 godaddy 的域名,且以前申请的 https 泛域名证书快到期了,续期的时候才发现还须要修改 TXT 记录。虽然有人提供了自动化操做的脚本,可是本身想研究研究。

稍稍看了下 godaddy 的开发者文档,发现原来修改 TXT 记录只须要一条 curl 命令就行,简单的一逼。下面我就将如何使用 curl 命令,以及脚本如何调用的方式写出来,供你们参考。

首先使用火狐浏览器访问 developer.godaddy.com,注意必定要使用火狐浏览器,我以前用谷歌、edge、ie 访问都不行,这里不得不吐槽 godaddy,作的真是垃圾。

打开网页以后,点击 api keys,登陆以后就能够生成 key 和 secret 了,环境我选择的是 production。它们之间的区别不是很了解,或许生产环境才能直接生效?不一样的环境调用的域名不一样,其余参数都同样。

#!/bin/bash 
api_key=""
api_secret=""
record="_acme-challenge" # 固定值
record_ttl="1500" # 单位是秒
record_type="TXT"
record_value=""
domain=""
api_base_url="https://api.godaddy.com/"
api_url=`echo -n "${api_base_url}v1/domains/${domain}/records/${record_type}/${record}"`

curl -X PUT "${api_url}" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: sso-key ${api_key}:${api_secret}" -d "[{\"data\": \"${record_value}\", \"ttl\": ${record_ttl}}]"
复制代码

一条 curl 命令就 ok 了,很是简单,你如今就能够填入对应值测试一把。

接下来就须要使用 certbot-auto 命令进行调用了,它会自动传递域名和 TXT 记录的值到脚本中,因此咱们须要修改下脚本。

#!/bin/bash 
api_key=""
api_secret=""
record="_acme-challenge"
record_ttl="600"
record_type="TXT"
record_value="${CERTBOT_VALIDATION}"
domain="${CERTBOT_DOMAIN}"
api_base_url="https://api.godaddy.com/"
api_url=`echo -n "${api_base_url}v1/domains/${domain}/records/${record_type}/${record}"`

curl -sX PUT "${api_url}" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: sso-key ${api_key}:${api_secret}" -d "[{\"data\": \"${record_value}\", \"ttl\": ${record_ttl}}]"
复制代码

它不会将值做为参数进行传递,而是直接传递环境变量,上面两个环境变量就是它传递的。而后咱们将上面的内容贴入到一个文件中,我这里使用的是 /tmp/test.sh,接着赋予该脚本执行权限。

最后续期:

./certbot-auto renew --preferred-challenges=dns --manual-auth-hook /tmp/test.sh
复制代码

--preferred-challenges 能够指定 http 和 dns,咱们是 dns,因此使用 dns。

--manual-auth-hook 会自动传递三个环境变量,其中 dns 只有上面咱们用到的两个,而 http 则会多一个 CERTBOT_TOKEN

你还可使用 --manual-cleanup-hook 指定验证完成后的操做,一样是个脚本,而且会自动传递 CERTBOT_AUTH_OUTPUT 给脚本。不过这个选项咱们用不上。

当你续期的时候,测试的时候不要太频繁。若是你刚修改 TXT 记录,而且时间还未超过 TTL 的时间时就开始续期,那么验证会失败,由于 Let’s Encrypt’s 查到的会是上次的缓存,而不是这次修改的值。