SSL 证书申请
这篇文章是给个人博客的域名申请 SSL 证书,从而实现个人博客的 https 访问。由于在阿里、腾讯等平台申请长期的 SSL 证书较贵,这里选择免费的 Let’s Encrypt 颁发的证书,并使用 Certbot 来实现自动化整个 SSL 证书的管理流程。
申请单域名证书
单域名证书指为单个子域名颁发的 SSL 证书,例如 abc.example.com 为单域名,def.example.com 为另外一个单域名,需要分别对这两个单域名颁发对应的证书
给域名添加 ssl 证书
Certbot Instructions | Certbot追加域名 ssl 证书
User Guide — Certbot 3.1.0.dev0 documentation
Let’s Encrypt 证书的一些操作(Certbot) | 菜包子
Certbot 为新子域名添加证书1
2
3
4
5# 追加子域名的 SSL 证书命令
$ sudo certbot -d xxx.xxx.com --expand
# 在已有域名 example.com 和 www.example.com 基础上接着添加一个域名 abc.example.com
$ sudo certbot certonly --cert-name example.com -d example.com -d www.example.com -d abc.example.comcertbot 删除已有 ssl 证书
let’s encrypt 如何用 certbot 删除一个证书1
$ sudo certbot delete
申请泛域名证书
范域名证书指为多个子域名颁发的整数,写作、*.example.com,包含 abc.example.com、def.example.com 等子域名,只需要颁发一个证书,该二级域名的子域名就都能使用
参考:使用 Let’s Encrypt 免费申请泛域名 SSL 证书,并实现自动续期
- 笔者在阿里云购买的域名并完成 DNS 解析,因此这里通过阿里云 DNS 来给泛域名申请 SSL 证书,相关脚本及使用教程见 GitHub - justjavac/certbot-dns-aliyun: 阿里云 DNS 的 certbot 插件,用来解决阿里云 DNS 不能自动为通配符证书续期的问题
- 这里将该脚本的使用步骤及注意事项列出
安装
aliyun cli
工具1
2
3
4wget 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安装
certbot-dns-aliyun
插件1
2
3
4
5wget https://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申请证书
- 测试是否能正确申请:
1
$ certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
- 正式申请时去掉
--dry-run
参数:1
$ certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
- 测试是否能正确申请:
一般情况下,证书会自动续期,可通过以下命令测试是否正常实现自动续期,否则需要添加定时任务每个三个月执行一次
1
$ certbot renew --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
实现证书的自动续期
1
$ 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"
,这里的--deploy-hook "nginx -s reload"
表示在续期成功后自动重启nginx