Let's Encrypt免费证书自动续期教程


Let's Encrypt是免费的SSL证书颁发机构,提供域名验证型(DV)证书。证书有效期为90天,需要定期续期。本文介绍在搬瓦工VPS上使用certbot申请Let's Encrypt证书并配置自动续期的完整流程。

安装Certbot

Debian/Ubuntu 系统:

sudo apt update
sudo apt install certbot -y

# 如果使用Nginx,安装Nginx插件
sudo apt install python3-certbot-nginx -y

# 如果使用Apache,安装Apache插件
sudo apt install python3-certbot-apache -y

CentOS/AlmaLinux 系统:

sudo yum install epel-release -y
sudo yum install certbot -y
sudo yum install python3-certbot-nginx -y

使用Snap安装(推荐,版本更新):

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

申请证书

Certbot支持多种验证方式,以下介绍最常用的两种。

方式一:Nginx插件模式(推荐)

# 自动配置Nginx
sudo certbot --nginx -d example.com -d www.example.com

# 交互过程中需要:
# 1. 输入邮箱(用于到期提醒)
# 2. 同意服务条款
# 3. 选择是否跳转HTTPS

方式二:Webroot模式

适用于不想让certbot修改Web服务器配置的场景:

# 先在Nginx配置中添加验证路径
# location /.well-known/acme-challenge/ {
#     root /var/www/certbot;
# }

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

方式三:Standalone模式

certbot临时启动内置Web服务器进行验证,需要先停止Nginx:

sudo systemctl stop nginx
sudo certbot certonly --standalone -d example.com -d www.example.com
sudo systemctl start nginx

证书文件说明

证书申请成功后,文件存放在 /etc/letsencrypt/live/example.com/ 目录下:

配置自动续期

Let's Encrypt证书有效期为90天,建议在到期前30天自动续期。certbot安装后通常会自动创建定时任务,可以检查是否已存在:

# 检查systemd timer(Snap/新版certbot)
sudo systemctl list-timers | grep certbot

# 检查crontab
sudo crontab -l | grep certbot

如果没有自动配置定时任务,手动添加cron任务:

# 编辑root用户的crontab
sudo crontab -e

# 添加以下行(每天凌晨3点检查并续期)
0 3 * * * certbot renew --quiet --deploy-hook "systemctl reload nginx"

参数说明:

使用Systemd Timer

也可以使用systemd timer替代cron,管理更方便:

# 创建service文件
sudo nano /etc/systemd/system/certbot-renew.service
[Unit]
Description=Certbot Renewal

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx"
# 创建timer文件
sudo nano /etc/systemd/system/certbot-renew.timer
[Unit]
Description=Run certbot renewal twice daily

[Timer]
OnCalendar=*-*-* 03,15:00:00
RandomizedDelaySec=3600
Persistent=true

[Install]
WantedBy=timers.target
# 启用并启动timer
sudo systemctl daemon-reload
sudo systemctl enable certbot-renew.timer
sudo systemctl start certbot-renew.timer

# 查看timer状态
sudo systemctl status certbot-renew.timer

测试续期

使用 --dry-run 参数模拟续期过程,不会真正更换证书:

sudo certbot renew --dry-run

输出中显示 "Congratulations, all simulated renewals succeeded" 表示续期流程正常。

查看和管理证书

# 列出所有已申请的证书
sudo certbot certificates

# 输出示例:
# Certificate Name: example.com
# Domains: example.com www.example.com
# Expiry Date: 2026-06-25
# Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
# Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem

# 删除不再需要的证书
sudo certbot delete --cert-name example.com

# 扩展证书域名(添加新子域名)
sudo certbot --nginx -d example.com -d www.example.com -d blog.example.com --expand

通配符证书

Let's Encrypt支持通配符证书(*.example.com),需要通过DNS验证:

sudo certbot certonly --manual --preferred-challenges dns \
  -d "*.example.com" -d example.com

certbot会要求你在域名DNS中添加一条TXT记录进行验证。通配符证书的自动续期需要配合DNS API插件,常用的DNS提供商插件:

# Cloudflare
sudo apt install python3-certbot-dns-cloudflare -y
sudo certbot certonly --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  -d "*.example.com" -d example.com

常见问题

Tip: SSL证书配置请参考 Nginx配置SSL教程。更多教程请查看新手教程
关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工 BandwagonHost 的优惠信息、使用教程和方案对比,方便中文用户选购和使用。

新手必读
搬瓦工优惠码

当前最大折扣优惠码:

NODESEEK2026(优惠 6.77%)

在购买方案时填入优惠码即可自动抵扣。详见 优惠码使用教程