关于certbot的简单用法

技术 · 2022-04-09

最近一直在用Certbot进行Let's Encrypt的证书管理,这里大概记录一下常用的吧。

签发证书

直接通过Auth插件

安装完certbot之后直接可以通过以下代码就可以了,目前只使用过一次,虽然方便但是貌似后面续期的时候不太好用

certbot --nginx

通过webroot

目前大部分通过这种方式进行,可能自己用的不太深入吧,说一下nginx的相关配置吧,需要先添加以下配置,注意一下自己的主目录

location ~ /.well-known/acme-challenge/ {
            allow   all;
            root /usr/share/nginx/html;
            default_type    "text/plain";
    }
    location = /.well-known/acme-challenge/ {
            return 404;
    }

然后执行以下代码获取证书

certbot certonly --webroot -w /usr/share/nginx/html/ -d www.exmaple.com

然后再配置一下nginx的证书配置即可

ssl_certificate /etc/letsencrypt/live/www.exmaple.com/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/www.exmaple.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/www.exmaple.com/chain.pem;
ssl_protocols   TLSV1.1 TLSV1.2;
ssl_prefer_server_ciphers       on;
ssl_ciphers     AES256+EECDH:AES256+EDH:!aNULL;

然后在443的端口中也添加一下well-known那段配置,方便后面续期。

查看当前证书

可能有时会签好几个证书,那么可以通过以下代码查看已签发的证书

certbot certificates

删除证书

保险起见先吊销证书

certbot revoke --cert-name www.exmaple.com

成功之后进行删除

certbot delete --cert-name www.exmaple.com

续期证书

可以通过crontab添加定时任务进行证书的续期,使用renew命令进行续期,renew会使用之前成功的操作方式进行续期

certbot renew

添加crontab时注意certbot所在的路径,最好带上全路径(可以通过which certbot找到路径),避免找不到certbot而失败,执行crontab -e添加一下代码

0 5 * * 1 /usr/bin/certbot renew >> /var/log/le-renew.log

以上的命令大部分都是在debian上进行的,可能其它系统可不通过,需要注意。

ssl certbot
Theme Jasmine by Kent Liao