用letsencrypt的certbot申请免费证书,并部署全流程


关于letsencrypt

let's encrypt

官网:https://letsencrypt.org
文档:https://letsencrypt.org/zh-cn/docs/
文档2:https://eff-certbot.readthedocs.io/en/latest/using.html#certbot-commands

安装certbot

apt-get install certbot

获取证书

命令

certbot certonly --manual --preferred-challenges=dns --server https://acme-v02.api.letsencrypt.org/directory -d rg4.net -d *.rg4.net

中间需要你在你的域名解析里加一个类型为TXT的域名,域名的名字为:_acme-challenge,txt的值为certbot提供给你的一段字串。

Please deploy a DNS TXT record under the name
_acme-challenge.rg4.net with the following value:
npcipMkfxZ20_1234567890ESNaAVZa7sFxLvtFig6M

设置成功后回车,certbot会自动校验,校验通过了会提示你,生成证书成功,并告知你证书保存的路径。

Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/rg4.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/rg4.net/privkey.pem
   Your cert will expire on 2025-01-08. 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

部署证书

如果你用了宝塔,那你可以将生成的证书内容填到宝塔的SSL配置那里。

以我申请到的证书及路径为例:

  1. 密钥(KEY): 对应/etc/letsencrypt/live/rg4.net/privkey.pem文件
  2. 证书(证书PEM格式): 对应/etc/letsencrypt/live/rg4.net/fullchain.pem文件

部署完成后,可以考虑让nginx重新加载一下

nginx -s reload

问题

  1. 运行certbot失败
    我的阿里云的 ecs 安装了CentOS 7.5,python环境用的python 3.x,而yum install certbot后发现依赖的是python2,结果自然报错。而我的ecs又不想再在上面折腾了,空间已经不够了,所以在自己的笔记本上安装了一个certbot,并在笔记本上申请standalone的证书,再把申请好的证书上传到ecs上,即可正常工作。
  2. 打开网站报错:NET::ERR_CERT_COMMON_NAME_INVALID
    遇见NET::ERR_CERT_COMMON_NAME_INVALID这个报错的原因是网站ssl证书颁发的域名与网站实际打开的域名(或者请求的URL)不匹配。
    我一开始申请证书的时候,域名只填了一个通配符 *.rg4.net,但是打开网站的时候走了 https://rg4.net ,结果报错。
    解决方案是重新来一遍,并把 rg4.net 这个域名也加进去。
certbot certonly --manual --preferred-challenges=dns --server https://acme-v02.api.letsencrypt.org/directory -d rg4.net -d *.rg4.net

Leave a comment

Your email address will not be published. Required fields are marked *