menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

使用acme.sh自动续签https证书

作者:Chancel Yang, 创建:2024-03-29, 字数:3271, 已阅:619, 最后更新:2024-06-25

Acme.sh 是一个开源的自动化证书管理工具,用于获取、安装、更新和部署SSL/TLS证书,使用Shell脚本编写并支持在Linux、macOS、FreeBSD和Windows等操作系统上运行

Acme.sh 基于 ACME 协议(Automatic Certificate Management Environment)工作,该协议由Let's Encrypt提出并广泛采用

ACME 协议允许用户通过自动化的方式获取和管理SSL/TLS证书,而不需要手动进行复杂的证书请求、验证和安装过程

Acme.sh 提供了一系列命令和选项,可以与各种证书颁发机构(包括Let's Encrypt)进行交互,并自动处理证书申请、域名验证和证书安装

此外还支持各种验证方法,包括HTTP验证、DNS验证和TLS-SNI验证,以满足不同环境和需求的证书获取和更新

使用可以轻松地配置和管理SSL/TLS证书,从而网站、应用程序或其他服务启用安全的HTTPS连接

1. 使用指南

1.1. 安装

Acme.sh 安装非常方便,使用官方提供的脚本即可:

Bash
curl https://get.acme.sh | sh

执行安装后,Acme.sh 会安装到目录 $HOME/.acme.sh 中,切换到该目录,执行注册用户:

Bash
cd $HOME/.acme.sh
acme.sh --register-account -m your_email@email.com

注册成功后,查看一下定时任务,Acme.sh 默认配置每天自动检查证书(有效期大于30天)并自动续签

Bash
sudo cat /var/spool/cron/crontabs/$USER

因为 Let's Encrypt 的协议会更新,所以要设置允许 Acme.sh 自动升级

Bash
acme.sh  --upgrade  --auto-upgrade

1.2. 部署

设置环境变量 Namesilo_Key 用于访问 Namesilo 的API:

Bash
# 其他域名服务商参数名称参考:https://github.com/acmesh-official/acme.sh/wiki/dnsapi
export Namesilo_Key="域名服务商API-KEY"

检查输出中没有错误后,认证通配符域名(Wildcard Certificate):

Bash
# 同样的,dns参数值参考:https://github.com/acmesh-official/acme.sh/wiki/dnsapi
acme.sh --issue --dns dns_namesilo -d chancel.me -d "*.chancel.me"

必须填 *.chancel.me 和 chancel.me ,因为 *.chancel.me 不包含 chancel.me 的域名

确认没有错误输出,查看证书列表

Bash
$ acme.sh --list                                                                                                          
Main_Domain  KeyLength  SAN_Domains   CA           Created               Renew
chancel.me   "ec-256"   *.chancel.me  ZeroSSL.com  2024-06-26T01:43:28Z  2024-08-24T01:43:28Z

证书位置位于 $HOME/.acme.sh/chancel.me_ecc/ ,如下:

TEXT
.
├── ...
├── chancel.me_ecc
│   ├── ca.cer
│   ├── chancel.me.cer
│   ├── chancel.me.conf
│   ├── chancel.me.csr
│   ├── chancel.me.csr.conf
│   ├── chancel.me.key
│   └── fullchain.cer
└── ...

2. Docker-Compose

Acme.sh 也支持容器部署,编辑一个 docker-compose.yaml 文件:

YAML
version: '3'

services:
  acme.sh:
    image: neilpang/acme.sh
    volumes:
      - ./acmesh:/acme.sh
    environment:
      - Namesilo_Key=[域名服务商API-KEY]
    command: '--issue --dns dns_namesilo -d "chancel.me" -d "*.chancel.me" --server letsencrypt --renew --log --debug 2'

这里使用的是neilpang/acme.sh镜像,然后添加一些处理:

  • 将当前路径下的 ./acmesh 目录作为镜像存放数据的卷,这样每次运行都可以自动续签证书
  • command 命令中添加了日志与详细的debug输出

运行该容器

Bash
sudo docker-compose up

检查输出没有错误后,检阅一下垆坶,证书位置位于 ./acmesh/chancel.me_ecc/ ,如下:

Bash
$ tree                                                                                            
.
├── ...
│   ├── chancel.me_ecc
│   │   ├── ca.cer
│   │   ├── chancel.me.cer
│   │   ├── chancel.me.conf
│   │   ├── chancel.me.csr
│   │   ├── chancel.me.csr.conf
│   │   ├── chancel.me.key
│   │   └── fullchain.cer
│   └── ...
└── docker-compose.yml

可以通过 openssl 工具检查证书信息:

Bash
openssl x509 -in fullchain.cer -text -noout

检查输出中 X509v3 Subject Alternative Name 这一行是否包含了指定的2个域名参数,如下:

TEXT
Certificate:
    Data:
        ...
        X509v3 extensions:
            ...
            X509v3 Subject Alternative Name: 
                DNS:*.chancel.me, DNS:chancel.me
            ...
    ...

可以看到,DNS中包含了泛域名 *.chancel.me 也包括了单域名 chancel.me

手动将docker-compose添加到crontab任务中,就可以在证书到期前30天自动更新


[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]