📋 本文记录:使用 Certbot 为
blog.lazyyoun.xyz申请 Let's Encrypt 免费 HTTPS 证书的完整流程和注意事项。
| 项目 | 值 |
|---|---|
| 服务器 | xx.xx.xx.xx (阿里云) |
| 系统 | Alibaba Cloud Linux 3 |
| 域名 | blog.lazyyoun.xyz |
| Web 服务器 | Nginx 1.20.1 |
| 证书类型 | Let's Encrypt (90 天有效期) |
| 验证方式 | DNS TXT 记录验证 |
bash# 阿里云 Linux 3 / CentOS
yum install -y epel-release
yum install -y certbot python3-certbot-nginx
bashcertbot --version
# 输出:certbot 2.x.x
bashcertbot certonly -d blog.lazyyoun.xyz --manual --preferred-challenges dns --agree-tos --email admin@lazyyoun.xyz
参数说明:
certonly - 只申请证书,不自动配置-d - 指定域名--manual - 手动模式(需要手动配置 DNS)--preferred-challenges dns - 使用 DNS 验证--agree-tos - 同意服务条款--email - 接收续期通知的邮箱执行命令后会输出:
Please deploy a DNS TXT record under the name: _acme-challenge.blog.lazyyoun.xyz. with the following value: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Before continuing, verify the TXT record has been deployed.
记下这个 TXT 记录值,下一步要用。
lazyyoun.xyz 域名| 字段 | 值 |
|---|---|
| 主机记录 | _acme-challenge.blog |
| 记录类型 | TXT |
| 记录值 | (certbot 输出的值) |
| TTL | 10 分钟(默认) |
⚠️ 重要提示:
_acme-challenge.blog 记录,请先删除DNS 传播需要时间,通常 1-5 分钟。
方法 1:Google Admin Toolbox
https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.blog.lazyyoun.xyz
方法 2:命令行
bashdig +short TXT _acme-challenge.blog.lazyyoun.xyz @8.8.8.8
方法 3:nslookup
bashnslookup -qt=TXT _acme-challenge.blog.lazyyoun.xyz 8.8.8.8
看到正确的 TXT 记录值后,继续下一步。
在 certbot 终端中按回车键,继续验证流程。
成功输出:
Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/blog.lazyyoun.xyz/fullchain.pem Key is saved at: /etc/letsencrypt/live/blog.lazyyoun.xyz/privkey.pem This certificate expires on 2026-06-20.
bashcat > /etc/nginx/conf.d/blog.conf << 'NGINX_CONF'
server {
listen 443 ssl;
server_name blog.lazyyoun.xyz;
# 证书路径
ssl_certificate /etc/letsencrypt/live/blog.lazyyoun.xyz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/blog.lazyyoun.xyz/privkey.pem;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
client_max_body_size 10M;
# 网站配置
location / {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
}
}
server {
listen 80;
server_name blog.lazyyoun.xyz;
return 301 https://$host$request_uri;
}
NGINX_CONF
bash# 测试配置
nginx -t
# 重载 Nginx
nginx -s reload
bash# 测试访问
curl -I https://blog.lazyyoun.xyz
# 应该返回 HTTP/1.1 200 OK
错误信息:
Detail: Incorrect TXT record "XXX" found at _acme-challenge.blog.lazyyoun.xyz
原因:
解决方案:
_acme-challenge.blog TXT 记录原因: 每次重新启动 certbot 都会生成新的 TXT 记录值。
正确做法:
如果 certbot 已退出:
bash# 重新启动 certbot,会生成新值
certbot certonly -d blog.lazyyoun.xyz --manual --preferred-challenges dns
# 使用新的 TXT 记录值
可能原因:
解决方案:
_acme-challenge.blog)@8.8.8.8 查询,避免本地 DNS 缓存Let's Encrypt 证书有效期:90 天
bash# 续期命令(与申请相同)
certbot certonly -d blog.lazyyoun.xyz --manual --preferred-challenges dns
# 续期后重载 Nginx
nginx -s reload
建议:
查看证书到期时间:
bashopenssl x509 -in /etc/letsencrypt/live/blog.lazyyoun.xyz/fullchain.pem -noout -dates
使用阿里云 DNS API 实现自动续期:
bash# 安装插件
pip3 install certbot-dns-aliyun
# 配置 API Key
cat > /root/.aliyun-credentials.ini << EOF
[default]
access_key_id = YOUR_ACCESS_KEY_ID
access_key_secret = YOUR_ACCESS_KEY_SECRET
EOF
# 自动续期
certbot certonly -d blog.lazyyoun.xyz \
--dns-aliyun \
--dns-aliyun-credentials /root/.aliyun-credentials.ini
# 配置定时任务(每天凌晨 2 点检查)
crontab -e
# 添加:0 2 * * * certbot renew --deploy-hook "nginx -s reload"
bash# 检查证书信息
openssl x509 -in /etc/letsencrypt/live/blog.lazyyoun.xyz/fullchain.pem -noout -text
# 检查证书有效期
openssl x509 -in /etc/letsencrypt/live/blog.lazyyoun.xyz/fullchain.pem -noout -dates
# 测试 HTTPS 连接
curl -I https://blog.lazyyoun.xyz
bash# 1. 安装 certbot
yum install -y certbot python3-certbot-nginx
# 2. 申请证书
certbot certonly -d blog.lazyyoun.xyz --manual --preferred-challenges dns
# 3. 验证 DNS
dig +short TXT _acme-challenge.blog.lazyyoun.xyz @8.8.8.8
# 4. 测试 Nginx 配置
nginx -t
# 5. 重载 Nginx
nginx -s reload
# 6. 查看证书有效期
openssl x509 -in /etc/letsencrypt/live/blog.lazyyoun.xyz/fullchain.pem -noout -dates
# 7. 续期证书
certbot certonly -d blog.lazyyoun.xyz --manual --preferred-challenges dns
nginx -s reload
nginx -t)nginx -s reload)curl -I https://blog.lazyyoun.xyz)本文档基于 2026-03-23 实际申请流程整理 博客地址:https://blog.lazyyoun.xyz
本文作者:lazyyoun
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!