官网 https://certbot.eff.org/ 官网上有相关教程, 但是都是英文.....今天做个记录吧, 方便自己以后查看.
我的服务器环境是 Centos7 + Nginx , DNS 服务商是 Cloudflare . 如果你的情况和我不一样, 请去官网看教程.
一. 前提准备
要获取 Certbot 的泛域名证书, 只能用 DNS 验证的方法. 简单来说就是给域名添加一条 txt 记录, 但这个证书有效期是 90 天, 所以我们需要自动化续期, 自动添加域名解析.
目前官网上支持下图中的 DNS 服务商自动更新, 地址 https://certbot.eff.org/docs/using.html#manual
二. 安装必要的软件
1. 启用 EPEL 储存库
- yum install -y epel-release
- yum clean all
- yum makecache
复制代码
2. 启用可选通道
- yum -y install yum-utils
- yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
复制代码
3. 安装 Certbot- yum -y install certbot python2-certbot-nginx
复制代码
4. 安装 DNS 插件 (命令后面的 cloudflare 换成你的 DNS 服务商)
- yum install python2-certbot-dns-cloudflare
复制代码
三. 配置 DNS 插件
从 cloudflare 账户 获取 api key , 然后写入一个文件, 如 cf.ini
- # Cloudflare API credentials used by Certbot
- dns_cloudflare_email = [email protected]
- dns_cloudflare_api_key = 0123456789abcdef0123456789abcdef01234567
复制代码
/etc/nginx/ssl/cf.ini //是刚才的配置文件地址.
--dns-cloudflare-propagation-seconds 60 // 等待 60 秒, 等 DNS 解析生效.
配置好之后, 就可以申请泛域名证书了!
运行命令:
- certbot certonly \
- --dns-cloudflare \
- --dns-cloudflare-credentials /etc/nginx/ssl/cf.ini \
- --dns-cloudflare-propagation-seconds 60 \
- -d *.guhei.net
复制代码
成功获取了泛域名证书
四. 自动续订
自动续订也很简单, 运行
最后就是 nginx 重新载入配置文件
创建日志文件
- touch /var/log/certbot.log
复制代码
添加到定时任务, 每月 1号 1时 1分 , 执行一次自动续订的命令
每月 1 号 1时 3分 , nginx 重新载入配置文件
- 1 1 1 * * /usr/bin/certbot renew > /var/log/certbot.log 2>&1
- 3 1 1 * * /usr/sbin/nginx -s reload > /var/log/certbot.log 2>&1
复制代码
|