免费 HTTPS 证书安装与续期完全指南

1 阅读6分钟

免费 HTTPS 证书安装与续期完全指南

使用 Let's Encrypt 免费证书 + Certbot 自动管理工具 适用系统:Ubuntu/Debian + Nginx


目录

  1. 什么是 HTTPS 证书
  2. 为什么选择 Let's Encrypt
  3. 安装前准备
  4. 安装 Certbot
  5. 申请证书
  6. 配置 Nginx
  7. 自动续期设置
  8. 验证安装
  9. 常见问题
  10. 手动续期

一、什么是 HTTPS 证书

HTTPS 证书(SSL/TLS 证书)用于:

  • 加密数据传输 - 防止信息被窃听
  • 验证网站身份 - 确保你访问的是真实网站
  • 提升搜索排名 - Google 优先收录 HTTPS 网站
  • 浏览器信任 - 没有证书会显示"不安全"警告

二、为什么选择 Let's Encrypt

对比项Let's Encrypt付费证书
价格完全免费¥几百~几千/年
有效期90天(自动续期)1~2年
安全性与付费证书相同与免费证书相同
浏览器支持99.9% 信任99.9% 信任
适用场景个人/中小企业大型企业/特殊需求

结论:对于绝大多数网站,Let's Encrypt 完全够用!


三、安装前准备

3.1 必要条件

  • 一台服务器(VPS/云服务器)
  • 一个域名(如 example.com)
  • 域名已解析到服务器 IP
  • 服务器已安装 Nginx
  • 80 和 443 端口已开放

3.2 检查域名解析

# 检查域名是否指向你的服务器 IP
dig +short yourdomain.com
# 或
nslookup yourdomain.com

3.3 检查端口开放

# 检查 80 端口
sudo ss -tlnp | grep :80

# 检查 443 端口
sudo ss -tlnp | grep :443

四、安装 Certbot

Certbot 是 Let's Encrypt 官方推荐的客户端工具。

4.1 使用 Snap 安装(推荐)

# 安装 snap 版 certbot
sudo snap install --classic certbot

# 创建软链接
sudo ln -sf /snap/bin/certbot /usr/bin/certbot

# 验证安装
certbot --version

4.2 使用 APT 安装(备用)

# 更新软件源
sudo apt update

# 安装 certbot 和 nginx 插件
sudo apt install -y certbot python3-certbot-nginx

五、申请证书

5.1 方式一:自动配置 Nginx(最简单)

# 自动获取证书并配置 nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

按提示操作:

  1. 输入邮箱地址(用于接收续期提醒)
  2. 同意服务条款
  3. 选择是否分享邮箱给 EFF

5.2 方式二:手动配置(更灵活)

步骤 1:创建网站根目录
sudo mkdir -p /var/www/yourdomain.com
sudo chown -R $USER:$USER /var/www/yourdomain.com
步骤 2:创建临时 Nginx 配置
sudo tee /etc/nginx/sites-available/yourdomain.com << 'EOF'
server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;

    # ACME 验证目录
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        root /var/www/yourdomain.com;
        index index.html;
    }
}
EOF
步骤 3:启用配置
# 创建软链接
sudo ln -sf /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

# 删除默认配置(可选)
sudo rm -f /etc/nginx/sites-enabled/default

# 测试配置
sudo nginx -t

# 重载 nginx
sudo systemctl reload nginx
步骤 4:申请证书
# 使用 webroot 方式申请
sudo certbot certonly --webroot \
    -w /var/www/certbot \
    -d yourdomain.com \
    -d www.yourdomain.com \
    --agree-tos \
    --no-eff-email \
    --email your-email@example.com

六、配置 Nginx

6.1 完整 HTTPS 配置示例

sudo tee /etc/nginx/sites-available/yourdomain.com << 'EOF'
# HTTP 服务器 - 自动跳转到 HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;

    # ACME 验证(续期需要)
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    # 其他请求跳转到 HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
}

# HTTPS 服务器
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name yourdomain.com www.yourdomain.com;

    # SSL 证书路径
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;

    # SSL 安全配置
    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_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;

    # 网站根目录
    root /var/www/yourdomain.com;
    index index.html index.htm;

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

    # 安全响应头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
}
EOF

6.2 应用配置

