自文章:https://learnku.com/articles/64097

手动DNS申请SSL证书

# 手动DNS申请
acme.sh --issue -d www.xxx.com,m.xxx.com --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please
#会报错,提示需要解析
#手动解析后再尝试执行命令,加上 --renew  参数
acme.sh --renew -d www.xxx.com,m.xxx.com --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please
#如果同时申请多个域名,--renew出现以下报错的话,可能需要改成单个域名才能找到申请的目录
#报错:
'm.xx.com,www.xx.com' is not an issued domain, skipping.
#换成单个域名(第一个域名,因为申请的时候key放到这个目录了m.xx.com_ecc/m.xx.com.key,renew的时候去m.xx.com,www.xx.com这个目录找,找不到文件)
acme.sh --renew -d m.xx.com --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please
  1. 安装 acme.sh#
    以下三种任选一种即可,把 my@example.com 修改成自己的邮箱。

安装过程不会污染任何功能和文件,所有的修改都限制在安装目录中:~/.acme.sh/。

(1)通过 https://get.acme.sh 安装
curl https://get.acme.sh | sh -s email=my@example.com
或者

wget -O - https://get.acme.sh | sh -s email=my@example.com
(1)通过 GitHub 安装
curl https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m my@example.com
或者

wget -O - https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m my@example.com
(3)通过 git clone 安装

使用加速通道

git clone https://github.com.cnpmjs.org/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install -m my@example.com
安装过程进行了以下几步:
(1)acme.sh 安装到 home 目录下

~/.acme.sh/
(2)创建一个 bash 的 alias

alias acme.sh=~/.acme.sh/acme.sh
(3)创建 cronjob,每天 0:00 自动检测所有证书,如果快过期了,会自动更新证书。

  1. 生成证书
    以下两种任选一种即可,把 mydomain.com 更换成自己的域名。

如何生成泛域名证书,请参阅 使用 acme.sh 生成免费的泛域名证书

(1)http 方式
指定域名,并指定网站根目录。 acme.sh 会全自动生成验证文件,并放到网站的根目录,自动完成验证。最后会删除验证文件,整个过程没有任何副作用。

acme.sh --issue -d mydomain.com -d www.mydomain.com --webroot /home/wwwroot/mydomain.com/
如果使用 apache 服务器, acme.sh 可以智能的从 apache 的配置中自动完成验证,不需要指定网站根目录:

acme.sh --issue -d mydomain.com --apache
如果使用 nginx 服务器,或者反向代理,acme.sh 可以智能的从 nginx 的配置中自动完成验证,不需要指定网站根目录:

acme.sh --issue -d mydomain.com --nginx
注意!无论是 apache 还是 nginx 模式,acme.sh 不会自动修改配置文件,需要手动修改配置文件,否则无法访问 https

如果还没有运行任何 web 服务,80 端口是空闲的,那么 acme.sh 还能假装自己是一个 webserver,临时监听 80 端口,完成验证:

acme.sh --issue -d mydomain.com --standalone
(2)DNS 方式
好处:不需要任何服务器,不需要任何公网 IP, 只需要 DNS 的解析记录即可完成验证。

坏处:如果不同时配置 Automatic DNS API 则 acme.sh 将无法自动更新证书。

acme.sh --issue --dns -d mydomain.com \
--yes-I-know-dns-manual-mode-enough-go-ahead-please
acme.sh 会生成相应的解析记录,到域名解析中添加 TXT 记录,解析成功后,重新生成证书。

acme.sh --renew -d mydomain.com \
--yes-I-know-dns-manual-mode-enough-go-ahead-please
DNS 方式的真正强大之处在于可以使用域名解析商提供的 api 自动添加 TXT 记录完成验证。

acme.sh 目前支持几十种域名服务商:dnsapi

以 dnspod 为例,需要先登录到 dnspod 账号,生成 api id 和 api key

export DP_Id="1234"

export DP_Key="qwertyuiopasdfghjkl"

acme.sh --issue --dns dns_dp -d mydomain.com -d www.mydomain.com
acme.sh 会自动生成证书,并且会记录 api id 和 api key 以后再使用 dnspod api 时就不需要再指定了。

  1. 复制 / 安装 证书
    注意!默认生成的证书都放在安装目录下:~/.acme.sh/,不要直接使用此目录下的文件,这里面的文件都是内部使用,而且目录结构可能会变化。

正确的使用方法是使用 --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 force-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 "service nginx force-reload"

  1. 更新证书
    证书在 60 天以后会自动更新,无需任何操作。
  2. 更新 acme.sh
    升级 acme.sh 到最新版

acme.sh --upgrade
如果不想手动升级,也可以开启自动升级

acme.sh --upgrade --auto-upgrade
关闭自动更新

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

  1. 出错怎么办
    在命令后添加 --debug 或 --debug 2 比如:

acme.sh --issue -d mydomain.com --nginx --debug

acme.sh --issue -d mydomain.com --nginx --debug 2
或者查阅日志

~/.acme.sh/acme.sh.log

标签: none

添加新评论