# 1、安装 acme.sh ```bash curl https://get.acme.sh | sh -s email=my@example.com ``` 安装完成后重新连接SSH,使alias生效 [scode type="blue"]普通用户和 root 用户都可以安装使用,会把 acme.sh 安装到你的 home 目录下,并创建 一个 bash 的 alias 此外会自动为你创建 cronjob, 每天 0:00 点自动检测所有的证书 [/scode] # 2、申请证书 [scode type="blue"]在申请证书时指定参数`-k 2048`,可申请RSA证书;指定参数`-k ec-256`,可申请ECC证书。(不指定时默认为ECC证书。ECC证书可以和RSA证书同时部署,有着安全性高、处理速度快的优点,但不是所有的证书签发机构都支持ECC证书)[/scode] ## 方式一:通过HTTP验证文件申请证书 [scode type="yellow"]该方式需要能够通过域名+80端口的方式访问到服务器,请根据实际情况选择对应的命令[/scode] - 此命令会将验证文件放到/root/test/目录下,需确保可以通过`example.com`域名访问到此目录 ```bash acme.sh --issue -d example.com --webroot /root/test/ ``` - 此命令会将自动读取Apache配置文件中example.com网站的目录,需确保可以通过example.com域名访问到此目录 ```bash acme.sh --issue -d example.com --apache ``` - 此命令会将自动读取Nginx配置文件中example.com网站的目录,需确保可以通过example.com域名访问到此目录 ```bash acme.sh --issue -d example.com --nginx ``` - 如果你还没有运行任何web服务,80端口是空闲的,那么acme.sh可临时作为网站服务器监听80端口,代替完成验证 ```bash acme.sh --issue -d example.com --standalone # 当然你也可以指定端口,但前提是你会通过代理或负载均衡转发到该端口。 # 因为 letsencrypt 可只会访问 80 端口哦。 $ acme.sh --issue -d example.com --standalone --httpport 88 ``` ## 方式二:通过校验DNS解析记录申请证书 [scode type="green"]该方式不需要任何服务器,不需要任何公网IP,只需要添加一个TXT类型的DNS解析记录即可完成验证[/scode] ### 方法一:自动修改DNS记录(推荐) [scode type="yellow"]该方法需要调用DNS服务商API接口,且需要开通对应的API权限[/scode] - 阿里云 [获取方法][1] ```bash export Ali_Key="Ali_Key" export Ali_Secret="Ali_Secret" acme.sh --issue --dns dns_ali -d *.example.com -d example2.com ``` - Cloudflare [获取方法][2] ```bash export CF_Key="你的Global API Key" export CF_Email="注册Cloudflare邮箱" acme.sh --issue --dns dns_cf -d *.example.com -d example2.com ``` - DnsPod(腾讯云) [获取方法][3] ```bash export DP_Id="DnsPod ID" export DP_Key="DnsPod Token" acme.sh --issue --dns dns_dp -d *.example.com -d example2.com ``` [scode type="share"]这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用.[/scode] ### 方法二:手动修改DNS记录 [scode type="yellow"]使用这种方法 acme.sh 将无法自动更新证书,每次都需要手动再次重新解析验证域名所有权。[/scode] ```bash acme.sh --issue --dns -d *.example.com -d example2.com ``` # 3、自动复制证书并重启Web服务 接下来需要把证书 copy 到真正需要用它的地方。正确的使用方法是使用 `--install-cert` 命令,并指定目标位置,然后证书文件会被copy到相应的位置。 [scode type="yellow"]证书生成以后,默认都放在安装目录下: `~/.acme.sh/`,请不要直接使用此目录下的文件,这里面的文件都是脚本内部使用,其目录结构未来可能会发生变化[/scode] - Apache ```bash 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 ```bash acme.sh --install-cert -d example.com \ --key-file /path/to/keyfile/in/nginx/key.pem \ --fullchain-file /path/to/fullchain/in/nginx/cert.pem \ --reloadcmd "service nginx force-reload" ``` - NPS ```bash acme.sh --install-cert -d example.com \ --key-file /path/to/keyfile/in/nps/key.pem \ --fullchain-file /path/to/fullchain/in/nps/cert.pem \ --reloadcmd "systemctl restart Nps" ``` 如需安装ECC证书,则需添加参数`--ecc`,例如: ```bash acme.sh --install-cert -d example.com --ecc \ --key-file /path/to/keyfile/in/nginx/key.pem \ --fullchain-file /path/to/fullchain/in/nginx/cert.pem \ --reloadcmd "service nginx force-reload" ``` [scode type="share"]这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用。[/scode] # 4、更新证书 [scode type="blue"]正常情况下证书在 60 天以后会自动更新, 你无需任何操作,你也可以手动执行命令更新[/scode] ```bash acme.sh --cron ``` [scode type="yellow"]如果申请证书时使用的方法是手动修改DNS记录,在需要更新证书时请执行[/scode] ```bash acme.sh --renew -d example.com ``` # 5、设置通知(非必要) ## 5.1 设置更新证书通知 - 钉钉 dingtalk 推送通知到钉钉群聊天机器人,先在群设置中添加一个 webhook 机器人,获得 webhook url,并设置一个 keyword。目前不支持签名模式。[官方文档][4] ```bash export DINGTALK_WEBHOOK='复制的Webhook' export DINGTALK_KEYWORD=acme acme.sh --set-notify --notify-hook dingtalk ``` - 飞书 feishu.cn 推送通知到飞书群聊天机器人。先在群设置中添加一个群机器人,获得 webhook 地址,并设置一个关键字(目前暂不支持签名模式)。[官方文档][5] ```bash export FEISHU_WEBHOOK='https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxx' export FEISHU_KEYWORD=acme acme.sh --set-notify --notify-hook feishu ``` - 企业微信 work.weixin.qq.com 推送通知到企业微信群聊天机器人。先在群设置中添加一个群机器人,获得 webhook 地址,并设置一个关键字(目前暂不支持签名模式)。[官方文档][6] ```bash export WEIXIN_WORK_WEBHOOK='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa' export WEIXIN_WORK_KEYWORD=acme acme.sh --set-notify --notify-hook weixin_work ``` - Telegram 推送通知到TG机器人,先创建[机器人][7],通过[@BotFather][8]获得 token,其次,需要获得TG账户或群组的chat_id,最简单的方法是与你的新机器人开始对话,并向它发送一个简单的测试消息。然后,使用`curl`,调用`getUpdates` API接口,并在返回的json对象中寻找您发送的消息的聊天ID。 ```bash $ bot_token="...." # 输入机器人Token $ curl -s "https://api.telegram.org/bot${bot_token}/getUpdates" | python -mjson.tool { "ok": true, "result": [ { "message": { "chat": { "first_name": "Joe", "id": 12345678, <----- 这个就是chat_id "last_name": "Bloggs", "type": "private", "username": "joebloggs" }, ...... "text": "你刚才输入的消息", ...... $ ``` 一旦你有了`API Token`和你想让机器人设置通知的`chat_id`,设置以下两个变量,供通知脚本使用。 ```bash export TELEGRAM_BOT_APITOKEN="..." # 输入上面@BotFather返回的Token export TELEGRAM_BOT_CHATID="..." # 输入上面获取到的Chat ID ``` 最后设置acme.sh ```bash acme.sh --set-notify --notify-hook telegram ``` ## 5.2 设置通知级别,默认为2 ```bash # --notify-level <0|1|2|3> # 0: 关闭没有通知 # 1: 有错误时才通知 # 2: 证书更新成功或错误才通知 # 3: 跳过更新、更新证书、错误都有通知 acme.sh --set-notify --notify-level 2 ``` ## 5.3 设置通知模式,默认为0 ```bash # --notify-mode <0|1> # 0: 批量模式,所有更新信息放在一条通知里 # 1: 证书模式,每个域名更新都发一条通知 acme.sh --set-notify --notify-mode 0 ``` # 6、其他常用命令 ## 6.1 查询所有命令 ```bash acme.sh -h ``` ## 6.2 查看acme.sh版本 ```bash acme.sh -v ``` ## 6.3 acme.sh列出所有已注册证书 ```bash acme.sh --list ``` ## 6.4 acme.sh脚本升级 ```bash acme.sh --upgrade ``` ## 6.5 设置acme.sh自动升级 ```bash acme.sh --upgrade --auto-upgrade 1 ``` ## 6.6 关闭acme.sh自动升级 ```bash acme.sh --upgrade --auto-upgrade 0 ``` ## 6.7 卸载acme.sh, 并删除所有acme.sh添加的crontab任务. ```bash acme.sh --uninstall ``` ## 6.8 acme.sh更改申请证书机构 [scode type="yellow"]目前版本默认是ZeroSSL,一般不需要修改,可用机构见[Wiki][9][/scode] ```bash acme.sh --set-default-ca --server zerossl ``` [1]: https://help.aliyun.com/document_detail/53045.html [2]: https://developers.cloudflare.com/api/keys/#view-your-api-key [3]: https://docs.dnspod.cn/account/dnspod-token/#token [4]: https://open.dingtalk.com/document/group/assign-a-webhook-url-to-an-internal-chatbot [5]: https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN [6]: https://developer.work.weixin.qq.com/document/path/91770 [7]: https://core.telegram.org/bots#creating-a-new-bot [8]: https://t.me/botfather [9]: https://github.com/acmesh-official/acme.sh/wiki/Server Loading... # 1、安装 acme.sh ```bash curl https://get.acme.sh | sh -s email=my@example.com ``` 安装完成后重新连接SSH,使alias生效 <div class="tip inlineBlock info"> 普通用户和 root 用户都可以安装使用,会把 acme.sh 安装到你的 home 目录下,并创建 一个 bash 的 alias 此外会自动为你创建 cronjob, 每天 0:00 点自动检测所有的证书 </div> # 2、申请证书 <div class="tip inlineBlock info"> 在申请证书时指定参数`-k 2048`,可申请RSA证书;指定参数`-k ec-256`,可申请ECC证书。(不指定时默认为ECC证书。ECC证书可以和RSA证书同时部署,有着安全性高、处理速度快的优点,但不是所有的证书签发机构都支持ECC证书) </div> ## 方式一:通过HTTP验证文件申请证书 <div class="tip inlineBlock warning"> 该方式需要能够通过域名+80端口的方式访问到服务器,请根据实际情况选择对应的命令 </div> - 此命令会将验证文件放到/root/test/目录下,需确保可以通过`example.com`域名访问到此目录 ```bash acme.sh --issue -d example.com --webroot /root/test/ ``` - 此命令会将自动读取Apache配置文件中example.com网站的目录,需确保可以通过example.com域名访问到此目录 ```bash acme.sh --issue -d example.com --apache ``` - 此命令会将自动读取Nginx配置文件中example.com网站的目录,需确保可以通过example.com域名访问到此目录 ```bash acme.sh --issue -d example.com --nginx ``` - 如果你还没有运行任何web服务,80端口是空闲的,那么acme.sh可临时作为网站服务器监听80端口,代替完成验证 ```bash acme.sh --issue -d example.com --standalone # 当然你也可以指定端口,但前提是你会通过代理或负载均衡转发到该端口。 # 因为 letsencrypt 可只会访问 80 端口哦。 $ acme.sh --issue -d example.com --standalone --httpport 88 ``` ## 方式二:通过校验DNS解析记录申请证书 <div class="tip inlineBlock success"> 该方式不需要任何服务器,不需要任何公网IP,只需要添加一个TXT类型的DNS解析记录即可完成验证 </div> ### 方法一:自动修改DNS记录(推荐) <div class="tip inlineBlock warning"> 该方法需要调用DNS服务商API接口,且需要开通对应的API权限 </div> - 阿里云 [获取方法][1] ```bash export Ali_Key="Ali_Key" export Ali_Secret="Ali_Secret" acme.sh --issue --dns dns_ali -d *.example.com -d example2.com ``` - Cloudflare [获取方法][2] ```bash export CF_Key="你的Global API Key" export CF_Email="注册Cloudflare邮箱" acme.sh --issue --dns dns_cf -d *.example.com -d example2.com ``` - DnsPod(腾讯云) [获取方法][3] ```bash export DP_Id="DnsPod ID" export DP_Key="DnsPod Token" acme.sh --issue --dns dns_dp -d *.example.com -d example2.com ``` <div class="tip inlineBlock share"> 这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用. </div> ### 方法二:手动修改DNS记录 <div class="tip inlineBlock warning"> 使用这种方法 acme.sh 将无法自动更新证书,每次都需要手动再次重新解析验证域名所有权。 </div> ```bash acme.sh --issue --dns -d *.example.com -d example2.com ``` # 3、自动复制证书并重启Web服务 接下来需要把证书 copy 到真正需要用它的地方。正确的使用方法是使用 `--install-cert` 命令,并指定目标位置,然后证书文件会被copy到相应的位置。 <div class="tip inlineBlock warning"> 证书生成以后,默认都放在安装目录下: `~/.acme.sh/`,请不要直接使用此目录下的文件,这里面的文件都是脚本内部使用,其目录结构未来可能会发生变化 </div> - Apache ```bash 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 ```bash acme.sh --install-cert -d example.com \ --key-file /path/to/keyfile/in/nginx/key.pem \ --fullchain-file /path/to/fullchain/in/nginx/cert.pem \ --reloadcmd "service nginx force-reload" ``` - NPS ```bash acme.sh --install-cert -d example.com \ --key-file /path/to/keyfile/in/nps/key.pem \ --fullchain-file /path/to/fullchain/in/nps/cert.pem \ --reloadcmd "systemctl restart Nps" ``` 如需安装ECC证书,则需添加参数`--ecc`,例如: ```bash acme.sh --install-cert -d example.com --ecc \ --key-file /path/to/keyfile/in/nginx/key.pem \ --fullchain-file /path/to/fullchain/in/nginx/cert.pem \ --reloadcmd "service nginx force-reload" ``` <div class="tip inlineBlock share"> 这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用。 </div> # 4、更新证书 <div class="tip inlineBlock info"> 正常情况下证书在 60 天以后会自动更新, 你无需任何操作,你也可以手动执行命令更新 </div> ```bash acme.sh --cron ``` <div class="tip inlineBlock warning"> 如果申请证书时使用的方法是手动修改DNS记录,在需要更新证书时请执行 </div> ```bash acme.sh --renew -d example.com ``` # 5、设置通知(非必要) ## 5.1 设置更新证书通知 - 钉钉 dingtalk 推送通知到钉钉群聊天机器人,先在群设置中添加一个 webhook 机器人,获得 webhook url,并设置一个 keyword。目前不支持签名模式。[官方文档][4] ```bash export DINGTALK_WEBHOOK='复制的Webhook' export DINGTALK_KEYWORD=acme acme.sh --set-notify --notify-hook dingtalk ``` - 飞书 feishu.cn 推送通知到飞书群聊天机器人。先在群设置中添加一个群机器人,获得 webhook 地址,并设置一个关键字(目前暂不支持签名模式)。[官方文档][5] ```bash export FEISHU_WEBHOOK='https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxx' export FEISHU_KEYWORD=acme acme.sh --set-notify --notify-hook feishu ``` - 企业微信 work.weixin.qq.com 推送通知到企业微信群聊天机器人。先在群设置中添加一个群机器人,获得 webhook 地址,并设置一个关键字(目前暂不支持签名模式)。[官方文档][6] ```bash export WEIXIN_WORK_WEBHOOK='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa' export WEIXIN_WORK_KEYWORD=acme acme.sh --set-notify --notify-hook weixin_work ``` - Telegram 推送通知到TG机器人,先创建[机器人][7],通过[@BotFather][8]获得 token,其次,需要获得TG账户或群组的chat_id,最简单的方法是与你的新机器人开始对话,并向它发送一个简单的测试消息。然后,使用`curl`,调用`getUpdates` API接口,并在返回的json对象中寻找您发送的消息的聊天ID。 ```bash $ bot_token="...." # 输入机器人Token $ curl -s "https://api.telegram.org/bot${bot_token}/getUpdates" | python -mjson.tool { "ok": true, "result": [ { "message": { "chat": { "first_name": "Joe", "id": 12345678, <----- 这个就是chat_id "last_name": "Bloggs", "type": "private", "username": "joebloggs" }, ...... "text": "你刚才输入的消息", ...... <snip rest of output> $ ``` 一旦你有了`API Token`和你想让机器人设置通知的`chat_id`,设置以下两个变量,供通知脚本使用。 ```bash export TELEGRAM_BOT_APITOKEN="..." # 输入上面@BotFather返回的Token export TELEGRAM_BOT_CHATID="..." # 输入上面获取到的Chat ID ``` 最后设置acme.sh ```bash acme.sh --set-notify --notify-hook telegram ``` ## 5.2 设置通知级别,默认为2 ```bash # --notify-level <0|1|2|3> # 0: 关闭没有通知 # 1: 有错误时才通知 # 2: 证书更新成功或错误才通知 # 3: 跳过更新、更新证书、错误都有通知 acme.sh --set-notify --notify-level 2 ``` ## 5.3 设置通知模式,默认为0 ```bash # --notify-mode <0|1> # 0: 批量模式,所有更新信息放在一条通知里 # 1: 证书模式,每个域名更新都发一条通知 acme.sh --set-notify --notify-mode 0 ``` # 6、其他常用命令 ## 6.1 查询所有命令 ```bash acme.sh -h ``` ## 6.2 查看acme.sh版本 ```bash acme.sh -v ``` ## 6.3 acme.sh列出所有已注册证书 ```bash acme.sh --list ``` ## 6.4 acme.sh脚本升级 ```bash acme.sh --upgrade ``` ## 6.5 设置acme.sh自动升级 ```bash acme.sh --upgrade --auto-upgrade 1 ``` ## 6.6 关闭acme.sh自动升级 ```bash acme.sh --upgrade --auto-upgrade 0 ``` ## 6.7 卸载acme.sh, 并删除所有acme.sh添加的crontab任务. ```bash acme.sh --uninstall ``` ## 6.8 acme.sh更改申请证书机构 <div class="tip inlineBlock warning"> 目前版本默认是ZeroSSL,一般不需要修改,可用机构见[Wiki][9] </div> ```bash acme.sh --set-default-ca --server zerossl ``` [1]: https://help.aliyun.com/document_detail/53045.html [2]: https://developers.cloudflare.com/api/keys/#view-your-api-key [3]: https://docs.dnspod.cn/account/dnspod-token/#token [4]: https://open.dingtalk.com/document/group/assign-a-webhook-url-to-an-internal-chatbot [5]: https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN [6]: https://developer.work.weixin.qq.com/document/path/91770 [7]: https://core.telegram.org/bots#creating-a-new-bot [8]: https://t.me/botfather [9]: https://github.com/acmesh-official/acme.sh/wiki/Server Last modification:May 22, 2024 © Allow specification reprint Like 如果觉得我的文章对你有用,请随意赞赏