51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

cerbot 自动续期 ssl 证书nginx

cerbot 自动续期 ssl 证书nginx

使用Certbot申请免费 HTTPS 证书及自动续期 - 知乎 (zhihu.com)

安装 Certbot

安装完 后可以跳到 自动续费,最下边。

yum install certbot -y

需要依赖的包比较多

申请证书

比如我们的域名是 k8s.baimeidashu.com

主域名: baimeidashu.com

泛域名: *.baimeidashu.com

泛域名证书又叫通配符证书,一张那样的证书能够维护一个域名及其这一域名全部的二级或是三级子域名,使它们都变为 https 数据加密连接,并且不限定子域名总数。

执行以下命令:

certbot certonly -d k8s.baimeidashu.com --manual --preferred-challenges dns


输入邮箱:


然后:
你需要按照提示,在你的域名服务商处,添加对应的 DNS TXT 解析记录。

Please deploy a DNS TXT record under the name
_acme-challenge.k8s.baimeidashu.com with the following value:

CyXn4E9PasRL904bf23iwtyo0QcOV9WIjrYZlT4EgTU

Before continuing, verify the record is deployed.

配置好之后,按回车继续。

如果成功的话,它会生成两个文件:

ls /etc/letsencrypt/live/k8s.baimeidashu.com/

2个文件有用:

/etc/letsencrypt/live/test.com/fullchain.pem
/etc/letsencrypt/live/test.com/privkey.pem

这个证书的过期时间为:

 Your certificate will expire on 2024-01-23

有了证书可以配置 nginx了, 参考: nginx 配置 https ssl

Certbot 是申请的Let's Encrypt的免费证书,有效期 3 个月,到期之后我们可以再次续期,达到永久免费的效果。

{#more-14246}

有了下边的工具,上边生成证书的命令,可以不用操作, 因为下边 正式申请会给我们自动创建证书的。

自动续期:

在到期前,再手动执行生成证书的命令

certbot certonly -d k8s.baimeidashu.com --manual --preferred-challenges dns

再重复一下配置 DNS 解析的操作就 OK 啦。

那么怎么才能做到自动呢?

因为要去 服务商那边修改记录, 这个时候不用怕

这里推荐 @justjavac 大佬写的,https://github.com/justjavac/ce

防止丢失:

原理:

当我们使用 certbot 申请通配符证书时,需要手动添加 TXT 记录。每个 certbot 申请的证书有效期为 3 个月,虽然 certbot 提供了自动续期命令,但是当我们把自动续期命令配置为定时任务时,我们无法手动添加新的 TXT 记录用于 certbot 验证。

好在 certbot 提供了一个 hook,可以编写一个 Shell 脚本。在续期的时候让脚本调用 DNS 服务商的 API 接口动态添加 TXT 记录,验证完成后再删除此记录。

如何操作呢?

安装 aliyun cli 工具

wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
tar xzvf aliyun-cli-linux-latest-amd64.tgz
sudo cp aliyun /usr/local/bin
rm aliyun

安装完成后需要配置凭证信息 (验证阿里云账号):连接

我们用 AK

accesskey: 连接

aliyun configure --profile akProfile

配置交互过程示例如下:{#d42c971070un0}

Configuring profile 'akProfile' in '' authenticate mode...
Access Key Id []: AccessKey ID
Access Key Secret []: AccessKey Secret
Default Region Id []: cn-hangzhou
Default Output Format [json]: json (Only support json))
Default Language [zh|en] en:
Saving profile[akProfile] ...Done.

出现如图 说明成功

2-安装 certbot-dns-aliyun 插件

这个就是 hook了

wget https://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh
sudo cp alidns.sh /usr/local/bin
sudo chmod +x /usr/local/bin/alidns.sh
sudo ln -s /usr/local/bin/alidns.sh /usr/local/bin/alidns
rm alidns.sh

3-申请证书

测试是否能正确申请:

certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

使用该方法,你必须确保这个域名属于你,并且你的域名在当前服务器中的80或者443端口可访问性。

不然下图就会失败

正常的是这样的结果。

我们去后台看看解析有没添加 txt 记录

正式申请时去掉 --dry-run 参数:

certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"

上图我申请了一个k8s.baimeidshu.com 的证书, 会自动放到目录:

/etc/letsencrypt/live/

查看证书日期:

openssl x509 -in /etc/letsencrypt/live/k8s.baimeidash.com/fullchain.pem  -noout -dates

4-证书续期

对本机上所有的证书 都续期

certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

如果以上命令没有错误,把 --dry-run 参数去掉。

对本机上某个证书续期:

--cert-name domain
certbot renew --cert-name k8s.baimei.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

再来 查看证书日期:

没有变化, 我们明天再来做实验:

2023年10月26日

certbot renew --cert-name 后依然没变, 不知道这是什么原理,按理来说应该延迟1天才对,或者我到下午5点的时候再试试

自动续期

添加定时任务 crontab。

vim  /etc/crontab

1 1 */1 * * root certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload"

上面脚本中的 --deploy-hook "nginx -s reload" 表示在续期成功后自动重载 nginx。

疑问:

刚开始不敢给所有的 官网 域名搞,

只能自己测试 ,用k8s.baimeidashu.com ,每天看看过期时间有没有增加,这样就能验证了。

查看ssl证书过期时间

openssl x509 -in 证书名 -noout -dates

demo:

openssl x509 -in /etc/nginx/ssl_key/k8s.baimeidashu.com.pem -noout -dates

我们手动续期一下,看看 过期时间变化了吗

certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
赞(1)
未经允许不得转载:工具盒子 » cerbot 自动续期 ssl 证书nginx