使用Certbot获取配置Let's Encrypt的ssl证书

安装 snap 

sudo apt update
sudo apt install snapd

安装 Certbot

sudo snap install --classic certbot

链接文件,方便使用命令

sudo ln -s /snap/bin/certbot /usr/bin/certbot

因为这里使用的是Cloudflare解析,这里直接安装Cloudflare插件,

sudo snap install certbot-dns-cloudflare

止目前已知支以下DNS服务的插件:

certbot-dns-cloudflare
certbot-dns-cloudxns
certbot-dns-digitalocean
certbot-dns-dnsimple
certbot-dns-dnsmadeeasy
certbot-dns-gehirn
certbot-dns-google
certbot-dns-linode
certbot-dns-luadns
certbot-dns-nsone
certbot-dns-ovh
certbot-dns-rfc2136
certbot-dns-route53
certbot-dns-sakuracloud

若出现以下错误,则根据提示执行 snap set certbot trust-plugin-with-root=ok 即可

error: cannot perform the following tasks:
- Run hook prepare-plug-plugin of snap "certbot" (run hook "prepare-plug-plugin": 
-----
Only connect this interface if you trust the plugin author to have root on the system.
Run `snap set certbot trust-plugin-with-root=ok` to acknowledge this and then run this command again to perform the connection.
If that doesn't work, you may need to remove all certbot-dns-* plugins from the system, then try installing the certbot snap again.
-----)

配置插件所需的Cloudflare帐户里的API Key,创建一个文件(假设放在/etc/cloudflare.ini),内容如下

dns_cloudflare_api_token = your api key

获取证书,可以直接支持泛域

certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials /etc/cloudflare.ini \
  -d yourdomainname.com \
  -d *.yourdomainname.com

如果没有意外,将返回获取成功的信息,自行查看证书存放的位置,在去配置Nginx即可

若支持的插件没有自己所需的DNS服务商,可通过手动设置TXT记录完成,执行以下命令:

certbot certonly  -d "*.yourdomainname.com" -d yourdomainname.com --manual --preferred-challenges dns-01  --server https://acme-v02.api.letsencrypt.org/directory

执行命令后,按提示添加一个TXT的DNS记录,确认解析生效生按回车确认下一步,可通过以下命令检验是否成功:

nslookup -q=txt _acme-challenge.yourdomainname.com

也可以根据提示直接访问googleapps的工具来检验是否生效:

https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.yourdomainname.com

如果没有意外,应该出现成功的提示。

最后可测试一下自动续定:

sudo certbot renew --dry-run

若续定失败,请确认自己Cloudflare帐户里的API Key是否正确,如正确,请考虑是否时间过短引起的,可使用以下代码,将校验时间增加到30秒(默认10秒,也可根据自己的需要调整):

sudo certbot --dns-cloudflare-propagation-seconds 30 renew --dry-run

后期可通过以下命令查看已创建的证书:

sudo certbot certificates

若需要删除证书,可使用以下命令:

sudo certbot delete --cert-name example.com

posted on 2022-04-06 19:19  深秋之韵  阅读(616)  评论(0编辑  收藏  举报

导航