阿里云+acme.sh自动HTTPS证书申请

转载:https://zhuanlan.zhihu.com/p/681746103

安装acme.sh

由于网络问题,无法直接使用脚本。所以将 acmesh-official/acme.sh: A pure Unix shell script implementing ACME client protocol (github.com) 下载解压(如果没装unzip,需要用apt或者yum等工具安装一下),并安装:

  • unzip acme.sh-master.zip
  • cd acme.sh-master
  • ./acme.sh --install -m i@test.top

这里的 i@test.top 换成自己的邮箱。之后:

  • cd ~
  • source .bashrc

这样就可以用acme.sh命令了。

阿里云设置

安全起见,在阿里云,选择access key,【创建子账户】并获取对应的key和secret。记录下来。并点击用户名,给这个子账户添加DNS相关权限。

千万别漏了授权DNS相关权限的步骤!

授权后,命令行执行:

export Ali_Key="key值"
export Ali_Secret="key Secret"
acme.sh --issue --dns dns_ali -d *.test.top --force

这里的 *.test.top 是要申请HTTPS证书的域名。前面两个export执行一次之后就会自动记住,不用放bashrc。

之后成功了,会提示信息。告诉你四个文件的位置。

[Wed Feb  7 10:15:36 CST 2024] Your cert is in: /root/.acme.sh/*.test.top_ecc/*.test.top.cer
[Wed Feb  7 10:15:36 CST 2024] Your cert key is in: /root/.acme.sh/*.test.top_ecc/*.test.top.key
[Wed Feb  7 10:15:36 CST 2024] The intermediate CA cert is in: /root/.acme.sh/*.test.top_ecc/ca.cer
[Wed Feb  7 10:15:36 CST 2024] And the full chain certs is there: /root/.acme.sh/*.test.top_ecc/fullchain.cer

把 /root/.acme.sh/*.test.top_ecc/fullchain.cer 和 /root/.acme.sh/*.test.top_ecc/*.test.top.key 替换掉nginx里面原来的pem和key文件。

为了能让nginx在证书更新后自动重新加载,创建一个脚本文件(我放在了 /root/nginx/reload_nginx.sh ),内容为:

#!/bin/bash
service nginx restart

加上可执行权限:chmod +x /root/nginx/reload_nginx.sh

接下来执行: acme.sh --install-cert -d *.test.top --reloadcmd "/root/nginx/reload_nginx.sh"

证书自动化工具

https://github.com/mouday/domain-admin

相关文章