centos 7 使用certbot解决域名证书续签最佳实践

1、单域名Certbot内网解析的域名认证部署

问题描述:certbot的官网指引中 需要使用公网服务器并且使用HTTP站点在线,这主要是为了完成域名认证所有权,内网域名配置https方案中,无法进行在线认证 可以使用DNS质询的方式,在申请证书时,选择DNS质询,申请过程中会提示你添加域名解析。

certbot 的验证方式有两种,一种是文件验证 一种是DNS验证

如果使用的是文件验证方式的话 需要使用到80端口 因为certbot 需要指定该文件路径进行验证 DNS则不需要这个条件

1.1、安装必要的插件信息:

可选配置:启动EPEL存储库 非必要项

yum install -y epel-release
yum clean all
yum makecache
#启用可选通道  可以不配置
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

必要配置参数:安装certbot

yum -y install certbot python2-certbot-nginx

1.2、开始申请证书:

申请方式1:不推荐使用

使用该方式进行注册域名的时候 续签过程会导致无法续签因为challenge验证不通过

certbot certonly --manual --preferred-challenges dns -d example.com

执行成功之后会提示下一步操作:

为了证明域名所有权 会要求你在解析的DNS解析站点上添加认证字段

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

667drNZ.........rjF1lSaUndc

Once this is deployed,
Press ENTER to continue

此时需要添加相关的配置信息 认证网站所有权

使用该申请证书续签出现问题:

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 (harbor.example.com) from /etc/letsencrypt/renewal/harbor.example.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.
All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/harbor.example.com/fullchain.pem (failure)

分析原因如下:

当我们使用 --manual 选项,并使用 DNS 质询时,每次都要设置不同的 DNS TXT 记录。

问题就在这里,当我们 certbot renew 时,这是个自动化过程,而 certbot 无法处理这种场景。

我们可以使用插件解决这个问题,插件在本质上还是调用 DNS 服务的 API 设置 DNS TXT 记录。

在 CentOS 7.x 中:Cloudflare DNS 可以使用 python2-certbot-dns-cloudflare 插件;Google Cloud DNS 可以使用 python2-certbot-dns-google 创建;DigitalOcean DNS 可以使用 python2-certbot-dns-digitalocean 插件,参考 Welcome to certbot-dns-digitalocean’s documentation! 文档,具体细节不再展开。

推荐申请证书使用方式:standalone

certbot certonly --standalone --email mail@qq.com -d domain.com

使用该方法签证证书需要公网解析的IP与部署的服务器IP保持一致  否则会解析不通过无法生成证书;

配置完成之后再敲回车继续执行,此时认证成功之后,会在配置的服务器上生成以下文件:

注意生成的文件名与软连接信息 与自动化注册续期有关联

然后将需要的配置文件拷贝出来保存在nginx配置文件中指定的路径即可;

server {


    listen 80;
    server_name www.testdomain.com;
    return  301  https://$server_name$request_uri;
#    rewrite ^(.*)$ https://$server_name$1 permanent;


}
server {
    listen 443 ssl;
    server_name www.payxuntuofu.ga;
  #配置调用生成的证书信息  然后既可以使用  存放路径可以自定义  拷贝使用即可注意文件名信息
    ssl_certificate /etc/letsencrypt/live/www.testdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.testdomain.com/privkey.pem;
#使用自带的证书路径  可以在续签证书过程中直接调用

    location / {
      root  /data/www/fans/;
      access_log on;

    }
}

使用证书:

不同于其他方法,该方法只能申请证书,你需要手动修改Web服务器配置以使用证书。

1.3、配置自动续订功能:

客户首次申请是要手动改DNS记录,等到过期后,也不能自动续期,需要再次手动调整DNS记录。后续需要验证

证书有效期为 90 天,需要在定时任务中使用 certbot renew 命令重新续期证书,有关内容参考官方文档。但是还需要附加操作,

certbot renew --deploy-hook 'nginx -s reload'
同样可以配置自动脚本方式:

0 1 * * * certbot renew --deploy-hook 'nginx -s reload'

使用standalone 的方式才能进行上述指令的续签证书:

当证书在30天内过期时,就会自动更新,更新成功会重新加载nginx。新注册的证书同样会放置在/etc/letsencrypt/live/域名目录下,因此可以在配置虚拟主机的时候 添加ssl证书路径时调用该路径下的配置文件 避免自动注册后文件没有更新的情况;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值