Certbot安装和使用方法

说明

Certbot是Let’s Encrypt提供的一个获取证书的程序, 支持自动获取证书(不用注册用户), 自动续期证书(免费证书只有3个月有效期, 但可以无限续期)

安装

根据系统选择安装

yum install certbot -y
yum install python3-certbot-nginx
apt install certbot python3-certbot-nginx

使用方法

1,申请SSL证书

# 如果申请多个域名,每个域名前面都是-d选项
certbot certonly --nginx -d www.xxxx.com

注意:申请证书之前必须安装好nginx,并配置好对应域名的conf文件。因为在申请域名的过程中会使用域名+相应路径进行密钥校验,下面是示例可以直接复制使用

upstream test_server {
    server 127.0.0.1:8091;
}

server {
    if ($host = www.xxxx.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen       80;
    server_name  www.xxxx.com;
    return 301   https://$server_name$request_uri;


}

server {
    listen       443 ssl http2;
    server_name  www.xxxx.com;

    access_log /var/log/nginx/xxx/access.log main;
    error_log  /var/log/nginx/xxx/error.log;
    # ssl配置,可以先注释掉,等生成完成后再修改
    # ssl_certificate /etc/letsencrypt/live/xxxx.xxxx.com/fullchain.pem; # managed by Certbot
    # ssl_certificate_key /etc/letsencrypt/live/xxxx.xxxx.com/privkey.pem; # managed by Certbot
    ssl_session_timeout  1d;
    ssl_session_cache shared:MozSSL:10m;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_protocols TLSv1.1 TLSv1.2;
    add_header Strict-Transport-Security "max-age=63072000" always;

    charset utf-8;
    client_max_body_size 4096m;
    location / {
        proxy_pass http://test_server;
        proxy_http_version 1.1;
        proxy_buffering  off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

	# 这地方主要是为了完成certbot的密钥校验,下面的return换成对应的密钥即可
    location ~ "^/\.well-known/acme-challenge/(.*)$" {
        default_type text/plain;
        return 200 "$1.IL3bE2eqHDs1k0Lmxm63CXpLvzmosMuUDIywEIBTPnG";
    }

}

第一次执行命令以后,不出意外的话会报错,如果是这个错误,就需要把报错信息中的密钥复制到上面nginx配置中重启

Detail: The key authorization file from the server did not match
[r9NH9hle6_7L9avkG-ID6A1BI4h4IgFVn6nx3VQZRpI.IL3bE2eqHDs1k0Lmxm63CXpLvzmosMuUDIywEIBTPnG]

2,续费证书

每个月的第一天检查证书是否过期,进行续签(三个月)

crontab -e
0 0 1 * * certbot renew --quiet

3,注销证书

撤销证书,certbot revoke --cert-path是固定格式,后面跟证书的最新地址

certbot revoke --cert-path /etc/letsencrypt/archive/www.xxxx.com/cert1.pem

泛域名证书

泛域名证书只适用于子域名,要主域名和子域名都用一个证书的话,可以申请多域名证书,在申请时同时加上主域名和泛域名

certbot certonly --preferred-challenges dns --manual  -d *.bysir.cn --server https://acme-v02.api.letsencrypt.org/directory
参数说明
–preferred-challenges dns认证方式选择DNS, 泛域名支持DNS
–manual手动模式
-d *.bysir.cn申请的泛域名
–server泛域名证书是新功能, 如果要使用就得加上这个参数
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.bysir.cn with the following value:

cuAVEgl69tzimaIm2ncIEIVeMLYnzw05JohSdXuAOAA

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

这一步需要手动配置TXT记录, 在域名解析服务商添加一个泛解析,设置好了敲下回车.
最后就会将生成好的证书保存到本地.

1,手动续期

手动续期需要再域名到期之前,重新申请一次域名证书,在DNS加一个TXT记录即可

2,自动续期

1)配置阿里云凭证信息

点击阿里云头像 ——》 控制访问 ——》创建一个拥有DNS权限的用户
这个用户不用太多权限,所以有 OpenAPI 的调用访问即可请添加图片描述
创建完成后,在用户界面能看到 用户对应的AccessKey ID和AccessKey Secret了,记录报错值

请添加图片描述
刷新界面后,可为用户添加权限
请添加图片描述
权限只需要有 阿里云的DNS 操作权限即可
请添加图片描述

2)配置脚本定时续期

安装 aliyun cli 工具

wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
tar xzvf aliyun-cli-linux-latest-amd64.tgz
sudo cp aliyun /usr/local/bin
rm aliyun

将拥有 DNS 权限的角色配置到云服务器中

cd aliyun /usr/local/bin
aliyun configure --profile akProfile

配置会进入交互式内容,如下输入完成配置:

Configuring profile 'akProfile' in '' authenticate mode...
Access Key Id []: 在这里输入刚新建角色的 Access Key 然后回车进入下一项
Access Key Secret []: 在这里输入刚新建角色的 Access Key Secret 然后回车进入下一项
Default Region Id []: cn-hangzhou 
Default Output Format [json]: json (Only support json))
Default Language [zh|en] en:
Saving profile[akProfile] ...Done.

出现如下界面,配置完成
请添加图片描述

安装 certbot-dns-aliyun 插件

wget http://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh
sudo cp alidns.sh /usr/local/bin
sudo chmod +x /usr/local/bin/alidns.sh
sudo ln -s /usr/local/bin/alidns.sh /usr/local/bin/alidns
rm alidns.sh

测试是否能正确申请

certbot certonly -d *.替换自己的域名.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

正式申请时去掉 --dry-run 参数:

certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"

证书续期

certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

自动续期,添加定时任务 crontab。

crontab -e
1 1 */1 * * root certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值