
Let's Encrypt 是由互联网安全研究组(ISRG)运营的免费、自动化、开放的证书颁发机构(CA)。它免费颁发域名验证(DV)SSL证书,获得Mozilla、Google、Cisco等科技巨头的支持。
Certbot 是电子前哨基金会(EFF)开发的ACME协议客户端,能自动完成Let's Encrypt证书的申请和续期,原生支持Nginx和Apache插件。
在安装Certbot之前,请确认以下条件都已满足:
| 要求 | 详细说明 | 状态 |
|---|---|---|
| Ubuntu VPS | Ubuntu 20.04 / 22.04 / 24.04 | ✅ 必须 |
| 已安装Nginx | 建议1.18版本以上 | ✅ 必须 |
| 域名已解析到VPS | A记录 → VPS IP地址 | ✅ 必须 |
| 80和443端口已开放 | Certbot使用HTTP验证 | ✅ 必须 |
| root或sudo权限 | 安装软件包需要 | ✅ 必须 |
验证Nginx正在运行:
sudo systemctl status nginx
# 应显示:Active: active (running)
推荐通过snap安装,始终获得最新版本并自动更新:
# 更新软件包列表
sudo apt update
# 安装snapd(Ubuntu 20.04+已预装)
sudo apt install -y snapd
# 更新snap core
sudo snap install core
sudo snap refresh core
# 通过snap安装certbot
sudo snap install --classic certbot
# 创建符号链接
sudo ln -s /snap/bin/certbot /usr/bin/certbot
# 验证安装
certbot --version
sudo apt remove certbot
Certbot的Nginx插件可以自动完成证书申请和Nginx配置:
# 申请证书并自动配置Nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
# 或者只申请证书,不修改Nginx配置
sudo certbot certonly --nginx -d yourdomain.com -d www.yourdomain.com
Certbot会要求提供邮箱地址并同意服务条款,证书存储在:
/etc/letsencrypt/live/yourdomain.com/fullchain.pem # 证书文件
/etc/letsencrypt/live/yourdomain.com/privkey.pem # 私钥文件
通配符SSL覆盖所有子域名(*.yourdomain.com),需要使用DNS验证:
sudo certbot certonly --manual --preferred-challenges dns \
-d "yourdomain.com" -d "*.yourdomain.com"
Certbot会要求您在DNS中添加TXT记录,等待DNS传播后按Enter继续。
使用--nginx标志时,Certbot会自动修改配置。如需手动配置,使用以下模板:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/yourdomain.com;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
}
# 测试配置
sudo nginx -t
# 重新加载Nginx
sudo systemctl reload nginx
HSTS强制浏览器始终使用HTTPS,增加额外的安全层:
# 在443 server块内添加
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Let's Encrypt证书有效期为90天。通过snap安装时,Certbot会自动设置systemd定时器或cron任务。
# 检查续期定时器
sudo systemctl list-timers | grep certbot
# 或检查cron
sudo crontab -l | grep certbot
测试续期(不实际颁发证书):
sudo certbot renew --dry-run
# 应显示:Congratulations, all simulated renewals succeeded
安装完成后,使用以下工具验证SSL配置:
ssllabs.com/ssltest — 全面检查加密套件、协议和证书链curl -I https://yourdomain.com — 检查响应头和状态码openssl s_client -connect yourdomain.com:443 — 查看证书详情# 查看已颁发的证书
sudo certbot certificates
# 输出显示域名、到期日期和证书路径
# 在UFW中开放80和443端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
域名A记录可能未指向VPS的IP,或DNS尚未传播。更改DNS后请等待24-48小时。
# 检查DNS传播
dig yourdomain.com A +short
# 应返回您的VPS IP地址
# 查看日志
sudo journalctl -u snap.certbot.renew --since "1 day ago"
# 或查看certbot日志
sudo tail -50 /var/log/letsencrypt/letsencrypt.log
证书与私钥不匹配,强制重新申请证书:
sudo certbot renew --force-renewal -d yourdomain.com
完全免费,没有任何隐性收费。Let's Encrypt是由Mozilla、Google、Cisco和EFF支持的非营利组织,证书申请和续期永远免费。
Let's Encrypt颁发DV(域名验证)SSL,适合博客和一般网站。付费SSL提供DV、OV(组织验证)和EV(扩展验证)级别,OV和EV需要验证企业身份,适合企业和电商网站。
运行sudo certbot renew --force-renewal,然后运行sudo systemctl reload nginx。如果自动续期配置正确,Certbot应在过期前自动续期。
可以。AsiaGB的泰国和新加坡VPS完全支持Let's Encrypt。只需将域名A记录指向VPS的IP地址,然后按照本教程的步骤操作即可。
支持Ubuntu 20.04、22.04和24.04 LTS。推荐通过snap安装,可获得最新版本和自动更新。
每个注册域名每周最多颁发50张证书,每小时最多重复申请5次。正常使用中很少达到这些限制。
在Nginx VPS上使用Certbot安装Let's Encrypt SSL只需5-10分钟,几条命令即可完成。关键是正确配置自动续期,避免证书意外过期。
如果您的业务网站需要OV或EV级别的SSL证书来验证企业身份,AsiaGB提供多种SSL证书类型,并配有专业安装支持团队。
查看AsiaGB VPS套餐