在Nginx上使用Certbot安装Let's Encrypt SSL证书 — 完整教程

在Nginx VPS上使用Certbot安装Let's Encrypt SSL

目录

  1. 什么是Let's Encrypt和Certbot?
  2. 安装前的准备工作
  3. 在Ubuntu上安装Certbot
  4. 申请SSL证书
  5. 配置Nginx支持HTTPS
  6. 设置自动续期
  7. 验证SSL — 达到A级评分
  8. 常见问题排查
  9. 常见问题解答(FAQ)
  10. 总结

什么是Let's Encrypt和Certbot?

Let's Encrypt 是由互联网安全研究组(ISRG)运营的免费、自动化、开放的证书颁发机构(CA)。它免费颁发域名验证(DV)SSL证书,获得Mozilla、Google、Cisco等科技巨头的支持。

Certbot 是电子前哨基金会(EFF)开发的ACME协议客户端,能自动完成Let's Encrypt证书的申请和续期,原生支持Nginx和Apache插件。

Let's Encrypt的优势: 完全免费 · 自动续期 · 支持通配符 · 所有主流浏览器信任 · 2秒内完成颁发

安装前的准备工作

在安装Certbot之前,请确认以下条件都已满足:

要求详细说明状态
Ubuntu VPSUbuntu 20.04 / 22.04 / 24.04✅ 必须
已安装Nginx建议1.18版本以上✅ 必须
域名已解析到VPSA记录 → VPS IP地址✅ 必须
80和443端口已开放Certbot使用HTTP验证✅ 必须
root或sudo权限安装软件包需要✅ 必须

验证Nginx正在运行:

sudo systemctl status nginx
# 应显示:Active: active (running)

在Ubuntu上安装Certbot

推荐通过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
⚠️ 注意: 如果之前通过apt安装了certbot,请先卸载:sudo apt remove certbot

申请SSL证书

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证书

通配符SSL覆盖所有子域名(*.yourdomain.com),需要使用DNS验证:

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

Certbot会要求您在DNS中添加TXT记录,等待DNS传播后按Enter继续。

配置Nginx支持HTTPS

使用--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(推荐)

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
✅ Certbot自动续期,当证书剩余有效期少于30天时自动续期,无需每90天手动操作。

验证SSL — 达到A级评分

安装完成后,使用以下工具验证SSL配置:

# 查看已颁发的证书
sudo certbot certificates

# 输出显示域名、到期日期和证书路径

常见问题排查

1. 错误:连接被拒绝 / 80端口被阻塞

# 在UFW中开放80和443端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

2. DNS问题 / 域名无法解析

域名A记录可能未指向VPS的IP,或DNS尚未传播。更改DNS后请等待24-48小时。

# 检查DNS传播
dig yourdomain.com A +short
# 应返回您的VPS IP地址

3. 证书续期失败

# 查看日志
sudo journalctl -u snap.certbot.renew --since "1 day ago"

# 或查看certbot日志
sudo tail -50 /var/log/letsencrypt/letsencrypt.log

4. Nginx: SSL_CTX_use_PrivateKey_file失败

证书与私钥不匹配,强制重新申请证书:

sudo certbot renew --force-renewal -d yourdomain.com

常见问题解答(FAQ)

Let's Encrypt真的完全免费吗?有隐性收费吗?

完全免费,没有任何隐性收费。Let's Encrypt是由Mozilla、Google、Cisco和EFF支持的非营利组织,证书申请和续期永远免费。

Let's Encrypt与付费SSL证书有什么区别?

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吗?

可以。AsiaGB的泰国和新加坡VPS完全支持Let's Encrypt。只需将域名A记录指向VPS的IP地址,然后按照本教程的步骤操作即可。

Certbot支持哪些Ubuntu版本?

支持Ubuntu 20.04、22.04和24.04 LTS。推荐通过snap安装,可获得最新版本和自动更新。

Let's Encrypt的频率限制是什么?

每个注册域名每周最多颁发50张证书,每小时最多重复申请5次。正常使用中很少达到这些限制。

总结

在Nginx VPS上使用Certbot安装Let's Encrypt SSL只需5-10分钟,几条命令即可完成。关键是正确配置自动续期,避免证书意外过期。

如果您的业务网站需要OV或EV级别的SSL证书来验证企业身份,AsiaGB提供多种SSL证书类型,并配有专业安装支持团队。

查看AsiaGB VPS套餐