• : +86 02388256629

Nginx + Let’s Encrypt + 自动续签配置教程

Nginx + Let’s Encrypt + 自动续签配置教程

1️⃣ 准备工作

  • 一台已安装 Nginx 的服务器。

  • 一个已备案/可用的域名,并将域名的 A 记录 指向你的服务器 IP。

    • 比如:yourdomain.comwww.yourdomain.com → 服务器 IP。


2️⃣ 安装 Certbot

Let’s Encrypt 官方推荐使用 Certbot 工具。

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

3️⃣ 配置 Nginx

确保你的网站配置文件里有正确的 server_name。例如:

/etc/nginx/sites-available/yourdomain.conf

server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourdomain; index index.html index.htm; }

检查配置是否正确:

sudo nginx -t sudo systemctl reload nginx

4️⃣ 申请 Let’s Encrypt 证书

执行以下命令:

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

过程说明:

  • Certbot 会自动修改 Nginx 配置,添加 HTTPS 443 配置。

  • 输入邮箱 → 接受协议 → 是否强制 HTTP 自动跳转到 HTTPS(建议选 Yes)。

申请成功后,你会看到类似提示:

Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem

5️⃣ 自动续签

Let’s Encrypt 证书有效期是 90 天,Certbot 会自动安装定时任务(cron)。

你可以手动测试:

sudo certbot renew --dry-run

如果没有报错,就说明自动续签配置成功。


6️⃣ 手动续签(可选)

如果你想自己设置 cron 任务:

sudo crontab -e

添加:

0 3 * * * /usr/bin/certbot renew --quiet

👉 每天凌晨 3 点检查并自动续签。


7️⃣ 查看证书路径

证书和私钥文件存放在:

/etc/letsencrypt/live/yourdomain.com/ ├── fullchain.pem # 证书 └── privkey.pem # 私钥

✅ 至此,你的网站就启用了 HTTPS + 自动续签,浏览器访问会显示安全锁 🔒。

Comments

Comments

There are currently no comments

New Comment

Write Your Comments