Skip to content

使用 Let's Encrypt生成证书

要为主域名和子域名生成 SSL 证书,你可以使用 Let's Encrypt 的 Certbot。以下是基本步骤:

  1. 安装 Certbot

    • 在你的服务器上安装 Certbot。可以使用以下命令(以 Ubuntu 为例):
      bash
      sudo apt update
      sudo apt install certbot python3-certbot-nginx
  2. 获取证书

    • 使用 Certbot 生成 SSL 证书。假设你的主域名是 example.com,子域名是 sub.example.com,可以运行以下命令:
      bash
      # 自动配置 Nginx
      sudo certbot --nginx -d example.com -d sub.example.com 
      # 手动
      sudo certbot certonly --manual -d example.com -d sub.example.com
      # certonly 选项表示只获取证书,而不自动配置 Web 服务器。
      # manual 选项表示手动验证域名所有权。
  3. 自动续期

    • Certbot 会自动配置证书的续期。你可以通过以下命令手动测试续期:
      bash
      sudo certbot renew --dry-run
  4. 确认 SSL 配置

    • 确保你的 Nginx 配置文件中启用了 SSL,并指向正确的证书文件。通常情况下,Certbot 会自动完成这一步。
  5. 重启 Nginx

    • 在配置完成后,重启 Nginx:
      bash
      sudo systemctl restart nginx