使用Let's Encrypt申请永久免费通配符证书

由于现在网络安全形势的影响,大多数网站都启用了https。本文介绍一种申请永久免费的通配符证书的方法,可以适配一个域名下的所有站点(例如:www.abc.com和子域名ss.abc.com都可以用一个证书启用https)。
我所使用的申请证书的环境是Centos7,因为这个是官方提供的是一个python的程序,有一些依赖的库,要pip下载,因为有些链接容易超时,还是建议把pip的源改到中国来

申请方式
首先,下载程序,在shell下面执行:

wget https://dl.eff.org/certbot-auto

接着进行证书申请(我们这里假设申请*.abc.com这个域名的证书)

./certbot-auto certonly  -d *.abc.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

可能会安装一些依赖的库,出现一些选项都选择yes,然后会配置一条DNS的TXT记录,验证域名的所有权,过后就会成功申请证书。
证书会生成在这个目录下/etc/letsencrypt/archive/,总共有4个文件,如下:
cert1.pem 公钥
chain1.pem 证书
fullchain1.pem 公钥加证书
privkey1.pem 私钥

其他
如果windows要使用的话,需要把私钥转化成后缀为pfx格式的,命令如下(生成的pfx为test.pfx):

openssl pkcs12 -export -out test.pfx -inkey privkey.pem -in cert.pem 

如果是tomcat要使用的话,需要将刚刚的pfx再进行转化,需要注意下面的命令设置的密码,比如和pfx的密码相同,命令如下(生成server.jks):

keytool -importkeystore -srckeystore test.pfx -destkeystore server.jks -srcstoretype PKCS12 -deststoretype JKS
keytool -importkeystore -srckeystore server.jks -destkeystore server.jks -deststoretype pkcs12

ps:注意这里申请的证书有效期是3个月,到期后需要再进行上述操作。
pps:今天在续期的时候,发现官方给出了,运行certbot-auto renew可以续期证书,大家可以试试

自动续期证书
续期证书远没有上面说的那么简单,因为续期证书的时候会检查域名的所有权(就是上面说的在DNS配置txt记录),官方提供的办法是将DNS的TXT记录插入进去的脚本,然后再验证你的域名所有权。
使用的命令是

certbot certonly --manual --manual-auth-hook /path/to/http/authenticator.sh --manual-cleanup-hook /path/to/http/cleanup.sh -d secure.example.com

前面那个文件authenticator.sh是在你的域名的DNS中插入TXT,后面是清理之前的TXT记录。官方给出的是cloudflare的实例,可以更改成阿里云等等的。
上面提到的两个文件源码如下
authenticator.sh:

#!/bin/bash

# Get your API key from https://www.cloudflare.com/a/account/my-account
API_KEY="your-api-key"
EMAIL="your.email@example.com"

# Strip only the top domain to get the zone id
DOMAIN=$(expr match "$CERTBOT_DOMAIN" '.*\.\(.*\..*\)')

# Get the Cloudflare zone id
ZONE_EXTRA_PARAMS="status=active&page=1&per_page=20&order=status&direction=desc&match=all"
ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN&$ZONE_EXTRA_PARAMS" \
     -H     "X-Auth-Email: $EMAIL" \
     -H     "X-Auth-Key: $API_KEY" \
     -H     "Content-Type: application/json" | python -c "import sys,json;print(json.load(sys.stdin)['result'][0]['id'])")

# Create TXT record
CREATE_DOMAIN="_acme-challenge.$CERTBOT_DOMAIN"
RECORD_ID=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
     -H     "X-Auth-Email: $EMAIL" \
     -H     "X-Auth-Key: $API_KEY" \
     -H     "Content-Type: application/json" \
     --data '{"type":"TXT","name":"'"$CREATE_DOMAIN"'","content":"'"$CERTBOT_VALIDATION"'","ttl":120}' \
             | python -c "import sys,json;print(json.load(sys.stdin)['result']['id'])")
# Save info for cleanup
if [ ! -d /tmp/CERTBOT_$CERTBOT_DOMAIN ];then
        mkdir -m 0700 /tmp/CERTBOT_$CERTBOT_DOMAIN
fi
echo $ZONE_ID > /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID
echo $RECORD_ID > /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID

# Sleep to make sure the change has time to propagate over to DNS
sleep 25

cleanup.sh

#!/bin/bash

# Get your API key from https://www.cloudflare.com/a/account/my-account
API_KEY="your-api-key"
EMAIL="your.email@example.com"

if [ -f /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID ]; then
        ZONE_ID=$(cat /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID)
        rm -f /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID
fi

if [ -f /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID ]; then
        RECORD_ID=$(cat /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID)
        rm -f /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID
fi

# Remove the challenge TXT record from the zone
if [ -n "${ZONE_ID}" ]; then
    if [ -n "${RECORD_ID}" ]; then
        curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
                -H "X-Auth-Email: $EMAIL" \
                -H "X-Auth-Key: $API_KEY" \
                -H "Content-Type: application/json"
    fi
fi

目前我还没有测试自动续期,先贴出官方的代码。
具体可以参考官方文档:https://certbot.eff.org/docs/using.html#pre-and-post-validation-hooks
如果不想这么麻烦使用自动续期,那么就重复上面的操作就可以了

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值