介绍

Certbot 是一个由 Electronic Frontier Foundation (EFF) 开发的免费、开源的工具,用于自动化在 Web 服务器上部署 SSL/TLS 证书。SSL/TLS 证书是用于加密网站与用户之间传输的数据,确保数据传输的安全性和隐私性。

Certbot 支持大多数常见的 Web 服务器,包括 Apache、Nginx、IIS 等。

安装 certbot

(1)安装snap

certbot官方已经说了,各个发行版安装的certbot版本落后,功能不全,推荐我们使用snap安装,我之前就是用的是apt安装,后来一堆问题,血的教训!!

参考文档:https://snapcraft.io/docs/installing-snap-on-debian

# 安装snap
sudo apt update
sudo apt install snapd

# 重启系统
# 安装core
sudo snap install core
sudo snap refresh core

(2)安装certbot

安装之前确保没有其他方式安装的certbot

如:apt,pip

可通过这条命令查看 whereis certbot

如果有,卸载它们

sudo apt remove certbot

sudo pip uninstall certbot

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

(3)查看版本

sudo certbot --version

插件

查看已经安装的插件

sudo certbot plugins

nginx

使用snap安装的版本是已经有的,申请方法为:

(1)先新建一个 nginx的配置文件,如 /etc/nginx/conf.d/abc.conf

(2)编辑 abc.conf,填入以下模板,根据自己实际情况修改

server {
    listen 80;
    server_name domain.com;

    location / {
        proxy_pass http://127.0.0.1:80;  # 替换为你的应用程序地址
        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;
    }
}

(3)使用 certbot 的nginx插件申请

# 可以先查看哪些可以申请,是否列出了需要的域名
sudo certbot --nginx

额外的,申请前保证域名解析到当前的ip

certbot-dns-dnspod

此插件用于dns申请的,好处可以用于申请泛域名证书证书。也就是像 *.seektao.cc这种,一次申请,到处使用,方便至极。

certbot-dns-dnspod 是dnspod的插件,默认的certbot是没有的,需要手动安装

Github地址

https://github.com/tengattack/certbot-dns-dnspod

sudo snap install certbot-dns-dnspod
sudo snap set certbot trust-plugin-with-root=ok
sudo snap connect certbot:plugin certbot-dns-dnspod

pip

sudo pip install git+https://github.com/tengattack/certbot-dns-dnspod.git

安装完成之后新建一个 .ini文件,比如 /etc/certbot/dnspod.ini

sudo mkdir /etc/certbot
sudo touch /etc/certbot/dnspod.ini

编辑 dnspod.ini,填入下面的内容,点此跳转到dnspod api 申请页面,注意申请的是dnspod token

dns_dnspod_api_id = 12345
dns_dnspod_api_token = 1234567890abcdef1234567890abcdef
sudo chmod 600 /etc/certbot/dnspod.ini

申请,替换为自己的域名

sudo certbot certonly -a dns-dnspod \
    --dns-dnspod-credentials /etc/certbot/dnspod.ini \
    -d seektao.cc \
    -d "*.seektao.cc"


接着就是:
输入邮箱,
是否同意注册acme(选Y),
是否同意发送邮件。。。看自己吧

接着等待,等待。。就完成了

通过日志可以看到证书文件保存在 /etc/letsencrypt/live

# 其中 
[cert name]/privkey.pem:证书的私钥。
[cert name]/fullchain.pem:在大多数服务器软件中使用的证书文件。
[cert name]/chain.pem:在Nginx >=1.3.7 中用于 OCSP stapling。
[cert name]/cert.pem:会破坏许多服务器配置,不应在未进一步阅读文档的情况下使用。

我们用到 privkey.pem,fullchain.pem即可。

此节关于 dnspod 申请ssl证书就是如此。

certbot-dns-cloudflare

帮助文档:https://certbot-dns-cloudflare.readthedocs.io/en/stable/

sudo snap install certbot-dns-cloudflare

# 如果提示如下信息,输入:sudo snap set certbot trust-plugin-with-root=ok
# 接着重新执行 sudo snap install certbot-dns-cloudflare

error: cannot perform the following tasks:
- Run hook prepare-plug-plugin of snap "certbot" (run hook "prepare-plug-plugin": 
-----
Only connect this interface if you trust the plugin author to have root on the system.
Run `snap set certbot trust-plugin-with-root=ok` to acknowledge this and then run this command again to perform the connection.
If that doesn't work, you may need to remove all certbot-dns-* plugins from the system, then try installing the certbot snap again.
-----)

申请api token,点此跳转

新建 cloudflare.ini,将下面的值替换为刚才自己申请的

# Cloudflare API token used by Certbot
dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567

建议修改权限

sudo chmod 600 cloudflare.ini

申请方式:将以下的文件和域名替换为自己的
(1)

sudo certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
  -d example.com

(2)

sudo certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
  -d example.com \
  -d www.example.com

自动续期

自动续期添加一个定时任务即可

# 设置定时任务
sudo crontab -e

# 每月1号的午夜执行 certbot renew 命令来续订证书
0 0 1 * * /usr/local/bin/certbot renew

需要注意自己的certbot执行路径是否正确

which certbot 可以看到certbot执行路径

关于续期的更多文档可以观看

https://eff-certbot.readthedocs.io/en/latest/using.html#setting-up-automated-renewal