泛域名证书申请

泛域名证书申请 (Ubuntu 20)

全网 HTTPS 时代就要到来,Let’s Encrypt 三个月有效期的免费 HTTPS
证书现在支持泛域名了,我们可以通过 Certbot 非常方便的申请和更新证书。网上很多关于 Certbot
的文章,但是关于泛域名证书的自动更新很少提及,或者很多误区,这里简单的讲解一下。

一丶 Certbot 安装

安装非常简单,只要进入 Certbot 官网选择对应的系统和 Web 服务软件就会提示如何安装,按照提示操作就可以了。

1
2
3
4
5
6
apt-get update
apt-get install software-properties-common
add-apt-repository universe
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install python3-certbot-nginx

二丶 申请泛域名证书

这里用到了一个开源工具:certbot-letencrypt-wildcardcertificates-alydns-au
工具就不介绍了,点开链接作者写的很详细了,看看怎么用吧

1. 下载

1
2
3
4
5
6
cd ~/code
git clone https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au
mv certbot-letencrypt-wildcardcertificates-alydns-au certbot
cd certbot
chmod 777 au.sh

2. 配置

(1) domain.ini

如果 domain.ini 文件没有你的根域名,请自行添加

(2) DNS API 密钥

ALY_KEYALY_TOKEN:阿里云 API key 和 Secrec 官方申请文档
屏幕截图 2023-03-30 235738

(3) 修改 au.sh 的 python 版本

image

(4) 选择运行环境

Python(支持 2.7 和 3.7,无需任何第三方库) Python 操作阿里云 DNS,增加/清空 DNS。
au.sh python aly add/clean

(5) 注释掉报错代码

image

3. 申请证书

测试是否有错误:

1
certbot certonly  -d *.example.com --manual --preferred-challenges dns --dry-run  --manual-auth-hook "/root/code/certbot/au.sh python aly add" --manual-cleanup-hook "/root/code/certbot/au.sh python aly clean"

实际申请:

1
certbot certonly  -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "/root/code/certbot/au.sh python aly add" --manual-cleanup-hook "/root/code/certbot/au.sh python aly clean"

4. 续期证书

对机器上所有证书 renew

1
certbot renew  --manual --preferred-challenges dns --manual-auth-hook "/root/code/certbot/au.sh python aly add" --manual-cleanup-hook "/root/code/certbot/au.sh python aly clean"

对某一张证书进行续期

1
2
3
4
#先看看机器上有多少证书:
certbot certificates
#记住证书名,比如 simplehttps.com,然后运行下列命令 renew
certbot renew --cert-name simplehttps.com --manual-auth-hook "/root/code/certbot/au.sh python aly add" --manual-cleanup-hook "/root/code/certbot/au.sh python aly clean"

三丶 加入 crontab

编写证书更新脚本

1
2
3
4
5
6
7
8
9
#!/bin/bash
docker stop nginx
certbot renew --cert-name example.com --manual-auth-hook "/root/code/certbot/au.sh python aly add" --manual-cleanup-hook "/root/code/certbot/au.sh python aly clean"
cp /etc/letsencrypt/live/example.com/fullchain.pem /opt/docker/nginx/cert
cp /etc/letsencrypt/live/example.com/privkey.pem /opt/docker/nginx/cert
echo "Certificates Renewed"
chmod 777 /opt/docker/nginx/cert/*.pem
echo "Read Permission Granted for Private Key"
docker start nginx

添加执行权限 chmod +x /opt/docker/nginx/cert/renew.sh

定时任务 crontab -e:

1
2
#证书有效期<30天才会renew
0 4 1 * * /bin/bash /opt/docker/nginx/cert/renew.sh

泛域名证书申请
https://blog.jaychou.site/2023/12/02/泛域名证书申请/
作者
Nichuanfang
发布于
2023年12月2日
更新于
2023年12月27日
许可协议