Skip to content

SSL证书自动续期

方式一. Certd

一款自动化续期SSL证书的工具;

文档https://certd.docmirror.cn/guide/install/baota/

1. Docker 安装

bash
version: '3.3' # 兼容旧版docker-compose
services:
  certd:
    # 镜像                                                  #  ↓↓↓↓↓ ---- 镜像版本号,建议改成固定版本号,例如:certd:1.29.0
    image: registry.cn-shenzhen.aliyuncs.com/handsfree/certd:latest
    container_name: certd # 容器名
    restart: unless-stopped # 自动重启
    volumes:
      #   ↓↓↓↓↓ -------------------------------------------------------- 数据库以及证书存储路径,默认存在宿主机的/data/certd/目录下,【您需要定时备份此目录,以保障数据容灾】
      - /data/certd:/app/data
    ports: # 端口映射
      #  ↓↓↓↓ ---------------------------------------------------------- 如果端口有冲突,可以修改第一个7001为其他不冲突的端口号
      - "7001:7001"
      #  ↓↓↓↓ ---------------------------------------------------------- https端口,可以根据实际情况,是否暴露该端口
      - "7002:7002"
    #↓↓↓↓ -------------------------------------------------------------- 如果出现getaddrinfo ENOTFOUND错误,可以尝试设置dns
#    dns:
#      - 223.5.5.5      # 阿里云公共dns
#      - 223.6.6.6
#       # ↓↓↓↓ --------------------------------------------------------- 如果你服务器在腾讯云,可以用这个替换上面阿里云的公共dns
#      - 119.29.29.29  # 腾讯云公共dns
#      - 182.254.116.116
#       # ↓↓↓↓ --------------------------------------------------------- 如果你服务器部署在国外,可以用这个替换上面阿里云的公共dns
#      - 8.8.8.8       # 谷歌公共dns
#      - 8.8.4.4
#    extra_hosts:
#        # ↓↓↓↓ -------------------------------------------------------- 这里可以配置自定义hosts,外网域名可以指向本地局域网ip地址
#      - "localdomain.com:192.168.1.3"
    labels:
      com.centurylinklabs.watchtower.enable: "true"
#    ↓↓↓↓ -------------------------------------------------------------- 启用ipv6网络,还需要把下面networks的注释放开
#    networks:
#      - ip6net
    environment:
#     设置环境变量即可自定义certd配置
#     配置项见: packages/ui/certd-server/src/config/config.default.ts
#     配置规则: certd_ + 配置项, 点号用_代替
#                                    #↓↓↓↓ ----------------------------- 如果忘记管理员密码,可以设置为true,重启之后,管理员密码将改成123456,然后请及时修改回false
      - certd_system_resetAdminPasswd=false

#     默认使用sqlite文件数据库,如果需要使用其他数据库,请设置以下环境变量
#     注意: 选定使用一种数据库之后,不支持更换数据库。
#     数据库迁移方法:1、使用新数据库重新部署一套,然后将旧数据同步过去,注意flyway_history表的数据不要同步
#                                    #↓↓↓↓ ----------------------------- 使用postgresql数据库,需要提前创建数据库
#      - certd_flyway_scriptDir=./db/migration-pg                        # 升级脚本目录
#      - certd_typeorm_dataSource_default_type=postgres                  # 数据库类型
#      - certd_typeorm_dataSource_default_host=localhost                 # 数据库地址
#      - certd_typeorm_dataSource_default_port=5433                      # 数据库端口
#      - certd_typeorm_dataSource_default_username=postgres              # 用户名
#      - certd_typeorm_dataSource_default_password=yourpasswd            # 密码
#      - certd_typeorm_dataSource_default_database=certd                 # 数据库名

#                                    #↓↓↓↓ ----------------------------- 使用mysql数据库,需要提前创建数据库 charset=utf8mb4, collation=utf8mb4_bin
#      - certd_flyway_scriptDir=./db/migration-mysql                     # 升级脚本目录
#      - certd_typeorm_dataSource_default_type=mysql                     # 数据库类型, 或者 mariadb
#      - certd_typeorm_dataSource_default_host=localhost                 # 数据库地址
#      - certd_typeorm_dataSource_default_port=3306                      # 数据库端口
#      - certd_typeorm_dataSource_default_username=root                  # 用户名
#      - certd_typeorm_dataSource_default_password=yourpasswd            # 密码
#      - certd_typeorm_dataSource_default_database=certd                 # 数据库名

#         ↓↓↓↓ ---------------------------------------------------------  自动升级,上面certd的版本号要保持为latest
#  certd-updater:  # 添加 Watchtower 服务
#    image: containrrr/watchtower:latest
#    container_name: certd-updater
#    restart: unless-stopped
#    volumes:
#      - /var/run/docker.sock:/var/run/docker.sock
#    # 配置 自动更新
#    environment:
#      - WATCHTOWER_CLEANUP=true            # 自动清理旧版本容器
#      - WATCHTOWER_INCLUDE_STOPPED=false   # 不更新已停止的容器
#      - WATCHTOWER_LABEL_ENABLE=true       # 根据容器标签进行更新
#      - WATCHTOWER_POLL_INTERVAL=600       # 每 10 分钟检查一次更新


#    ↓↓↓↓ -------------------------------------------------------------- 启用ipv6网络,还需要把上面networks的注释放开
#networks:
#  ip6net:
#    enable_ipv6: true
#    ipam:
#      config:
#        - subnet: 2001:db8::/64

访问应用

  1. http://you.ip:7001
  2. https://you.ip:7002
  3. 默认账号密码 admin/123456

登录后请及时修改密码

方式二. certbot 工具

文档网址

自动申请SSL证书工具,certbot安装

  1. 安装必要工具
shell
sudo yum install snapd
  1. 删除 certbot-auto 和任何 Certbot OS 软件包
shell
dnf remove certbotsudo yum remove certbot
  1. 安装 Certbot
shell
sudo snap install --classic certbot
  1. 准备 Certbot 命令,在计算机上的命令行上执行以下指令,以确保可以运行该命令 certbot
shell
sudo ln -s /snap/bin/certbot /usr/bin/certbot

自动申请SSL证书

方式一:自动获取证书,并修改nginx配置

shell
sudo certbot --nginx

方式二:只获取证书而不去修改nginx文件(当然这要自己手动去修改配置)

shell
sudo certbot certonly --nginx

测试自动续订

shell
sudo certbot renew --dry-run

创建自动任务

shell
crontab -e

文件内容如下:(解读下面的任务:每天凌晨两点, 获取ssl证书,获取完证书后重启nginx)

shell
0 2 * * * sudo certbot renew --deploy-hook  "nginx -s reopen"

个人收集整理, MIT License