免费https证书

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

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

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

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

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

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

如何做

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

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

大家应该都清楚,你申请证书的时候是要提供主机名的,比如 www.ntpstat.com,因为主机名是需要写进证书中的。还有一个种是只提供通配主机名而非具体主机名的,比如 *.ntpstat.com,这种叫泛域名证书或者通配符证书,也就是只要是这个域下的所有二级域名都可以使用这个证书。

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

具体怎么做呢?先拿具体的主机名证书申请为例,我们先将证书签署请求发送到 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 查到的会是上次的缓存,而不是此次修改的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值