目录
功能需求
阿里云CentOS7, 多子域名项目升级https, 申请证书以及Nginx配置.
注意
下文使用的所有命令, 如域名, 邮箱, 脚本目录等, 粘贴后请修改为自己需要的值
Nginx
对于使用有限数量子域名的情况,可直接在 digitalocean 中部分域名的方式生成配置文件, Cerbot配置可以完全适用
digitalocean-tools-nginx
1. OpenResty
参考官方文档完成安装
默认位置
/usr/local/openresty
启动和设置开机自动启动
systemctl start openresty && systemctl enable openresty
#基本使用
openresty -t
openresty -s [stop, quit, reopen, reload]
Certbot
主要参考文档的cerbot-renewal部分, 由于证书90天自动过期, 需检查或配置自动续期策略
1. 安装
certbot-auto 也基本一样用包管理器傻瓜式安装即可
#本次实例未使用certbot-auto 以及 snap的方式下载
yum -y install epel-release
yum -y install certbot
建议看看certbot -h, 没几个选项, 解释也很清楚
2. 域名配置
创建一个通用的ACME-challenge目录(用于 Let’s Encrypt):
- 当使用–webroot方式时,需要在server块中优先增加一个路由
- 这段配置如果从 digitaloeacn下载的配置文件包里有
# 当使用--webroot方式时,需要在server块中优先增加一个路由
# ACME-challenge
location ^~ /.well-known/acme-challenge/ {
root /var/www/_letsencrypt;
}
2.1 单一域名
certbot certonly --webroot -d *.domain.com --email xx@xx.com -w /var/www/_letsencrypt -n --agree-tos --force-renewal
主要参数:
- –webroot 方式只适用于指定域名的情况, 泛解析申请的证书浏览器报不安全异常
- –manual 手动方式, 适用于泛解析的情况, 例如 a.xx.com b.xx.com…等更多可能的域名
参数说明:
- -w 配合 --webroot出现, 定义certbot生成验证文件的目录
2.2 泛解析
2.2.1 人工配置 DNS TXT方式
人工配置方式无法完成自动续期, 因为续期时缺少人工环节
使用--manual
方式申请证书, 指定challenges
为dns-01
方式, 如果使用 --webhook
方式申请证书, chrome等浏览器会报请求不安全, 但可以在浏览器页面中输入thisisunsafe
加载页面, 十分不推荐.
- 申请证书
请求响应, 需要先配置 DNS TXT, 具体为一个certbot certonly --manual -d *.domain.com --email xx@xx.com --preferred-challenges dns-01 --force-renewal --server https://acme-v02.api.letsencrypt.org/directory
_acme-challenge.domain.com
的配置中填入以下内容xxxxxxxxxxxxx
Please deploy a DNS TXT record under the name -acme-challenge.domain.com with the following value: xxxxxxxxxxxxx Before continuing, verify the record is deployed.
- 配置 DNS TXT
上述命令返回提示后, 在服务器管理后台配置DNS TXT
, 每个云平台可能不同, 这里不赘述, 待人工配置完成后回车下一步 ,切勿在未配置相应的DNS TXT
的情况下做下一步 - 检查 DNS TXT
新开一个session
正确返回包含一下内容dig -t txt _acme-challenge.domain.com @8.8.8.8
配置成功后回车, 几秒后会提示证书的位置#返回中包含对应配置部分则说明配置生效,下文600为配置的ttl,视情况而定 ;; ANSWER SECTION: _acme-challenge.domain.com. 600 IN TXT "xxxxxxxxxxxx"
/etc/letsencrypt/live/domain.com/xxxx.pem
- 删除证书
配置失败时可以删除certbot delete --cert-name domain.com
2.2.2 自动化脚本方式
开源脚本主要用于证书的申请和续期, 自动续期通过crontab
完成, 其中主要逻辑就是调用云厂商的接口自动设置DNS TXT
, 更新为先删除后添加的逻辑.
-
前置条件
服务器php或python环境用于执行脚本, 本次因服务器有php项目, 直接使用php的脚本 -
脚本仓库
一定看readmegit clone https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au cd certbot-letencrypt-wildcardcertificates-alydns-au && chmod 0777 au.sh
-
修改配置
vim au.sh #填写服务器 accesskey 和 sercet
添加对应服务器厂商的信息
vim au.sh #填写阿里云的AccessKey ID及AccessKey Secret #如何申请见https://help.aliyun.com/knowledge_detail/38738.html ALY_KEY="" ALY_TOKEN="" #填写腾讯云的SecretId及SecretKey #如何申请见https://console.cloud.tencent.com/cam/capi TXY_KEY="" TXY_TOKEN="" #填写华为云的 Access Key Id 及 Secret Access Key #如何申请见https://support.huaweicloud.com/devg-apisign/api-sign-provide.html HWY_KEY="" HWY_TOKEN="" #PHP 命令行路径,如果有需要可以修改 phpcmd="/usr/bin/php" #Python 命令行路径,如果有需要可以修改 pythoncmd="/usr/bin/python"
-
测试调用
保存后测试, 服务器有php环境用php, 有python环境用python, 替换命令中关键字即可au.sh
脚本的三个参数:- 执行脚本的环境:
php
/python
, - 云服务器厂商:
aly
/hwy
/txy
…等, --manual-auth-hook
固定值add
,--manual-cleanup-hook
固定值clean
--dry-run
用于测试certbot certonly -d *.xxxxx.com --manual --preferred-challenges dns --dry-run --manual-auth-hook "/脚本目录/au.sh php aly add" --manual-cleanup-hook "/脚本目录/au.sh php aly clean"
- 执行脚本的环境:
-
实际申请
#测试无误,去除--dry-run再次执行,获取证书 certbot certonly -d *.xxxxx.com --manual --preferred-challenges dns --manual-auth-hook "/脚本目录/au.sh php aly add" --manual-cleanup-hook "/脚本目录/au.sh php aly clean"
3. 证书续期
对于单一域名的证书而言, 使用 certbot renew
命令就可以自动完成续期, 但对于泛解析域名, 根据上文中的申请方式不同, 会导致人工方式无法自动完成续期操作, 虽然certbot提供了脚本来调用服务商接口更新DNS TXT
信息, 但官方的脚本都是针对国外服务器厂商的.
国内服务器使用 自动续期脚本, 目前支持阿里云, 腾讯云, 华为云, godaddy等
3.1 单一域名
certbot 续期命令可能已经自动添加到系统的定时任务里了, 可以检查以下位置
crontab -l
/etc/cron.*/*
systemctl list-timers
本例中未自动添加任务
--renew-hook 执行脚本
, 至于执行什么完全自定义
certbot renew --force-renewal --renew-hook "openresty -s reload" > /xxxxxx/ssl-renew/ssl-update.log 2>&1 &
#自行添加任务, 不赘述
crontab -e
3.2 泛解析域名
脚本配置参考 2.2.2 自动化脚本方式
- 测试
如果使用python环境,则把 php 替换为 python 即可./certbot renew --dry-run --manual --preferred-challenges dns --manual-auth-hook "/脚本全路径/au.sh php aly add" --manual-cleanup-hook "/脚本全路径/au.sh php aly clean" #最后将输出 Congratulations, all simulated renewals succeeded: /etc/letsencrypt/live/xxxxxx/fullchain.pem (success) #也就测试续期成功了
- 服务器上申请的全部证书续期
certbot renew --manual --preferred-challenges dns --manual-auth-hook "/脚本全路径/au.sh php aly add" --manual-cleanup-hook "/脚本全路径/au.sh php aly clean"
- 服务器上申请的指定证书续期
查看所有证书
续期certbot certificates # 输出 #Found the following certs: #Certificate Name: 证书名称 #Serial Number: xxxxxxx #Key Type: RSA #Domains: *.xxxx.com #Expiry Date: 2022-09-29 01:23:03+00:00 (VALID: 89 days) #Certificate Path: /etc/letsencrypt/live/xxxx/fullchain.pem #Private Key Path: /etc/letsencrypt/live/xxxx/privkey.pem
certbot renew --cert-name [证书名称] --manual-auth-hook "/脚本全路径/au.sh php aly add" --manual-cleanup-hook "/脚本全路径/au.sh php aly clean"
- 自动触发
个人建议把续期的命令写在一个脚本里, 在用crontab一个表达式调用#renew.sh里就是上面的certbot renew那些东西了 crontab -e 0 0 1,15 * * root /xxxx/ssl-renew/renew.sh > /dev/null 2>&1 &
Nginx配置
可以完全复制于 digitalocean.com 中的全局以及域名server块中的配置,完全适用
1. 全局
#http全局模块
http {
....
# SSL
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# Diffie-Hellman parameter for DHE ciphersuites
ssl_dhparam /etc/nginx/dhparam.pem;
# Mozilla Intermediate configuration
ssl_protocols TLSv1.2 TLSv1.3;
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;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
resolver_timeout 2s;
....
}
2. 对应Server块
#
server {
# SSL
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/domain.com/chain.pem;
}
重新加载nginx配置, 本例是openresty
openresty -t && openresty -s reload