Let's Encrypt是免费的SSL证书颁发机构,提供域名验证型(DV)证书。证书有效期为90天,需要定期续期。本文介绍在搬瓦工VPS上使用certbot申请Let's Encrypt证书并配置自动续期的完整流程。
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/ 目录下:
fullchain.pem - 完整证书链(包含域名证书和中间证书),Nginx的 ssl_certificate 使用此文件privkey.pem - 私钥文件,Nginx的 ssl_certificate_key 使用此文件cert.pem - 域名证书(不含中间证书)chain.pem - 中间证书(用于OCSP Stapling)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"
参数说明:
--quiet:静默模式,只在出错时输出信息--deploy-hook:续期成功后执行的命令,这里重新加载Nginx使新证书生效也可以使用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
--staging 参数Tip: SSL证书配置请参考 Nginx配置SSL教程。更多教程请查看新手教程。