# 测试配置语法
sudo nginx -t

# 重载 nginx
sudo systemctl reload nginx

七、自动续期设置

7.1 检查自动续期是否已启用

Snap 安装的 Certbot 会自动创建续期任务:

# 查看定时任务
sudo systemctl list-timers | grep certbot

# 输出示例:
# snap.certbot.renew.timer  snap.certbot.renew.service

7.2 手动配置续期(如需要)

创建续期脚本
sudo tee /usr/local/bin/certbot-renew.sh << 'EOF'
#!/bin/bash
# 尝试续期证书
/usr/bin/certbot renew --quiet --no-self-upgrade

# 如果续期成功,重载 nginx
if [ $? -eq 0 ]; then
    /bin/systemctl reload nginx
fi
EOF

sudo chmod +x /usr/local/bin/certbot-renew.sh
添加 Cron 任务
# 编辑 crontab
sudo crontab -e

# 添加以下行(每天凌晨 3 点检查续期)
0 3 * * * /usr/local/bin/certbot-renew.sh >> /var/log/certbot-renew.log 2>&1

八、验证安装

8.1 浏览器验证

打开浏览器访问:https://yourdomain.com

应该看到:

  • 🔒 锁形图标(表示安全连接)
  • 没有"不安全"警告
  • 证书信息显示 Let's Encrypt

8.2 命令行验证

# 检查证书信息
certbot certificates

# 输出示例:
# Found the following certs:
#   Certificate Name: yourdomain.com
#     Domains: yourdomain.com www.yourdomain.com
#     Expiry Date: 2026-07-09 09:35:44+00:00
#     Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
#     Private Key Path: /etc/letsencrypt/live/yourdomain.com/privkey.pem

8.3 在线检测工具


九、常见问题

Q1: 证书申请失败,提示 "Connection refused"

原因:80 端口未开放或被防火墙阻挡

解决

# 检查防火墙
sudo ufw status
# 或
sudo iptables -L -n | grep :80

# 开放 80 端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Q2: 提示 "No valid IP addresses found"

原因:域名未解析到服务器 IP

解决:检查 DNS 设置,确保 A 记录指向服务器 IP

Q3: 证书已过期,自动续期失败

解决

# 手动强制续期
sudo certbot renew --force-renewal

# 重载 nginx
sudo systemctl reload nginx

Q4: 如何添加更多域名到现有证书?

# 使用 certonly 重新申请,包含所有域名
sudo certbot certonly --webroot -w /var/www/certbot \
    -d yourdomain.com \
    -d www.yourdomain.com \
    -d api.yourdomain.com \
    -d blog.yourdomain.com

Q5: 如何删除证书?

# 删除证书
sudo certbot delete --cert-name yourdomain.com

# 然后手动删除 nginx 配置中的 SSL 相关行

十、手动续期

10.1 测试续期流程

# 模拟续期(不真续期,仅测试)
sudo certbot renew --dry-run

10.2 立即续期

# 续期所有即将到期的证书
sudo certbot renew

# 强制续期(不管是否到期)
sudo certbot renew --force-renewal

# 续期后重载 nginx
sudo systemctl reload nginx

10.3 查看续期日志

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

# 查看续期脚本日志
sudo tail -f /var/log/certbot-renew.log

附录:常用命令速查

# 安装
certbot --version                          # 查看版本
certbot certificates                       # 查看所有证书
certbot certonly --webroot -w /var/www/certbot -d example.com  # 申请证书

# 续期
certbot renew                              # 续期所有证书
certbot renew --dry-run                    # 测试续期
certbot renew --force-renewal              # 强制续期

# 删除
certbot delete --cert-name example.com     # 删除证书

# Nginx
nginx -t                                   # 测试配置
systemctl reload nginx                     # 重载配置
systemctl restart nginx                    # 重启服务

总结

步骤命令说明
1sudo snap install --classic certbot安装 Certbot
2sudo certbot --nginx -d yourdomain.com申请证书
3sudo nginx -t && sudo systemctl reload nginx应用配置
4certbot certificates验证安装
5自动续期已内置,无需操作

记住:Let's Encrypt 证书永久免费,每 90 天自动续期,放心使用!


教程编写时间:2026-04-10
适用 Certbot 版本:5.5.0