前言

家喻户晓 HTTPS 是保障 HTTP 通信平安的协定,网站启用 HTTPS 能够防止很多安全性的问题, 而且 Chrome 浏览器 从 68 版本开始间接将 HTTP 网站标记为不平安了。

所以把网站升级成 HTTPS 天然是大势所趋,不过启用 HTTPS 有个最重要的问题是 HTTPS 证书要花钱!如果每年额定花钱去购买 HTTPS 证书,那也是一笔很大的开销。那么有没有收费的HTTPS证书能够用呢,查了下材料有个叫Let’s Encrypt的我的项目就提供了收费签发 HTTPS 证书的服务,这里记录下如何应用Let’s Encrypt来签发证书。

certbot 介绍

certbot是用于从 Let's Encrypt 获取证书的命令行工具,代码开源在github上。

应用certbot命令行工具能够轻松的实现HTTPS证书签发,在签发证书之前,须要证实签发的域名是属于你管制的,目前certbot有两种验证形式:

  1. HTTP
    HTTP 形式就是certbot会生成一个特定的文件名和文件内容,要求放在你对应域名下对应门路(/.well-known/acme-challenge/)下,而后certbot再通过 HTTP 申请拜访到此文件,并且文件内容与生成时候的统一。

    例如:certbot生成文件名check和内容!@#$%^,你须要申请的域名为baidu.com,则certbot拜访http://baidu.com/.well-known/acme-challenge/check来校验是否与生成的内容统一。

  2. DNS
    DNS 则是certbot生成一段特定的文本,要求在你对应域名中配置一条对应子域名(_acme-challenge)的TXT类型解析记录。

    例如:certbot生成内容!@#$%^,你须要申请的域名为baidu.com,则须要增加一条_acme-challenge.baidu.comTXT类型解析记录,值为之前生成的内容。

在域名验证通过之后,certbot就能够签发HTTPS证书了,留神在此验证步骤根底上,certbot提供了很多开箱即用的主动验证计划,然而都不合乎我的需要,起因是我须要反对通配符域名的证书,然而这种证书只反对DNS验证形式,而官网提供的DNS插件中并没有反对我用的阿里云DNS,所以只能本人去实现 阿里云的 DNS 主动校验。

应用 certbot 签发 HTTPS 证书

通过官网教程能够抉择对应操作系统,并获取装置步骤:

这里我抉择的Debian 9,依据官网的提醒进行装置:

sudo apt-get install certbot -t stretch-backports

注:如果install失败能够先执行下 apt-get update

开始签发证书

certbot certonly --cert-name pdown.org -d *.pdown.org,*.proxyee-down.com --manual --register-unsafely-without-email  --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

这里签发了一个反对*.pdown.org*.proxyee-down.com通配符域名的证书,留神如果是通配符域名证书须要指定--server https://acme-v02.api.letsencrypt.org/directory

示例:

Saving debug log to /var/log/letsencrypt/letsencrypt.logPlugins selected: Authenticator manual, Installer NoneRegistering without email!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Please read the Terms of Service athttps://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You mustagree in order to register with the ACME server athttps://acme-v02.api.letsencrypt.org/directory- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(A)gree/(C)ancel: AObtaining a new certificatePerforming the following challenges:dns-01 challenge for pdown.orgdns-01 challenge for proxyee-down.com- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -NOTE: The IP of this machine will be publicly logged as having requested thiscertificate. If you're running certbot in manual mode on a machine that is notyour server, please ensure you're okay with that.Are you OK with your IP being logged?- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(Y)es/(N)o: Y- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Please deploy a DNS TXT record under the name_acme-challenge.pdown.org with the following value:Axdqtserd184wvJc86Dxen386UXqbK2wrgb-*******Before continuing, verify the record is deployed.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Press Enter to Continue

这里会生成一串随机字符并阻塞住,须要去设置一条对应的 TXT 类型的 DNS 解析记录再持续,在设置好之后能够用nslookup进行本地验证:

nslookup -type=txt _acme-challenge.pdown.org服务器:  UnKnownAddress:  192.168.200.200非权威应答:_acme-challenge.pdown.org       text =        "Tit0SAHaO3MVZ4S-d6CjKLv6Z-********"

本地验证通过之后按回车键持续,接着 Let's Encrypt 就会校验这个 DNS 解析记录是否正确,校验通过后就会进行下一个域名的验证直到全副验证通过。

Waiting for verification...Cleaning up challengesIMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at:   /etc/letsencrypt/live/pdown.org/fullchain.pem   Your key file has been saved at:   /etc/letsencrypt/live/pdown.org/privkey.pem   Your cert will expire on 2019-12-02. To obtain a new or tweaked   version of this certificate in the future, simply run certbot   again. To non-interactively renew *all* of your certificates, run   "certbot 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

当验证通过的时候会输入证书生成的目录,外面会蕴含证书和对应的私钥,这里目录是/etc/letsencrypt/live/pdown.org/

证书截图:

这样证书就生成好了,之后只须要把证书和私钥配置到nginx中就能够用https拜访了。

应用 certbot hook 主动续签

下面证书尽管是生成好了,然而证书的有效期只有三个月,意味着每过三个月就得从新签发一个新的证书,一不注意证书就过期了,而且每次手动签发都十分的繁琐须要去手动设置 DNS 解析,所以certbot提供了一种主动续签的计划:hook

在创立证书的时候certbot提供了两个hook参数:

  • manual-auth-hook
    指定用于验证域名的脚本文件
  • manual-cleanup-hook
    指定用于清理的脚本文件,即验证实现之后

通过自定义这两个脚本就能够做到主动续签了,文档参考pre-and-post-validation-hooks。

在此基础上,官网曾经提供了很多云厂商的主动续签计划,然而我用的阿里云官网并没有提供,于是参照官网文档,写了一个基于阿里云的主动续签脚本,在验证域名的脚本中通过阿里提供的 DNS API 增加一条域名解析记录,在验证实现之后再把刚刚那条域名解析记录删除,命令行调用如下:

certbot certonly --cert-name pdown.org -d *.pdown.org,*.proxyee-down.com --manual --register-unsafely-without-email --manual-auth-hook /path/to/dns/authenticator.sh --manual-cleanup-hook /path/to/dns/cleanup.sh --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

为了方便使用,提供了一个docker镜像,通过环境变量将阿里云 API 调用的 AK 传递就能够生成和续签证书了。

  • 启动容器
docker run \--name cert \-itd \-v /etc/letsencrypt:/etc/letsencrypt \-e ACCESS_KEY_ID=XXX \-e ACCESS_KEY_SECRET=XXX \liwei2633/certbot-aliyun
  • 首次创立证书
docker exec -it cert ./create.sh *.pdown.org

创立过程中会期待一段时间,来确保 dns 记录失效,实现之后在/etc/letsencrypt/live目录下能够找到对应的证书文件

  • 续签证书
docker exec cert ./renew.sh

代码开源在github,欢送 start。

我是MonkeyWie,欢送扫码????????关注!不定期在公众号中分享JAVAGolang前端dockerk8s等干货常识。