原创

利用acme.sh 申请 Google 免费证书

1.Google API权限准备

获取 EAB 密钥 ID 和 HMAC

  1. 登录你的 GCP 控制台面板,进入 Public Certificate Authority API 管理页面(https://console.cloud.google.com/apis/library/publicca.googleapis.com)点击启动
googleapi
或者直接在下一步的 Cloud Shell 中使用下面的命令启动也可以:
gcloud services enable publicca.googleapis.com
  1. 点击右上角的菜单栏激活 Cloud Shell,输入下面内容选中项目,如果你只有一个项目的话可以跳过这步,会自动设置为唯一项目:
gcloud config set project [project ID]
或者先使用下面的命令创建一个项目然后再选择该项目:
gcloud projects create [project ID]
gcloud config set project [project ID]
  1. 获取 EAB 密钥 ID 和 HMAC:
gcloud publicca external-account-keys create
中途弹窗点击授权之后会返回下列格式的信息,记录下来后面会用到:
Created an external account key
[b64MacKey: xxxxxxxxxxxxxxxxxxxxxxx
keyId: xxxxxxxxxxxxxxx]
* 获取的 EAB 密钥 7 天内有效,超过 7 天未使用该密钥会失效,注册的 ACME 帐号没有有效期。

2.申请证书

默认以 root 用户进行操作演示。

安装 acme.sh

curl  https://get.acme.sh | sh
若后面出现 command not found,则需要手动执行以下命令:
source ~/.bashrc

签发证书

这里以使用 Cloudflare 的 API 为例,通过 DNS 验证申请 Apex 域名和通配符(example.com 和 *.example.com)证书。
  1. 获取 Cloudflare API,可以自己选择全局 API 还是单域 API,建议创建单域 API 并限制访问 IP 以提高安全性,获取到 API 后注意保存,然后导入:
export CF_Token="xxxxxxxxxxxxxxxxxxxxxxxx"
export CF_Account_ID="xxxxxxxxxxxxxxxxxxxxxxxx"
export CF_Zone_ID="xxxxxxxxxxxxxxxxxxxxxxxx"
其中 CF_Token 为你获取到的 API 密钥,CF_Account_ID 和 CF_Zone_ID 可以在你域名概述页面侧边栏的 API 分栏看到。
  1. 向 Google Public CA 注册 ACME 帐号:
acme.sh  --register-account -m [邮箱] --server google \
    --eab-kid [申请到的 keyId] \
    --eab-hmac-key [申请到的 b64MacKey]
我在第一次注册的时候提示错误:
Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 6
无法解析 host,想了下应该是我的 acme.sh 是很久以前安装的,没有开启自动更新,使用 acme.sh --upgrade 命令更新一下就好了,或者将上面的 --server google 改成 --server https://dv.acme-v02.api.pki.goog/directory 手动指定服务器。
  1. 设置默认 CA:
acme.sh --set-default-ca --server google
  1. 签发 RSA 证书:
acme.sh --issue --dns dns_cf -d example.com -d *.example.com --keylength 2048
* 签发 ECC 证书:
acme.sh --issue --dns dns_cf -d example.com -d *.example.com --keylength ec-256
  1. 最后将证书安装到 Nginx 下:
acme.sh --install-cert -d example.com \
--key-file       /path/to/ssl/private.key  \
--fullchain-file /path/to/ssl/fullchain.pem \
--capath         /path/to/ssl/ca.pem  \
--reloadcmd      "service nginx reload"
* 安装 ECC 证书:
acme.sh --install-cert -d example.com --ecc \
--key-file       /path/to/ssl/ecc_private.key  \
--fullchain-file /path/to/ssl/ecc_fullchain.pem \
--capath         /path/to/ssl/ecc_ca.pem  \
--reloadcmd      "service nginx reload"
最后贴一张 Google 公共证书的证书路径:
 
正文到此结束
Loading...