certbot安装ssl证书

可外网访问

1.安装certbot

apt install certbot python3-certbot-nginx

2.生成证书

certbot --nginx

输入“certbot --nginx” →输入自己申请的邮箱→选择A→选择Y→选择要生成的域名,如果是多个域名的话,用 ” , "进行分割→选择 2,这样就生成了SSL证书了
SSL证书具体位置: /ect/letsencrypt/live

3.配置nginx

因为在上面第二步中,我们直接将nginx配置写入了已存在的配置文件

4.自动更新

每月一号三时更新

crontab -e

0 3 1 * * certbot renew --force-renew

只能内网访问

利用DNS质询方法

1.开始申请证书

执行如下命令开始申请证书,按照提示操作即可:

certbot certonly --manual --preferred-challenges dns -d example.com

2.添加解析记录

当命令执行中,会收到类似如下提示,要求添加 TXT 解析记录:

Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:

667drNmQL3vX6bu8YZlgy0wKNBlCny8yrjF1lSaUndc

Once this is deployed,
Press ENTER to continue

根据上面提示,登录云商后台(比如阿里云、腾讯云等等),添加名为 _acme-challenge.example.com 的 TXT 记录,并使用 667drNmQL3vX6bu8YZlgy0wKNBlCny8yrjF1lSaUndc 作为记录值

注意事项:
1)由于 DNS 记录不会马上生效,所以稍后再按回车键。
2)使用 dig +short -t txt _acme-challenge.example.com 命令验证 DNS 是否生效。

3.配置nginx

certbot生成的证书在/etc/letsencrypt/live/example.com/目录

配置nginx文件如下

server{
    listen 443 ssl;
    server_name example.com;

    include /etc/nginx/conf.d/example.d/*;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}
server{
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    }

    server_name example.com;
    listen 80;
    return 404;
}

内网自动化配置

1.配置脚本

git clone https://gitee.com/skyyemperor/certbot-letencrypt-wildcardcertificates-alydns-au /usr/local/certbot
cd /usr/local/certbot
chmod u+x ./au.sh

修改云厂商API配置

vim au.sh
# TXY_KEY="AKIDC......."
# TXY_TOKEN="3pLabL...."

2.申请证书

certbot certonly -d 'example.com' \
    --manual --preferred-challenges dns \
    --manual-auth-hook "/usr/local/certbot/au.sh python txy add" \
    --manual-cleanup-hook "/usr/local/certbot/au.sh python txy clean"

3.自动更新

crontab -e

0 3 1 * * certbot renew --force-renew --manual --preferred-challenges dns --manual-auth-hook "/usr/local/certbot/au.sh python txy add" --manual-cleanup-hook "/usr/local/certbot/au.sh python txy clean"

hhhhh