您的位置:

Certbot 全面解析

Certbot 能够通过自动化的方式,帮助用户申请和管理 SSL/TLS 证书,以此保证 Web 服务器上的数据传输安全。本文将从安装、申请泛域名证书、自动续期等多个方面进行详细的阐述。

一、Certbot 安装

Certbot 能够在多个操作系统中运行,如:Linux、macOS、FreeBSD 等。在安装 Certbot 之前,需要确保系统中已经安装了 Python 2.7 或 3.4 及其以上版本。

以下是针对 CentOS 7 操作系统的 Certbot 安装过程:

yum install epel-release
yum install certbot python2-certbot-apache

以上命令将同时安装 certbot 和 certbot 的 Apache 插件,使得 certbot 能够自动配置 Apache Web 服务器。如果使用的是其他操作系统,可参考 Certbot 官方文档进行安装。

二、Certbot 申请泛域名证书

在向 Certbot 申请证书时,当域名存在子域名时,需要使用泛域名证书。一般来说,泛域名证书的申请比单域名证书的申请更加困难。然而,Certbot 可以通过自动化的方式,为用户申请泛域名证书并自动配置 Web 服务器。

以下是一个针对 let's encrypt 泛域名证书的申请示例:

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

执行以上命令后,Certbot 将提示用户添加一条 DNS TXT 记录用于验证域名所有权。添加完毕后,Certbot 会自动为用户申请证书,并存储于本地。

三、Certbot-auto

Certbot-auto 实际上就是 Certbot 的一种包装形式,使用 Certbot-auto 安装 Certbot 可自动下载对应的版本,并在其上进行操作。使用 Certbot-auto 的主要原因在于其能够保持在最新版本。

使用以下命令,即可自动下载并安装 Certbot-auto:

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

注意:certbot-auto 命令需要使用 sudo 身份进行执行。

四、Certbot Renew

由于 Let's Encrypt 政策,SSL/TLS 证书的有效期只有三个月。因此,为了保证证书的连续性和更新性,Certbot 提供了 renew 命令用于更新证书。

Certbot 支持两种方式进行证书的更新,分别是 non-interactive 和 interactive 两种。其中,non-interactive 方式需要生成一个 cron 定时任务,用于在每个月的指定时间自动执行证书的更新。以下是一个 renew 非交互式模式的示例:

certbot renew --dry-run

执行 renew 命令后,Certbot 会检查证书是否过期,如果过期证书将会自动进行更新。

五、Certbot 申请证书

申请 SSL/TLS 证书的过程也是 Certbot 最核心的功能之一。以下是一个典型的申请过程:

certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com

该命令使用了 Certbot 的 Webroot 插件,用户需要将命令中的 /var/www/example 替换成服务器上的实际 Webroot 目录。

在执行完以上命令后,Certbot 会为用户的域名生成证书,并将证书安装于 Web 服务器上。

六、Certbot Docker

Certbot Docker 是将 Certbot 封装在 Docker 容器中,用于与 Web 服务器镜像一起部署。

以下是一个在 Docker 容器中使用 Certbot 的示例:

docker run -it --rm --name certbot \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
certbot/certbot certonly \
--webroot --webroot-path=/var/www/example \
-d example.com -d www.example.com

在执行以上命令后,Certbot 将在 Docker 容器中运行,并将证书存储于服务器上的 /etc/letsencrypt 目录中。

到此,Certbot 相关的内容已经全部讲解完毕。读者可以根据所需,选择合适的方式进行申请 SSL/TLS 证书。