Let's Encrypt 自动续期配置教程
Let's Encrypt 证书特点
- 证书有效期是 90天;
- Certbot 自带「续期」功能;
- 正确设置后,可以自动更新证书,不用手动管理!
安装 let`s encrypt
安装 Certbot:Certbot 是一个自动化工具,帮助申请和管理 Let's Encrypt 证书。可以根据操作系统安装 Certbot,通常可以通过包管理器安装。
- Ubuntu/Debian 系统:
sudo apt install certbot - CentOS/RHEL 系统:
sudo yum install certbot
申请证书: 如果使用的是 Apache 或 Nginx 等常见的 Web 服务器,Certbot 可以自动设置 SSL 证书。
- 对于 Apache:
sudo certbot --apache - 对于 Nginx:
sudo certbot --nginx
自动续期:Let's Encrypt 的证书有效期是 90 天,但 Certbot 会自动为你续期。你可以通过设置 cron 任务或 systemd 来定期执行证书续期。
如果出现下面这种情况
root@xiaogang:~# sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The requested nginx plugin does not appear to be installed
使用命令
sudo apt install python3-certbot-nginx
安装完成后,再执行:
sudo certbot --nginx
安装过程
root@xiaogang:~# certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 这里输入你的邮件名
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated) (Enter 'c' to cancel): 这里输入自己的域名
Obtaining a new certificate
配置 ngin x
虽然上面的命令会主动配置nginx,但因为某些原因(比如 Nginx 配置不标准,或者 Nginx 配置太简单/复杂),没法直接自动修改,可能提示「Unable to install the certificate」。
那就自己手动配置nginx
贴上自己的配置
server {
listen 80;
server_name www.iamxiaogang.fun;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name www.iamxiaogang.fun;
ssl_certificate /etc/letsencrypt/live/www.iamxiaogang.fun/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.iamxiaogang.fun/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
这样配置之后:
- 访问
http://www.iamxiaogang.fun➔ 自动跳到https://www.iamxiaogang.fun - HTTPS 访问时,Nginx 会用 Let's Encrypt 的证书
- 后端 Flask 项目也照常工作(proxy_pass 保持不变)
然后测试nginx配置是否有错
nginx -t
如果没错,那就重启nginx服务
sudo systemctl reload nginx
然后访问自己的网站,https + 域名
手动测试续期命令
在服务器上运行以下命令:
sudo certbot renew --dry-run
如果看到最后出现 Congratulations, all renewals succeeded,说明续期流程正常。
设置定时任务(Crontab 自动续期)
编辑定时任务:
sudo crontab -e
在最后添加:
0 2 * * * /usr/bin/certbot renew --quiet && /usr/sbin/nginx -s reload
- 每天凌晨 2 点自动检查证书是否需要续期。
- 如果证书续期成功,会自动 reload Nginx 让新证书生效。
(注意:/usr/bin/certbot 和 /usr/sbin/nginx 路径可使用 which certbot 和 which nginx 检查确认)
总结
-
证书续期命令测试成功。
-
每天定时检查,完全自动续期。
-
续期成功后,自动让 Nginx 用上新证书。