-
简要:
最近为自己网站挂载了 阿里云网盘来做视频床及图床 ,所以需要对原有域名进行重新配置设置。研究学习了网上资料后实现了该功能,现做简单总结。
1. certbot 及插件安装(官方没有阿里云插件,需自己安装)
- 环境安装
# 安装或更新python3
yum install -y python36
# 创建运行环境
mkdir -p /opt/certbot
cd /opt/certbot
python3 -m venv venv
source venv/bin/activate
- 工具安装
# 更新pip
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装(在venv中执行,使用国内源)
pip install certbot certbot-nginx certbot-dns-aliyun -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 阿里云AccessKey申请
-
-
添加子用户
-
添加Dnsfullaccess权限
- Accesskey创建成功后,点击下载key文件,后面需要用到这个key和secret.
3. 生成证书及密钥
- 创建xxx.ini文件(自定义文件名),写入key和secret参数
cat > xxx.ini <<EOF
certbot_dns_aliyun:dns_aliyun_access_key = 这里是keyID字符串
certbot_dns_aliyun:dns_aliyun_access_key_secret = 这里是secret字符串
EOF
# 设置文件权限只有读写权限
chmod 600 xxx.ini
- 使用python调用配置文件生成证书(生成时主域名必须先列出,然后才是泛域名,所以有两个-d)
/opt/certbot/venv/bin/certbot certonly \
-a certbot-dns-aliyun:dns-aliyun \
--certbot-dns-aliyun:dns-aliyun-credentials /opt/certbot/xxx.ini \
-d 你的域名.com \
-d "*.你的域名.com"
- 返回生成的证书及密钥地址
注意
在根据以上教程后发现无法创建证书,有如下报错:
后来发现使用子账号的AccessKey无法使用,但换成主账户AccessKey即可!
4. nginx配置
- 配置
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; # 证书地址
ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; # 密钥地址
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
- 重启nginx
# 验证配置
nginx -t
# 重载服务
systemctl reload nginx
5. 设置定时器,定时更新证书
- 通过crontab来实现的,生成的证书只有90天时效,设置每60天重新renew续期一次
crontab -e
# 启动crontab编辑保存
30 0 1 */2 * root /opt/certbot/venv/bin/certbot renew --quiet --post-hook "systemctl reload nginx" > /dev/null 2>&1
评论区