「Certbot」- 申请 Let’s Encrypt 证书 | 免费 HTTPS 证书 | HTTPS

该笔记将记录:在 Linux 中,如何安装 Certbot 工具,并使用它申请证书,以及相关问题的处理。

认识

官网:https://certbot.eff.org/
文档:https://eff-certbot.readthedocs.io/en/stable/
仓库:https://github.com/certbot/certbot/

Certbot 是 EFF 开发的一款免费、开源的命令行工具,其与 Let’s Encrypt 交互、获取和管理免费 SSL/TLS 证书的官方推荐客户端。它的核心价值在于,将原本复杂繁琐的证书申请、验证、安装、续订流程全部自动化,让用户能够通过几条简单的命令为网站轻松启用 HTTPS 服务。简而言之,Certbot 是获取和管理 Let’s Encrypt 证书的自动化助手。

组成

基本概念 2.1 Composition and Principle

原理概述

Let’s Encrypt/How It Works | https://letsencrypt.org/how-it-works/

在使用 Certbot 申请证书时,我们会疑问…………我们可以为任意域名申请证书吗?我们应该能够想到,这是不可能的…………

该部分将简述 ACME 协议工作原理,通过理解原理来找到它的适用场景。

分为两个步骤:(1)向 CA 证明域名所有权;(2)进行证书申请、续期、撤销动作。

域名所有权证明

  • 首先,客户端会生成密钥对,公钥发送给 CA 服务器,这等同于“创建帐号”。以后申请证书都与该公钥挂钩。
  • 然后,客户端会请求 CA 服务器,应该如何证明自己拥有域名。随后 CA 则会返回用于证明身份的方法,并且还会返回随机串,而客户端必须使用该随机字符串签名,以证明它控制密钥对。
  • 之后 CA 服务器会进行检查,如果检查通过就表示该密钥可以用于特定域名管理。

证书操作过程

  • 在密钥对认证通过之后,就可以使用该密钥对进行证书管理。

证书文件

Where are my certificates? | https://eff-certbot.readthedocs.io/en/latest/using.html#where-are-my-certificates

存储路径:/etc/letsencrypt/live/$domain/
使用方式:根据官方建议,通过软链接或绝对路径来引用证书。
访问权限:历史导致 0700 权限,若不降级则 chmod 0755 /etc/letsencrypt/{live,archive} 权限,针对部分环境 chgrp and chmod 0640 私钥。

privkey.pem | Private key for the certificate.
fullchain.pem | The server certificate is the first one in this file, followed by any intermediates.

cert.pem chain.pem | cert.pem contains the server certificate by itself, and chain.pem contains the additional intermediate certificate or certificates that web browsers will need in order to validate the server certificate. ⇒ 较少使用。若使用,则需要同时使用。

性质

Why ninety-day lifetimes for certificates?

Staging Environment

频率限制 | Rate Limits

Rate Limits | https://letsencrypt.org/docs/rate-limits/

HTTP Challenge vs. DNS Challenge

Let’s Encrypt Server Certificate via DNS Challenge

构建

安装并申请 Let’s Encrypt 证书

版本选择

Releases · certbot/certbot
https://github.com/certbot/certbot/releases

部署概述

该部分内容属于简述,详细内容请参考 certbot instructions 官方页面,依据提示操作即可。

以下是操作大致流程:
1)选择站点服务器软件,以及操作系统发行版
2)查看是否满足条件
3)依据提示安装软件包
4)执行命令生成证书
5)访问站点以检查证书是否生效
6)添加定时任务以实现证书自动续期

on Debian 10 | Buster | pip | venv | python

# 环境设置
apt update
apt install python3 python3-venv libaugeas0

# 安装 certbot 命令
python3 -m venv /srv/certbot/
/srv/certbot/bin/pip install --upgrade pip
/srv/certbot/bin/pip install certbot
ln -s /srv/certbot/bin/certbot /usr/bin/certbot

# 证书申请操作
...

# 定时任务,以完成证书自动续期
echo "0 0,12 * * * root /srv/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q --post-hook 'systemctl reload nginx'" \
    | sudo tee -a /etc/crontab > /dev/null

卡在 installing python packages 步骤 | no response “Installing Python packages” #2516

问题描述:在 Debian 中,执行 /usr/local/bin/certbot-auto –nginx 命令后,它会调用 pip 命令安装相关 Python 包,这是时候会卡住。

问题原因:在执行 pip 命令时,它访问官方仓库,由于网络原因导致无法正常快速下载。

