---
title: Certbot HTTPS 证书配置
description: Certbot HTTPS 证书配置文档，简述在 Debian 服务器上使用 C…
url: "https://brimflow.xyz/docs/operations/debian-certbot-https/"
updated: "2025-06-02"
category: 运维
---

# Certbot HTTPS 证书配置

Certbot HTTPS 证书配置文档，简述在 Debian 服务器上使用 Certbot 工具为域名配置 HTTPS 证书的流程。

在开始之前，请务必确认您的**云端防火墙**（如果已启用）和**本地防火墙**（如果已启用）均已开放 **80 (HTTP)** 和 **443 (HTTPS)** 端口，以确保 Certbot 能够成功进行域名验证，并且用户可以通过安全的 HTTPS 连接访问您的网站。

## 一、配置域名解析

在 DNS 管理后台添加 A 记录，指向服务器 IP，以 `xsymm.com` 为例：

 INI

```
A 记录: xsymm.com      → <服务器 IP>
A 记录: www.xsymm.com  → <服务器 IP>
```

## 二、申请 Let's Encrypt 证书

### 2.1 安装 Certbot

 Bash

```
sudo apt update
sudo apt install -y certbot
```

### 2.2 获取证书 (Standalone 模式)

确保 80 端口可用，执行：

 Bash

```
sudo certbot certonly --standalone -d xsymm.com -d www.xsymm.com
```

证书保存路径：`/etc/letsencrypt/archive/xsymm.com/`

## 三、故障排查：端口占用及防火墙

Certbot 的 `standalone` 模式依赖 80 端口进行域名验证。确认您的**云服务提供商控制台**中的防火墙设置已允许入站 TCP 连接到 **80 (HTTP)** 和 **443 (HTTPS)** 端口。检查您的**本地防火墙**（如 `ufw`、`firewalld` 或 `iptables`）是否允许入站 TCP 连接到 **80** 和 **443** 端口。

**重要提示：**仔细阅读 `Certbot` 申请证书时的输出信息，通常会包含详细的错误提示，有助于定位问题。若申请失败，可按以下步骤排查：

### 3.1 **识别监听 80 端口的进程**

使用 `lsof` 或 `netstat` 命令查看占用该端口的进程及其 PID（进程 ID）。

 Bash

```
sudo lsof -i:80
# sudo netstat -tulnp | grep :80
```

### 3.2 **临时停止占用 80 端口的服务 (推荐)**

如果 80 端口被 Web 服务器（如 Nginx 或 Apache）占用，建议临时停止这些服务，以确保服务平稳停止。

 Bash

```
# Nginx
sudo systemctl stop nginx

# Apache
sudo systemctl stop apache2
```

待 Certbot 验证完成后，再重新启动服务

 Bash

```
# Nginx
sudo systemctl start nginx

# Apache
sudo systemctl start apache2
```

### 3.3 **终止占用 80 端口的进程 (备选方案)**

如果占用 80 端口的不是 Web 服务器，或者临时停止服务不可行，可以使用 `kill` 或 `fuser` 命令终止相关进程。请务必确认要终止的进程。

使用 PID 终止：

 Bash

```
sudo kill <PID>
```

使用端口终止：

 Bash

```
sudo fuser -k 80/tcp
```

## 四、证书自动续期

### 4.1 **创建续期脚本**

例如：`/home/debian/cron/certbot-renew.sh`

 Bash

```
#!/bin/bash
certbot renew --quiet
# systemctl reload nginx # 取消注释并替换为您的 Web 服务器重载命令
# systemctl reload apache2 # 取消注释并替换为您的 Web 服务器重载命令
```

赋予执行权限：

 Bash

```
sudo chmod +x /home/debian/cron/certbot-renew.sh
```

### 4.2 添加定时任务

 Bash

```
sudo crontab -e
```

添加定时任务 (每日凌晨 3 点执行)：

 INI

```
0 3 * * * /home/debian/cron/certbot-renew.sh
```

## Certbot HTTPS 证书配置完成

以上为使用 Certbot 配置 HTTPS 证书的快速查阅指南。请根据您的实际环境进行操作。

 [CERTBOT 工具](https://certbot.eff.org)
