L o a d i n g . . .
SHIWIVI-文章

//sunny forever
while(life<end){
love++;
beAwesome :)}

    <
  • 主题:
  • + -
  • 清除背景
  • 禁用背景

基于certbot获取TLS证书

字数:8222 写于:2022-07-29
最新更新:2022-07-29 阅读本文预计花费您24分钟
Let’s Encrypt 官网:https://letsencrypt.org/

关于

Let’s Encrypt

Let’s Encrypt 是由 ISRG (Internet Security Research Group)推出的免费安全证书计划,我们可以利用Let’s Encrypt 提供的TLS证书,在我们的网站上部署HTTPS 服务。

ISRG

ISRG(Internet Security Research Group ,互联网安全研究小组)是一个公益性的组织,该公益组织旨在减少资金、技术、教育障碍,以保护互联网上的通信,推动全球网站的HTTPS化。ISRG成立于2013 年 5 月,起初由Mozilla、电子前沿基金会(EFF)、密歇根大学、思科和 Akamai共同建立,后来又得了谷歌、亚马逊、讯飞、IBM、RedHat、Linux基金会等大厂商的赞助,所以他们的技术和证书安全是可以保障的。

Certbot

Certbot 是由电子前沿基金会 (EFF)制作的免费开源软件工具,用于在网站上申请、管理、使用由Let’s Encrypt颁发的证书,证书每60天更新一次。

前置需要

  • 一台可用的服务器,并拥有root权限
  • 安装了Web服务,以Nginx为例
  • 一个可用的域名,并解析到了该服务器上

安装certbot

官方安装

certbot官方文档:https://certbot.eff.org/

官网文档里给出了不同系统的详细安装方法,但在安装过程中会遇到不少问题,有些甚至涉及到需要重新编译Linux内核,这在已经部署业务的服务器上往往是无法实现的。

以CentOS7为例,根据官方的文档,安装Certbot需要先添加EPEL存储库,然后从中安装snapd软件包,但安装snapd需要解决诸多依赖问题:

  • 安装snapd需要Linux内核支持 SquashFS 文件系统
  • 手动编译安装的SquashFS 文件系统,yum可能无法识别
  • 内核编译时需要启用CONFIG_DEVPTS_MULTIPLE_INSTANCES 选项,如果没有需要重新编译内核

通过pip3安装

如果无法安装snapd,那只能绕过它,通过其他包管理工具安装,以pip(python包管理工具)为例

安装python并通过pip安装certbot

yum install -y python3 && pip3 install certbot
pip3版本较低报错问题

在安装certbot时,如果yum源中的pip3版本较低,可能会导致模块导入失败,从而报错

Complete output from command python setup.py egg_info: =============================DEBUG ASSISTANCE========================== If you are seeing an error here please try the following to successfully install cryptography: Upgrade to the latest pip and try again. This will fix errors for most users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip =============================DEBUG ASSISTANCE========================== Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-build-2bwzqu_0/cryptography/setup.py", line 14, in <module> from setuptools_rust import RustExtension ModuleNotFoundError: No module named 'setuptools_rust' ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-2bwzqu_0/cryptography/

此时根据报错提示,升级pip3到最新版本即可

sudo pip3 install --upgrade pip

升级完毕再安装certbot

pip3 install certbot

申请证书

验证域名

申请证书前,Let’s Encrypt需要先验证域名,以确认用户拥有对域名的控制权。验证通过后,certbot会立马进入申请流程,因此下述命令会同时验证域名和申请证书。certbot针对不同的web服务器和需求,有多种不同的验证方式:

1. Nginx

如果部署了Nginx,可以直接执行(但要确保安装了certbot-nginx插件)

sudo certbot --nginx

certbot将自动获取域名,并在进入证书申请流程时让你确认域名,也可以在执行上述命令时通过添加-d参数来指定域名,如:

sudo certbot -‌-nginx -d shiwivi.com -d www.shiwivi.com

可以通过-‌-email来添加邮箱,Let’s Encrypt会在证书即将到期时发邮件通知你更新证书。

sudo certbot -‌-nginx --email xxxx@gmail.com -d shiwivi.com -d www.shiwivi.com

