使用acme.sh申请泛域名证书

Camellia 发布于 2026-03-05 71 次阅读


Let's Encrypt 证书申请指南

Let's Encrypt 是一个优秀的域名证书机构,旨在普及 HTTPS,如今已支持签发泛域名证书(俗称“野卡”),并且免费提供。相比其它机构的泛域名证书,Let's Encrypt 的确是一个很好的选择。

参考资料

可以参考 官方说明

一、安装 acme.sh

在 Linux 下在线安装脚本,使用以下命令:

curl https://get.acme.sh | sh -s email=my@example.com

或者:

wget -O - https://get.acme.sh | sh -s email=my@example.com

或者使用 git 安装:

git clone --depth 1 https://github.com/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install -m my@example.com

安装过程中完成的任务

  1. 拷贝脚本到 ~/.acme.sh/
  2. 创建别名:acme.sh=~/.acme.sh/acme.sh
  3. 启动定时任务,添加到 crontab

若未自动创建别名,可以自行创建:

alias acme.sh='/root/.acme.sh/acme.sh'

定时任务示例如下:

0 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

高级安装方式

如果不想使用默认安装,可以指定安装目录等相关内容:

git clone --depth 1 https://github.com/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install \
--home ~/myacme \
--config-home ~/myacme/data \
--cert-home ~/mycerts \
--accountemail "my@example.com" \
--accountkey ~/myaccount.key \
--accountconf ~/myaccount.conf \
--useragent "this is my client."

参数说明:

  • --home:指定 acme.sh 安装目录,默认是 ~/.acme.sh
  • --config-home:指定可写文件夹,用于存储所有配置及证书,默认为 --home
  • --cert-home:指定证书存储目录,默认为 --config-home
  • --accountemail:注册 Let's Encrypt 帐户的电子邮箱,用于接收续订通知。
  • --accountkey:保存账户私钥的文件路径,默认为 --config-home
  • --accountconf:账户配置文件路径,默认为 --config-home
  • --user-agent:发送给 Let's Encrypt 的请求信息。

注册账户

在申请域名证书之前,需要先注册(详细信息参考 ZeroSSL CA 说明):

acme.sh --register-account -m cert@yourmail.com --server zerossl

如果已注册过邮箱,后续可直接使用 --accountemail 参数安装。

二、申请域名证书

安装好 acme.sh 后,可以使用 http、tls、DNS 等方式申请单域名或多域名证书。

1. Webroot 模式

如果已在服务器上建立网站,可以通过 Webroot 方式验证申请证书:

acme.sh --issue -d example.com -d www.example.com -w /home/wwwroot/example.com

注意:/home/wwwroot/example.com 是网站根目录,必须可访问。

2. 独立模式

如果没有启用 Web 服务,可以使用独立模式申请证书,该模式需做 A 记录到服务器的 IP:

acme.sh --issue -d example.com -d www.example.com --standalone

如服务器未开放 80 端口,可使用 --httpport 指定端口:

acme.sh --issue -d example.com -d www.example.com --standalone --httpport 8080

3. TLS 独立模式

如果仅开放了 443 端口,可以使用 TLS 模式申请证书:

acme.sh --issue -d example.com -d www.example.com --alpn

非标准端口可使用 --tlsport 指定:

acme.sh --issue -d example.com -d www.example.com --alpn --tlsport 8443

4. Nginx / Apache 模式

如果服务器上已安装 Nginx 或 Apache,可以使用以下命令直接申请证书:

acme.sh --issue -d example.com --nginx
acme.sh --issue -d example.com --apache

5. DNS API 方式(泛域名证书)

导入 DNS API (以下以阿里云为例):

export Ali_Key="ali-key"
export Ali_Secret="ali-secret"

申请泛域名证书:

acme.sh --issue --dns dns_ali -d example.com -d *.example.com

6. DNS 手动方式

手动增加 DNS 和 TXT 记录以验证:

acme.sh --issue -d example.com -d *.example.com --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please

三、拷贝证书到 Nginx 或 Apache 目录

使用 --install-cert 命令,将证书拷贝到指定位置:

Apache 示例:

acme.sh --install-cert -d example.com \
--cert-file /path/to/certfile/in/apache/cert.pem \
--key-file /path/to/keyfile/in/apache/key.pem \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd "service apache2 reload"

Nginx 示例:

acme.sh --install-cert -d example.com \
--key-file /path/to/keyfile/in/nginx/key.pem \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd "docker exec nginx nginx -s reload && docker restart portainer"

四、升级更新 acme.sh

使用以下命令升级:

acme.sh --upgrade

自动更新命令:

acme.sh --upgrade --auto-upgrade

关闭自动升级:

acme.sh --upgrade --auto-upgrade 0

五、基本管理

  1. 查看已签发证书的域名:

     acme.sh --list
  2. 查看指定域名证书信息:

     acme.sh --info -d example.com
  3. 移除不需要再签发证书的域名:

     acme.sh --remove -d example.com
  4. 删除 acme.sh:

     acme.sh --uninstall

重要更新

现在 acme.sh 支持 ZeroSSL 申请泛域名证书,默认使用该方式。如需切换到 Let's Encrypt,请使用:

acme.sh --set-default-ca --server letsencrypt

若要恢复使用 ZeroSSL:

acme.sh --set-default-ca --server zerossl

注册账户(可能导致超时错误):

acme.sh --register-account -m myemail@example.com --server zerossl

参考链接

ps: 抄至https://www.psay.cn/toss/126.html