解决办法:修改 $HOME/.pip/pip.conf 文件,配置如下内容(使用阿里云镜像):

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

on Debian 8 | jessie

## 第一步、安装 Certbot 工具

apt-get remove certbot
wget https://dl.eff.org/certbot-auto
mv certbot-auto /usr/local/bin/certbot-auto
chown root /usr/local/bin/certbot-auto
chmod 0755 /usr/local/bin/certbot-auto

## 第二步、申请证书并配置

# 申请证书
/usr/local/bin/certbot-auto --nginx
# /usr/local/bin/certbot-auto certonly --nginx # 或者仅仅生成证书,不修改配置

# 配置证书自动续期
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew" \
    | tee -a /etc/crontab > /dev/null

## 第三步、验证配置正确
# 访问站点,以确认 HTTPS 是否生效

针对定时任务,其用于完成证书续期。

  • 每天 12:00 及 00:00 执行
  • 当执行时,休眠 0h-1h 时长 ⇒ 其中,函数 sleep() 以秒为单位,函数 random() 产生 0-1 之间的随机数。

on CentOS 7.4 with PIP 3.6

经验:这种东西就应该在虚拟环境里搞,但凡与应用有关且与系统管理无关的 Python 应用,都应该在虚拟环境里搞。要是在系统环境里搞,直接 yum install,pip2.7 install,等着吧,早晚有难受的时候。

我们并没有在虚拟环境里,因为系统没有使用 Python 3.6 环境,因此我们可以直接使用:

pip3.6 install certbot certbot-nginx certbot-dns-aliyun

on CentOS 7.4 with Yum

Certbot 1.0.0

# --------------------------------------------------------- # 安装 Certbot 工具

# 安装相关软件包
yum install -y epel-release
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

# 安装并获取证书
yum install certbot python2-certbot-nginx

# --------------------------------------------------------- # 申请证书并配置

# 申请 Nginx 证书,并自动修改 Nginx 配置
certbot --nginx

# 仅仅生成证书,不修改配置
# certbot certonly --nginx

# 配置证书自动续期
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" \
    | tee -a /etc/crontab > /dev/null

# --------------------------------------------------------- # 验证配置正确

# 访问站点确认 HTTPS 是否生效

通过 Snap 部署

# 2025-11-06

on CentOS 7.9

Ubuntu 22.04 | snapd
Installing snap on Ubuntu | Snapcraft documentation
Certbot Instructions | Certbot

sudo apt install snapd
sudo snap install --classic certbot

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

Q:证书自动续期?
A:其通过 systemd timer 来完成自动续期:systemctl list-units | grep certbot.renew
R:linux – How to automate certbot certificate renewal on Ubuntu 20.04 – Server Fault

自动重启服务

在自动证书续期后,可能需要重启服务以加载证书 | Let’s Encrypt: Reload Nginx after Renewing Certificates

certbot -q renew --renew-hook "/etc/init.d/nginx reload"

性能测试

http_load – multiprocessing http test client

应用

场景 | 通过 HTTP 质询 | HTTP Chanllenge

1、首先,域名需要解析到服务器,指向服务器的 IP 地址。

2、通过 certbot 命令,来申请证书;

# ----------------------------------------------------------------------------- # 申请证书,

certbot certonly --standalone -d vpn.infr.d3rm.org # 运行独立的 Web Server 来完成质询

场景 | 针对非标准路径的 Nginx 部署 /usr/local/nginx/

针对部分环境,Nginx 部署在 /usr/local/nginx/ 目录。

certbot run -a dns-dnspod                                  \
    --dns-dnspod-credentials /etc/letsencrypt/dnspod-credentials.ini \
    --nginx-server-root=/usr/local/nginx/conf/             \
    --nginx-ctl=/usr/local/nginx/sbin/nginx                \
    --no-redirect

# 通过 HTTP 质询来验证

certbot run                                                \
    --nginx-server-root=/usr/local/nginx/conf/             \
    --nginx-ctl=/usr/local/nginx/sbin/nginx                \
    --no-redirect

–no-redirect,该参数禁用 Certbot 自动配置 80 到 443 重定向。
–nginx-server-root=/usr/local/nginx/conf/,该路径需要包含 nginx.conf 文件。
–nginx-ctl=/usr/local/nginx/sbin/nginx,该参数指定 nginx 二进制程序的路径。

参考

Let’s Encrypt/Getting Started
快速签发 Let’s Encrypt 证书指南
certbot instructions