如果不添加上述参数,certbot会在验证通过后要求你添加,邮箱一般只需要在首次验证证书或者更改邮箱时添加。

2. Apache

Apache中的验证方法与上类似

sudo certbot --apache
3. Webroot模式

上述方法会重启我们的web服务以重新加载配置文件,如果我们希望在颁发证书时,不重启web服务,则可以使用webroot模式。webroot模式下,certbot 会利用已经部署的 web 服务,在其 web 服务根目录下创建名为/.well-known/acme-challenge的隐藏文件,然后由 Let’s Encrypt 服务端通过域名来访问该隐藏文件,从而完成验证。

因此,在执行命令时,我们需要添加-w参数来引导certbot找到web服务的根目录

certbot certonly --webroot -w 网站根目录 -d 域名

如:Nginx默认放置网页的根目录为:/usr/local/nginx/html,则对应命令为

certbot certonly --webroot -w /usr/local/nginx/html -d shiwivi.com
3. Standalone模式

如果服务器上没有部署任何web服务,或者不想使用已部署的服务,则可以使用Standalone模式,该模式下,certbot 会自动运行一个 web server 来进行验证。该服务需要占用80端口,如果我们自己的服务器上已经有 web server 正在运行 (如 Nginx 或 Apache )并且占用了80端口,则使用Standalone前应该关闭相关服务或修改端口。

certbot certonly --standalone -d 域名

可以在命令中指定验证的端口和协议

certbot certonly --standalone --<challenge-type>-address -d 域名
更多验证方法详见官方文档:https://eff-certbot.readthedocs.io/en/stable/

申请流程

certbot完成认证后会开始进入申请流程,初次申请证书,可能需要经历以下流程:

  • certbot会提示你阅读并同意使用者条款
  • 让你输入一个email地址以推送消息,在证书快到期时,lets encrypt会发邮件提醒你及时更新证书
  • 询问是否愿意将该邮箱提供给EFF,他们会推送有关lets encrypt 的资讯

邮箱的添加可以在一开始使用

证书申请成功后,certbot会返回如下提示信息,其中较为重要的信息包括证书与密钥路径,证书到期时间

IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/newyear1234.xyz/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/newyear1234.xyz/privkey.pem Your certificate will expire on 2022-10-27. 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

插件未安装报错问题

以nginx为例,执行sudo certbot -‌-nginx时,可能会有nginx插件未被正确安装报错

The requested nginx plugin does not appear to be installed

安装nginx插件

yum install python-certbot-nginx

问题叠问题,如果源中没有该插件包,又可能会遇到报错提示无有效的包

No package python-certbot-nginx available. Error: Nothing to do

此时需要更换一下repo 源,更换前记得先将自己的源的备份

yum remove -y epel-release  # 先移除当前的包
yum clean all -v     # 清除所有下载缓存的包,并显示过程
yum makecache        # 重新将远程服务器的包下载缓存到本地
yum install -y epel-release # 重新安装 epel 

安装完epel,再重新尝试安装插件,安装完成就可以开始申请证书了

yum install python-certbot-nginx

web服务无法找到报错问题

执行sudo certbot -‌-nginx时,还可能会遇到 certbot 无法找到 web server 或配置文件报错,这是路径问题,可以建一个软链接到 /etc/nginx 目录下

ln -s /usr/local/nginx/conf/ /etc/nginx

或者在申请证书时手动指定nginx配置文件路径

certbot --nginx-server-root /usr/local/nginx/conf

文件说明

证书申请成功后,相关文件会放置在/etc/letsencrypt目录下,一般就用得到证书和密钥文件

密钥与证书

证书和密钥位于 /etc/letsencrypt/live/域名.xx/ 路径下,该路径下有如下几个文件(配置时一般只需要fullchain.pem和privkey.pem文件):

  • fullchain.pem:完整的证书链文件,包含了cert.pem和chain.pem文件中的内容
  • privkey.pem:证书对应的私钥
  • cert.pem:证书文件,需要与chain.pem文件结合使用
  • chain.pem:链文件,包含了浏览器解析所需的其他全部证书,比如根证书和中间证书
  • README:说明文件

通用配置文件

