使用 Certbot 为 Nginx 自动配置 SSL 证书
配置步骤
1. 安装Certbot和Nginx插件
sudo apt-get update |
2. 获取和安装证书
运行 Certbot 自动安装 SSL 证书。注意替换
your_domain
:
sudo certbot --nginx -d your_domain |
Certbot 将自动与 Let’s Encrypt 的服务器通信,验证域名,请求SSL证书。
3. 测试自动更新
Let’s Encrypt 的证书每 90 天需要更新一次。运行以下命令来测试自动更新是否正常工作:
sudo certbot renew --dry-run |
若测试成功,证书将自动更新,无需任何其他操作。
配置过程示例
若是第一次配置,会要求输入一个合法邮箱提供给 Let’s Encrypt。若自动更新失效,Let’s Encrypt 会在证书失效前给你发邮件。
root@VM-PV30VGNA7611:~# sudo certbot --nginx -d storage.opennet.top |
自动更新测试
root@VM-PV30VGNA7611:~# sudo certbot renew --dry-run |
Nginx 配置文件示例
在写配置文件时只需写 80 端口,Certbot 会自动添加 443 端口的监听以及 SSL 证书的配置
server { |
取消 Certbot 对网站的管理
列出 Certbot 管理的所有证书
sudo certbot certificates |
删除证书,将
name_of_certificate
替换为证书名称,此时会自动取消续期
sudo certbot delete --cert-name name_of_certificate |
2. 删除 Nginx 的 SSL 配置:
在 Nginx 配置中删除关于该证书的引用。编辑 Nginx 配置文件(通常位于
/etc/nginx/sites-available/
),删除或注释掉所有
# managed by Certbot
的代码
重新加载 Nginx
sudo systemctl reload nginx |
使用 Cloudflare API 颁发证书
apt install python3-certbot-dns-cloudflare |
创建 API 密钥文件
mkdir -p /etc/letsencrypt |
编辑这个文件,添加你的 API 密钥: