使用阿里云Certbot-DNS-Aliyun插件自动获取并更新免费SSL泛域名(通配符)证书

进入nginx docker,一般是Alpine Linux系统

  1. 依次执行命令:
bash 复制代码
sudo docker-compose exec nginx bash

apk update

apk add certbot 

apk add --no-cache python3 python3-dev build-base

apk add python3 py3-pip

pip3 install --upgrade pip

pip3 install certbot-dns-aliyun -i https://mirrors.aliyun.com/pypi/simple/ --default-timeout=100 --no-cache-dir

最后一步一直报错,把pip3版本改成21.3.1, python版本改为3.6成功了。

bash 复制代码
pip3 install --upgrade pip==21.3.1

pip3 --version
pip 21.3.1 from /usr/lib/python3.6/site-packages/pip (python 3.6)

2.创建阿里云RAM 用户账号,赋予AliyunDNSFullAccess权限,阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

sudo vi /etc/letsencrypt/aliyun.ini

在文件中添加以下内容:

certbot_dns_aliyun:dns_aliyun_access_key = your-access-key

certbot_dns_aliyun:dns_aliyun_access_key_secret = your-access-key-secret

3.生成证书

bash 复制代码
certbot certonly -a certbot-dns-aliyun:dns-aliyun --certbot-dns-aliyun:dns-aliyun-credentials /etc/letsencrypt/aliyun.ini  -d '*.xxx.com,xxx.com' --email xxx@163.com --config-dir /etc/nginx/ssl

--config-dir参数为证书存放路径,可以去掉,默认是/etc/letsencrypt/live/

bash 复制代码
vi /usr/local/bin/ssl_renew_and_reload.sh

在文件中添加以下内容:

复制代码
#!/bin/sh

certbot renew \
  -a certbot-dns-aliyun:dns-aliyun \
  --certbot-dns-aliyun:dns-aliyun-credentials /etc/letsencrypt/aliyun.ini \
  --email xxx@163.com \
  --config-dir /etc/nginx/ssl \
  --certbot-dns-aliyun:dns-aliyun-propagation-seconds 60 

nginx -s reload

设置权限:

bash 复制代码
chmod +x /usr/local/bin/ssl_renew_and_reload.sh

查看证书:

bash 复制代码
certbot certificates

4.定时任务

bash 复制代码
crontab -e

0 0 * * * /usr/local/bin/ssl_renew_and_reload.sh >> /var/log/letsencrypt/renew.log 2>&1

注意:

使用生成证书的命令是有接口限制的,因此不建议频繁调用,如果报错了先排查出问题再继续,可以在末尾加上--dry-run用于获取测试证书排查问题,没有报错后再去掉它获取正式证书

certbot certonly --webroot -w /home/wwwroot/xxx -d xxx.com --dry-run

sudo certbot renew --dry-run

查看生成的文件路径和参数:cat /etc/letsencrypt/renewal/域名.conf

运行:/usr/local/bin/ssl_renew_and_reload.sh

检查 /var/log/letsencrypt/letsencrypt.log 有没有错误

相关推荐
TG_yunshuguoji12 小时前
亚马逊云代理:亚马逊云怎么样进行大规模数据分析与处理?
数据挖掘·数据分析·云计算·aws
影子240115 小时前
java jdbc连接sqlserver2008R2版本数据库报错,驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
java·数据库·ssl
武汉唯众智创17 小时前
云数据中心网络优化实训系统:构建新一代云计算人才实训平台
网络·云计算·php·数字孪生·云数据中心网络优化实训系统·云计算孪生
Clownseven18 小时前
阿里云ECS vs 腾讯云CVM:2核4G服务器性能实测对比 (2025)
服务器·阿里云·腾讯云
TG_yunshuguoji18 小时前
阿里云国际代理:如何利用RDS构建高可用、可扩展的数据库架构
服务器·数据库·阿里云·云计算·数据库架构
阿里云云原生20 小时前
阿里云可观测 2025 年 8 月产品动态
阿里云·云计算
Clownseven20 小时前
阿里云ECS快照教程:如何设置自动备份与一键恢复数据
阿里云·云计算
爱敲代码的TOM20 小时前
阿里云监控使用
阿里云·云计算
荣光波比20 小时前
MySQL数据库(一)—— 数据库基础与MySQL安装管理指南
运维·数据库·mysql·云计算
土星碎冰机1 天前
数据库开启ssl
数据库·网络协议·ssl