所有托管服务节省 15%

测试技能,享折扣

使用代码: Skills 开始使用
China
Linux 安全

如何在Ubuntu上使用Let’s Encrypt SSL保护Nginx(完整2025指南)

HTTPS 不再是可选的。在 2025 年,每个网站都需要 SSL/TLS 加密 — 不仅是为了保护用户数据,还要在 Google 搜索中具有竞争力并满足现代浏览器的安全要求。Let’s Encrypt 提供免费的受信任、自动续期的 SSL 证书,可与 Ubuntu 上的 Nginx 无缝协作。

本综合指南将逐步指导您完成每个步骤:安装 Certbot、获取 Let’s Encrypt SSL 证书、验证您的配置以及自动化续期 — 全部在运行 Nginx 的 Ubuntu 18.04、20.04 或 22.04 服务器上进行。

无论您是在管理 VPS Hosting 环境还是专用网络服务器,本教程都能在 15 分钟内为您提供生产就绪的 HTTPS 设置。

为什么 SSL 在 2025 年对 Nginx 很重要

在深入了解命令之前,值得理解通过 Let's Encrypt 保护 Nginx 能获得什么:

  • 数据加密: HTTPS 加密服务器和访问者之间的所有流量,防止中间人攻击和窃听。
  • SEO 排名信号: Google 已确认 HTTPS 是排名因素。没有 SSL 的网站在搜索结果中会受到主动处罚。
  • 浏览器信任指示: Chrome、Firefox 和 Edge 对 HTTP 网站显示”不安全”警告,破坏用户信心并增加跳出率。
  • 免费且自动化: Let's Encrypt 免费颁发证书,Certbot 自动处理续期 — 消除了付费证书的手动开销。
  • PCI DSS 和合规性: 任何处理支付或个人数据的网站在法律上必须使用加密连接。

如果您的域名已注册并指向您的服务器,您已准备好开始。如果您仍需要域名,AlexHost 提供价格实惠的域名注册服务,支持即时 DNS 管理。

前置条件

在继续之前,请确保满足以下条件:

要求详情
操作系统Ubuntu 18.04、20.04 或 22.04 LTS
Web 服务器已安装 Nginx 并正在为您的网站提供服务
域名已注册的域名,其 A 记录指向您服务器的公网 IP
服务器访问SSH 访问权限,用户具有 sudo 权限
防火墙在 UFW 或 iptables 中开放端口 80 (HTTP) 和 443 (HTTPS)
Root 访问不是严格必需的,但 sudo 访问是强制性的

验证 Nginx 正在运行

在安装 Certbot 之前,请确认 Nginx 处于活动状态:

sudo systemctl status nginx

您应该在输出中看到 active (running)。如果未安装 Nginx,请运行:

sudo apt update && sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

开放所需的防火墙端口

如果启用了 UFW,请允许 HTTP 和 HTTPS 流量:

sudo ufw allow 'Nginx Full'
sudo ufw reload
sudo ufw status

输出应显示 Nginx FullALLOW,适用于 IPv4 和 IPv6。

第 1 步 — 安装 Certbot 和 Nginx 插件

Certbot 是官方的 Let’s Encrypt 客户端。它自动化整个证书生命周期:颁发、安装和续期。python3-certbot-nginx 插件允许 Certbot 读取您的 Nginx 配置并自动修改它。

更新您的软件包索引

在安装新软件之前,始终更新您的软件包列表:

sudo apt update
sudo apt upgrade -y

安装 Certbot 和 Nginx 插件

sudo apt install certbot python3-certbot-nginx -y

验证安装

certbot --version

预期输出(版本可能有所不同):

certbot 2.x.x

> Ubuntu 18.04 用户注意:如果默认 APT 版本的 Certbot 已过时,请通过 Snap 安装最新版本:

> “`bash

> sudo snap install –classic certbot

> sudo ln -s /snap/bin/certbot /usr/bin/certbot

> “`

第 2 步 — 配置您的 Nginx 服务器块

