--- title: "certbot nginx SSL 证书管理" date: 2021-09-29T00:37:08+07:00 draft: false --- ## HTTPS 证书 现在很多站的 HTTPS 使用的是 Let's Encrypt 颁发的免费证书,证书 90 天过期,自动续期基本往往都会出现问题导致不能真正的自动续期。甚至后来的手动续期也会有各种各样的问题。比如: ```shell Your system is not supported by certbot-auto anymore. ``` ## 一般性方法 我推荐到 Let's Encrypt 的官方网站上参看指导方案解决。 打开网站[https://certbot.eff.org/instructions](https://certbot.eff.org/instructions),从页面中找到 My HTTP website is running () on () 这句话,从地一个下拉列表中选择自己的 Web 服务器类型,从第二个下拉列表中选择操作系统或者使用的管理工具。 本篇文章就是这个页面的中文翻译,对应 My HTTP website is running (nginx) on (pip)。我的 Server 是 Debian9,使用 pip 是因为这个解决方案可能有更好的通用性。 ## 使用 pip3 管理证书 1. 首先更新 ```shell $ sudo apt updte $ sudo apt install python3 python3-venv libaugeas0 ``` 2. 卸载旧版本 certbot ```shell $ sudo apt remove certbot ``` 3. 建立 Python 虚拟环境 ```shell $ sudo python3 -m venv ./certbot $ cd certbot $ source bin/activate $ pip install --upgrade pip ``` 4. 安装 certbot ```shell $ pip installl certbot certbot-nginx $ ln -s ./bin/certbot ./certbot ``` 5. 安装颁发证书 - 一步到位执行 ```shell $ sudo certbot --nginx ``` - 手动管理 nginx .well_known ```shell $ sudo certbot certonly --nginx ``` 6. 设置自动续期 在 /etc/crontab 中追加 ```shell $ echo "0 0,12 * * * root /opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null ``` 纯测试可运行 ```shell $ sudo certbot renew --dry-run ``` 7. 更新 certbot 在 3 中的虚拟环境中执行 ```shell $ sudo pip3 --upgrade certbot certbot-nginx ``` 出错的话删掉虚拟环境重装。 原文链接 [https://certbot.eff.org/lets-encrypt/pip-nginx](https://certbot.eff.org/lets-encrypt/pip-nginx)