登录后台

页面导航

本文编写于 2338 天前,最后修改于 1258 天前,其中某些信息可能已经过时。

Let's Encrypt 介绍

Let's Encrypt 是一个免费、开放,自动化的证书颁发机构,由 ISRG(Internet Security Research Group)运作。

ISRG 是一个关注网络安全的公益组织,其赞助商从非商业组织到财富100强公司都有,包括 Mozilla、Akamai、Cisco、Facebook,密歇根大学等等。ISRG 以消除资金,技术领域的障碍,全面推进加密连接成为互联网标配为自己的使命。

Let's Encrypt 项目于2012年由 Mozilla 的两个员工发起,2014年11年对外宣布公开,2015年12月3日开启公测。

CertBot 介绍

ISRG 的发起者 EFF (电子前哨基金会)为 Let's Encrypt 项目发布了一个官方的客户端 Certbot ,利用它可以完全自动化的获取、部署和更新安全证书。

CertBot 的官方网站 https://certbot.eff.org/,在官网选择好Web Server服务器类型之后就可以按照官网给出的教程进行安装操作。

CertBot 安装

CentOS默认的安装源有时更新滞后,部分内容无法无法通过 yum 来进行安装,所以需要安装 EPEL

EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS和Scientific Linux。

yum install -y epel-release

然后安装 CertBot

yum install -y certbot

理论上 CertBot 此时已经安装完成,但是可能在安装过程中发生部分软件没有安装成功或者在执行 CertBot 命令的时候会发生一些错误。

python-urllib3 安装失败

CertBot 安装过程中,依赖软件包 python-urllib3 安装失败。

  • 安装失败的原因:

可能是系统中已经安装了高于CertBot安装的版本
使用 pip 命令查看当前安装的 urllib3 版本

pip show urllib3
  • 重新安装 urllib3

卸载 urllib3

pip uninstall urllib3

安装 urllib3

yum install -y python-urllib3.nonarch 0:1.10.2-2.el7_1
  • 如果在运行 CertBot 中提示 urllib3 版本太低

使用 pip 命令来更新 urllib3

pip install --upgrade urllib3

pyOpenSSL 提示版本太低

运行CertBot命令时,提示如下错误:

ImportError: 'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.

使用 pip 命令查看当前 pyOpenSSL 版本

pip show pyOpenSSL

如果 pyOpenSSL 版本低于 0.14 可以尝试升级 pyOpenSSL

pip install --upgrade pyOpenSSL

如果仍然报错,且查询 pyOpenSSL 版本为17.X 或者更高,需要先卸载已经安装好的 CertBotpyOpenSSL

yum remove -y certbot
pip uninstall -y pyOpenSSL

重新安装依赖软件包

yum install -y python-devel
yum install -y openssl-devel

然后再次安装 CertBotpyOpenSSL

yum install -y certbot
pip install -y pyOpenSSL

CertBot 验证及证书发放

CertBot 默认的 standalone 模式会自动配置好 Web Server ,但是在重新生成证书的时候,Web Server会中断重启。所以本文采用的是 Webroot 模式来验证服务器。

CertBot 在验证域名的时候,会在服务器根目录下创建 /.well-known/acme-challenge/ 目录,然后在该目录生成随机文件,最后CertBot会通过HTTP来获取这个文件,如果获取成功,则验证通过。

修改 Nginx 配置文件,在 server 标签段添加如下代码:

location ^~ /.well-known/acme-challenge/ {
   default_type "text/plain";
   root     /usr/share/nginx/html;
}

接着重启 Nginx

systemctl restart nginx

运行 CertBot 命令来验证域名生成证书

certbot certonly --webroot -w /usr/share/nginx/html/ -d a.domain.com

多个域名验证

certbot certonly --webroot -w /usr/share/nginx/html/ -d a.domain.com -d b.domain.com -d c.domain.com

如果多个域名使用多个本地目录验证

certbot certonly --webroot -w /usr/share/nginx/html/a/ -d a.domain.com -w /usr/share/nginx/html/b/ -d b.domain.com -w /usr/share/nginx/html/c/ -d c.domain.com

正常情况下会提示以下内容:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/your.domain.com/fullchain.pem. Your cert
   will expire on 20XX-09-23. 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

如果提示有错误,是 CertBot 服务器不能通过域名访问到生成的随机文件,请检查 Nginx 配置文件,或者域名的解析还没有生效。

更新证书

先使用 CertBot 命令模拟更新,该操作不会覆盖当前已经生成的证书,只是检验 CertBot 是否能够更新。

certbot renew --dry-run

模拟更新成功之后会显示如下内容:

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/your.domain.com.conf
-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/your.domain.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)

模拟成功之后,去掉 --dry-run 参数就会正式更新

certbot renew

使用 crontab 自动更新证书

使用 crontab 命令来自动执行更新操作,具体用户可搜索 crontab 命令

crontab -e

添加以下内容:

0 0 1 * * /usr/bin/certbot renew  >> /var/log/certbot-renew.log

执行该任务后,会在每月1号自动更新证书,会将日志记录到/var/log/certbot-renew.log,可以在该文件中查看更新结果及记录