Certbot 需要在 Nginx 配置中检测您的域名以自动修改它。如果您还没有为您的域名创建服务器块,请现在创建。

创建服务器块

在本指南中,将 yourdomain.com 替换为您的实际域名:

sudo nano /etc/nginx/sites-available/yourdomain.com

添加以下基本配置:

server {
    listen 80;
    listen [::]:80;

    server_name yourdomain.com www.yourdomain.com;

    root /var/www/yourdomain.com/html;
    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

启用服务器块

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

测试 Nginx 配置

始终在重新加载前验证您的 Nginx 配置:

sudo nginx -t

预期输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载 Nginx

sudo systemctl reload nginx

第 3 步 — 获取 Let’s Encrypt SSL 证书

配置好 Nginx 并安装 Certbot 后,您现在可以请求 SSL 证书。

使用 Nginx 插件运行 Certbot

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

标志说明:

    --nginx — 告诉 Certbot 使用 Nginx 插件进行自动配置
    -d yourdomain.com — 指定主域名
    -d www.yourdomain.com — 将 www 子域名添加到同一证书(主题备用名称)
    
    交互式提示说明
    Certbot 将引导您完成一个简短的设置向导:
    1. 电子邮件地址
    Enter email address (used for urgent renewal and security notices):
    提供有效的电子邮件。Let’s Encrypt 使用此邮箱通知您证书过期和安全问题。
    2. 服务条款
    Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf
    (A)gree/(C)ancel:
    输入 A 并按 Enter。
    3. EFF 新闻通讯(可选)
    Would you be willing to share your email address with the Electronic Frontier Foundation?
    (Y)es/(N)o:
    这是可选的。如果您不想订阅,输入 N。
    4. HTTP 到 HTTPS 重定向
    Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
    1: No redirect
    2: Redirect - Make all requests redirect to secure HTTPS access
    始终选择选项 2。这确保所有访问者自动升级到 HTTPS,消除混合内容问题并改进安全态势。
    成功输出
    如果一切配置正确,您将看到:
    Successfully received certificate.
    Certificate is saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    Key is saved at:         /etc/letsencrypt/live/yourdomain.com/privkey.pem
    This certificate expires on YYYY-MM-DD.
    These files will be updated when the certificate renews.
    
    Deploying certificate
    Successfully deployed certificate for yourdomain.com to /etc/nginx/sites-available/yourdomain.com
    Successfully deployed certificate for www.yourdomain.com to /etc/nginx/sites-available/yourdomain.com
    Congratulations! You have successfully enabled HTTPS on https://yourdomain.com and https://www.yourdomain.com
    Certbot 在 Nginx 中修改的内容
    运行后,Certbot 会自动将 SSL 指令附加到您的服务器块。您的配置现在将类似于:
    server {
        server_name yourdomain.com www.yourdomain.com;
    
        root /var/www/yourdomain.com/html;
        index index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        listen 443 ssl;
        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;
    }
    
    server {
        if ($host = www.yourdomain.com) {
            return 301 https://$host$request_uri;
        }
        if ($host = yourdomain.com) {
            return 301 https://$host$request_uri;
        }
    
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
        return 404;
    }
    第 4 步 — 验证 SSL 安装
    Certbot 完成后,验证您的 SSL 证书是否正确安装并正常运行。
    方法 1:浏览器检查
    打开您的网络浏览器并导航到 https://yourdomain.com。查找:
    
    地址栏中的挂锁图标 — 确认活跃的、受信任的 SSL 连接
    没有安全警告 — 证书有效且受您的浏览器信任
    URL 中的 HTTPS — HTTP 请求应自动重定向到 HTTPS
    
    点击挂锁图标并选择”证书”以查看颁发者(应显示”Let’s Encrypt”)和过期日期。
    方法 2:SSL Labs 深度分析
    如需全面的安全审计,请使用 Qualys SSL Labs:
    
    导航到 https://www.ssllabs.com/ssltest/
  • 输入您的域名
  • 点击”提交”
  • 配置正确的 Let’s Encrypt + Nginx 设置应获得 A 或 A+ 评分。如果评分较低,SSL Labs 将识别需要解决的具体弱点。

    方法 3:命令行验证

    直接从终端检查证书详情:

    sudo certbot certificates

    输出:

    Found the following certs:
      Certificate Name: yourdomain.com
        Serial Number: abc123...
        Key Type: RSA
        Domains: yourdomain.com www.yourdomain.com
        Expiry Date: YYYY-MM-DD HH:MM:SS+00:00 (VALID: 89 days)
        Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
        Private Key Path: /etc/letsencrypt/live/yourdomain.com/privkey.pem

    方法 4:OpenSSL 命令

    使用 OpenSSL 验证证书链和过期时间:

    echo | openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 2>/dev/null | openssl x509 -noout -dates -issuer

    预期输出:

    notBefore=Mon Jan  1 00:00:00 2025 GMT
    notAfter=Tue Apr  1 00:00:00 2025 GMT
    issuer=C=US, O=Let's Encrypt, CN=R11

    第 5 步 — 设置自动证书续期

    Let's Encrypt 证书在 90 天后过期。这个较短的有效期是有意设计的 — 它限制了私钥泄露时的风险敞口。Certbot 会自动处理续期,但你应该验证自动化是否正常工作。

    Certbot 续期的工作原理

    当你通过 APT 或 Snap 安装 Certbot 时,它会自动创建一个 systemd timer(或在较旧系统上创建 cron 任务),每天运行两次。它检查是否有任何证书在过期前 30 天内,并自动续期。

    验证 Systemd Timer

    sudo systemctl status certbot.timer

    预期输出:

    ● certbot.timer - Run certbot twice daily
         Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
         Active: active (waiting) since ...
        Trigger: ...
       Triggers: ● certbot.service

    如果 timer 处于活跃且启用状态,自动续期已经配置好了。

    测试续期过程(干运行)

    在依赖自动续期之前,模拟它以确认一切正常:

    sudo certbot renew --dry-run

    成功输出:

    Simulating renewal of an existing certificate for yourdomain.com and www.yourdomain.com
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Congratulations, all simulated renewals succeeded:
      /etc/letsencrypt/live/yourdomain.com/fullchain.pem (success)
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    如果干运行成功,你的证书将自动续期,无需任何手动干预。

    验证 Cron 任务(旧系统)

    在不使用 systemd timer 的较旧 Ubuntu 版本上,检查 Certbot cron 任务:

    sudo cat /etc/cron.d/certbot

    你应该看到类似的一行:

    0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

    这会每天在随机时间间隔运行 Certbot 两次,以避免过载 Let's Encrypt 服务器。

    手动续期(如果需要)

    如果你需要手动强制续期:

    sudo certbot renew --force-renewal
    sudo systemctl reload nginx

    > 最佳实践:在任何证书续期后(手动或自动),重新加载 Nginx 以确保它获取新的证书文件:

    > “`bash

    > sudo systemctl reload nginx

    > “`

    > Certbot 的续期钩子在大多数配置中会自动处理这个问题。

    配置续期钩子

    为了确保 Nginx 在每次续期后自动重新加载,添加一个部署钩子:

    sudo nano /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

    添加以下内容:

    #!/bin/bash
    systemctl reload nginx

    使其可执行:

    sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

    故障排除常见问题

    错误:”Could not bind to IPv4/IPv6″

    Certbot 使用端口 80 进行域名验证。如果另一个进程正在使用端口 80,验证将失败。

    解决方案:临时停止 Nginx,以独立模式获取证书,然后重新启动:

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

    错误:”DNS problem: NXDOMAIN looking up A for yourdomain.com”

    您的域名 DNS 记录尚未传播到 Let’s Encrypt 的验证服务器。

    解决方案:验证您的 A 记录设置正确:

    dig yourdomain.com A +short

    输出应返回您服务器的公网 IP 地址。DNS 传播最多可能需要 48 小时,但通常在 1-2 小时内解决。

    错误:”Too many certificates already issued”

    Let’s Encrypt 对每个域名每周强制执行 5 个重复证书的速率限制。

    解决方案:使用 --staging 标志测试您的设置,而不会触发速率限制:

    sudo certbot --nginx --staging -d yourdomain.com -d www.yourdomain.com

    测试完成后,撤销暂存证书并颁发生产证书。

    错误:”Nginx configuration test failed”

    Certbot 修改您的 Nginx 配置。如果生成的配置有语法错误,Nginx 将无法重新加载。

    解决方案:手动测试并修复配置:

    sudo nginx -t
    sudo nano /etc/nginx/sites-available/yourdomain.com
    # Fix any syntax errors, then:
    sudo systemctl reload nginx

    证书未自动续期

    如果您收到过期警告电子邮件,尽管已安装 Certbot,请检查计时器状态:

    sudo systemctl status certbot.timer
    sudo journalctl -u certbot.service --since "7 days ago"

    查看日志中的错误并相应地解决。

    高级:加强您的 Nginx SSL 配置

    默认的 Let’s Encrypt + Certbot 配置是安全的,但您可以进一步加强它以获得 A+ SSL Labs 评分。

    启用 HTTP/2

    编辑您的服务器块以启用 HTTP/2 以提高性能:

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    添加 HSTS(HTTP 严格传输安全)

    HSTS 指示浏览器始终为您的域使用 HTTPS,即使用户输入 http://

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

    禁用弱协议和密码

    将这些指令添加到您的 nginx.conf 或服务器块:

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    进行更改后,始终测试并重新加载:

    sudo nginx -t && sudo systemctl reload nginx

    为您的 Nginx + SSL 设置选择合适的托管

    SSL 安全的 Nginx 服务器的性能和可靠性在很大程度上取决于底层基础设施。以下是 AlexHost 的托管选项如何与不同用例相适应:

    • VPS 托管 — 适合大多数网站和应用程序。完全的 root 访问权限让您可以按照本指南中的描述精确配置 Nginx 和 Certbot。AlexHost VPS 计划运行在 Ubuntu 18.04、20.04 和 22.04 LTS 上。
    • 独立服务器 — 最适合需要最大性能、专用资源和完整硬件控制的高流量网站。运行多个 Nginx 虚拟主机,每个都有自己的 Let’s Encrypt 证书。
    • 带 cPanel 的 VPS — 如果您更喜欢图形界面来管理 SSL 证书以及您的网络托管,cPanel 与 Let’s Encrypt 集成,并通过其 AutoSSL 功能处理证书颁发。
    • SSL 证书 — 对于企业环境、电子商务网站或需要扩展验证 (EV) 或组织验证 (OV) 证书的应用程序,AlexHost 提供具有担保覆盖和专业支持的高级 SSL 证书。

    结论

    在 Ubuntu 上使用 Let’s Encrypt SSL 证书保护 Nginx 是保护用户、改进搜索引擎排名和建立受众信任的最有影响力的步骤之一。整个过程——从安装 Certbot 到启用自动续期——只需不到 15 分钟,且完全免费。

    以下是您在本指南中完成的内容回顾:

    1. ✅ 安装了 Certbot 和 Nginx 插件
    2. ✅ 为您的域名配置了正确的 Nginx 服务器块
    3. ✅ 获得了受信任的 Let’s Encrypt SSL 证书
    4. ✅ 使用浏览器工具、SSL Labs 和 OpenSSL 验证了证书
    5. ✅ 通过 systemd 计时器和干运行测试确认了自动续期
    6. ✅ 应用了高级加固以获得 A+ 安全评级

    现在您的 Nginx 服务器已经得到保护,可以考虑探索额外的保护层:Web 应用防火墙 (WAF)、用于暴力破解保护的 fail2ban 以及定期安全审计。如果您管理多个域名或应用程序,AlexHost 的 VPS 控制面板提供了一个简化的界面来管理 SSL 证书、虚拟主机和服务器配置,同时不会牺牲 Linux 环境的灵活性。

    您的用户值得拥有安全的连接。现在他们有了。