在CentOS7上安装Certbot有三种方式:
- 使用Certbot官方提供的对应平台的RPM包安装
- 使用Certbot官方的提供的certbot-auto安装
- 使用pip安装Certbot,因为Certbot是Python程序
本文使用虚拟环境,使用pip安装Certbot
- 创建虚拟环境:可参考https://meaninglive.com/2020/06/15/%E5%88%9B%E5%BB%BA%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83-ansible-python3-and-virtualenvs-on-centos-and-rhel/
- 创建所需环境:
1 2 3 4 |
mkvirtualenv certbot workon certbot pip install -U setuptools pip pip install certbot-dns-route53 参考:https://wsgzao.github.io/post/certbot/ |
用如下命令申请证书
注意替换成自己的域名;执行该命令时不依赖nginx:
1 2 3 4 5 |
certbot -d meaninglive.com \ -d *.meaninglive.com \ --manual --preferred-challenges dns-01 \ --server https://acme-v02.api.letsencrypt.org/directory \ certonly --agree-tos |
介绍下相关参数:
- certonly,表示安装模式,Certbot 有安装模式和验证模式两种类型的插件。
- –manual 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择
- -d 为那些主机申请证书,如果是通配符,输入 *.devapi.haoshiqi.net(可以替换为你自己的域名)
- –preferred-challenges dns,使用 DNS 方式校验域名所有权
- –server,Let’s Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。
输入应急邮箱,证书到期前会有邮件提示
如果想跳过输入邮箱的步骤,可在申请命令后面加上:
1 |
--register-unsafely-without-email |
之后出现如下提示:要公开记录申请该证书的IP地址,是否同意?不同意就无法继续。
同意之后,出现如下提示,第一个“Press Enter to Continue”处直接回车,第二个“Press Enter to Continue”不要按回车:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
------------------------------------------------------------------------------- Please deploy a DNS TXT record under the name _acme-challenge.co1dawn.com <span class="hljs-keyword">with</span> the following value: iLS0NjcdP3RR1KphB6xbbVnKS_NS2uMW-xdDRzz85OM Before continuing, verify the record is deployed. ------------------------------------------------------------------------------- Press Enter to Continue #此处直接回车 ------------------------------------------------------------------------------- Please deploy a DNS TXT record under the name _acme-challenge.co1dawn.com <span class="hljs-keyword">with</span> the following value: f3V7aw5GPm5yzNsJFanQQaUFMyVQcqriUe3UjIDUHn0 Before continuing, verify the record is deployed. ------------------------------------------------------------------------------- Press Enter to Continue #此处不要按回车 |
为DNS解析增加TXT记录
进入自己域名的DNS记录管理页面,增加两条TXT记录,多数情况下,仅需在域名(Name)处填入_acme-challenge,在内容(Target)处填入上一步Certbot生成的内容即可(记得填写两个,多个文本记录之间以换行符(Enter键)分隔),不同DNS提供商处可能会略有不同,根据实际情况修改, 如果是www.name.com的域名的话, 就分别写两条相同主机名的记录, 填写不同的记录
稍等片刻,等TXT记录解析生效。查看是否生效的命令和生效后的查询结果如下:
host -t txt _acme-challenge.meaninglive.com
或dig -t txt _acme-challenge.meaninglive.com @114.114.114.114
查询结果如下:(必须是返回两行结果)
_acme-challenge.meaninglive.com. 289 IN TXT “e5-x4SFO7hVN-LKqDnOCaH7foFGANTgu_3tqrXMUoQ”
_acme-challenge.meaninglive.com. 289 IN TXT “L7DxvynorxxqOugy1_4Q9mZ1mnARSISAX0okyn_RLDc”
可以看到已经生效了.此时再回到刚才的界面,按回车键. 即可申请成功
可用以下命令查看证书具体信息:
1 |
certbot certificates |
certbot 自动更新证书
Let’s Encrypt颁发证书的时候,需要校验域名的所有权,证明操作者有权利为该域名申请证书,目前支持三种验证方式:
dns-01:给域名添加一个 DNS TXT 记录。
http-01:在域名对应的 Web 服务器下放置一个 HTTP well-known URL 资源文件。
tls-sni-01:在域名对应的 Web 服务器下放置一个 HTTPS well-known URL 资源文件。
而申请通配符证书,只能使用 dns-01 的方式
letsencrypt的通配符证书的有效期同样为90天.但是通配符的自动续约比较复杂.因为需要人工配置DNS记录来验证域名控制权,所以不能像单域名证书一样使用certbot工具来自动续约了.
如果用的是www.name.com的DNS, 可参考:https://github.com/laonan/certbot-dns-name-com
可只测试一下,而不申请(因为letsencrypt有申请次数限制):
certbot renew –cert-name meaninglive.com –manual-auth-hook “/opt/scripts/certbot-dns-name-com/certbot_dns_auth.sh” –dry-run
当成功无报错,可直接运行
certbot renew –cert-name meaninglive.com –deploy-hook “lnmp nginx restart” –manual-auth-hook “/opt/scripts/certbot-dns-name-com/certbot_dns_auth.sh” –manual-cleanup-hook “/opt/scripts/certbot-dns-name-com/certbot_dns_auth.sh clean”
解释一下:
- –deploy-hook:申请成功后的后续操作
- –manual-auth-hook 支持脚本hook
- –manual-cleanup-hook 由于续期时会再往DNS里插入TXT记录,会越来越多,这个是申请成功后做一次清理
可将以下任务加入crontab:
1 |
0 23 28 * * certbot renew --cert-name meaninglive.com --deploy-hook "lnmp nginx restart" --manual-auth-hook "/opt/scripts/certbot-dns-name-com/certbot_dns_auth.sh" --manual-cleanup-hook "/opt/scripts/certbot-dns-name-com/certbot_dns_auth.sh clean" |
certbot 证书默认存放路径结构: /etc/letsencrypt/
- live 目录下存放的将会链接到最新的证书和私钥
- csr keys 用来存放当前代理的授权密钥对
- account 用来存放证书的管理信息, 这里涉及 ACME
- renewal 存放当前代理所管理的域的信息
参考:
https://yq.aliyun.com/articles/713724
https://github.com/laonan/certbot-dns-name-com
https://wsgzao.github.io/post/certbot/
0 Comments