/etc/letsencrypt路径下会生成Nginx的通用配置文件options-ssl-nginx.conf

ssl_session_cache shared:le_nginx_SSL:10m; ssl_session_timeout 1440m;

ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers off;

ssl_ciphers “FCDHE-FCLSA….”;

  • ssl_session_cache:会话缓存
  • ssl_session_timeout:用户会话缓存失效时间,如果对安全性有较高要求,可以降低此值
  • ssl_protocols:加密协议
  • ssl_prefer_server_ciphers:是否开启服务端加密算法优先
  • ssl_ciphers:加密算法列表

密钥交换

/etc/letsencrypt路径下还有一个用于密钥交换算法的Diffie-Hellman 密钥ssl-dhparams.pem

历史文件

/etc/letsencrypt/archive/etc/letsencrypt/keys包含所有以前的密钥和证书,而/etc/letsencrypt/live目录下保存了最新的证书和密钥

日志文件

默认情况下,certbot 的日志文件位于/var/log/letsencrypt 路径下。且默认情况下,一旦日志目录中有 1000 条日志,certbot 就会开始日志轮替。

Nginx配置文件

server { listen 443 ssl;

#配置SSL证书和密钥路径
ssl_certificate /etc/letsencrypt/live/域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/域名/privkey.pem;

#可以将certbot生成的通用配置文件包含进去,自由选择
include /etc/letsencrypt/options-ssl-nginx.conf;

#使用生成的Diffie-Hellman 密钥,自由选择
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
……
}

Diffie-Hellman密钥用于密钥交换

常用命令

测试自动续订

  • sudo certbot renew -‌-dry-run 测试更新证书

如果测试成功,certbot会返回成功信息

- - - - - Account registered. Simulating renewal of an existing certificate for newyear110.xyz - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all simulated renewals succeeded: /etc/letsencrypt/live/newyear110.xyz/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
插件未安装报错

在测试续订时,如果遇到插件未安装的报错,则需要安装nginx插件

Failed to renew certificate newyear110.xyz with error: The requested nginx plugin does not appear to be installed

安装letsencrypt-nginx插件

pip3 install -U letsencrypt-nginx
更多关于插件未安装的报错,可以查看github上的Issues,上述解决方法也来源于该贴:https://github.com/certbot/certbot/issues/1736

续订证书

  • certbot renew 自动更新30天内到期的证书(推荐)
  • certbot renew -‌-force-renewal 强制更新未到期的证书

在初次申请证书时,certbot会记住所使用的配置和插件,并在续订时使用相同的配置和插件。如果需要更改配置(如:修改文件路径),则需要以下步骤

  1. 在命令行中使用修改以后的配置进行一次测试自动续订
  2. 测试成功,说明新的配置是可以运行的,再执行一次证书更新(时间未到就使用强制更新),这将保存新的配置
CA机构会限制证书颁发速度并阻止用户在短时间内多次续订同一个域名的证书,所以不要在短时间内多次执行强制更新证书操作

查看证书列表

  • certbot certificates 查看证书

会返回当前所有域名的证书情况,包括证书名、 证书序列号、密钥类型、到期时间、证书路径、私钥路径信息

Found the following certs: Certificate Name: newyear110.xyz Serial Number: 34829c6e3465f2b1b90db11c36111480280 Key Type: RSA Domains: newyear110.xyz Expiry Date: 2022-10-27 11:47:25+00:00 (VALID: 87 days) Certificate Path: /etc/letsencrypt/live/newyear110.xyz/fullchain.pem Private Key Path: /etc/letsencrypt/live/newyear110.xyz/privkey.pem

吊销与删除证书

删除与吊销证书前,应当从web服务器软件的配置文件中删除对对应证书的引用

  • certbot revoke -‌-cert-name 域名.xxx 吊销证书(根据域名)
  • certbot revoke -‌-cert-path /etc/letsencrypt/live/域名.xxx/cert.pem 吊销证书(根据路径)
  • certbot delete -‌-cert-name 域名.xxx 删除指定证书
  • certbot delete 从列表中选择删除证书
上一篇:Web的发展旅程
下一篇:路由与路由守卫
z z z z z