返回博客
一月 13, 2026指南

如何使用 Let's Encrypt 设置 SSL 证书

通过使用 Certbot 为 Nginx 和 Apache 安装 Let's Encrypt 免费 SSL 证书的逐步指南。

如何使用 Let's Encrypt 设置 SSL 证书

SSL 证书加密服务器与访问者浏览器之间的数据,确保连接安全。Let's Encrypt 提供被所有主流浏览器信任的免费 SSL 证书。本指南向您展示如何在 Hiddence 服务器上设置 SSL。

先决条件

  • 指向服务器 IP 的域名
  • 已安装 Nginx 或 Apache Web 服务器
  • 防火墙已打开 80 和 443 端口
  • 服务器的 root 或 sudo 访问权限

安装 Certbot

bash
# 适用于 Ubuntu/Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx -y

# 适用于 CentOS/RHEL
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-nginx -y

为 Nginx 获取证书

Certbot 可以自动配置 Nginx。运行:

bash
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
# 按照提示完成设置

为 Apache 获取证书

对于 Apache,请使用:

bash
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
# 按照提示完成设置

手动证书续期

Let's Encrypt 证书在 90 天后过期。测试续期:

bash
sudo certbot renew --dry-run

设置自动续期

Certbot 会自动创建一个 cron 任务。验证其是否存在:

bash
sudo systemctl status certbot.timer
# 或检查 crontab
sudo crontab -l | grep certbot

实用提示

  • 证书会在过期前 30 天自动续期
  • 测试续期过程:sudo certbot renew --dry-run
  • 为子域名使用通配符证书:certbot certonly --dns-cloudflare
  • 检查证书过期时间:sudo certbot certificates
  • 如果需要,强制续期:sudo certbot renew --force-renewal