acme.sh 自动化证书申请

项目地址

教程均根据官方文档

1.安装

curl https://get.acme.sh | sh -s email=my@example.com
#email自己瞎填
#安装完后,重新打开ssh,才能运行接下来的申请证书的步骤,否则找不到acme指令

注意日志爆红的地方

[Sat Nov 29 11:11:22 PM CST 2025] It is recommended to install socat first.
[Sat Nov 29 11:11:22 PM CST 2025] We use socat for the standalone server, which is used for standalone mode.
[Sat Nov 29 11:11:22 PM CST 2025] If you don't want to use standalone mode, you may ignore this warning.
解释:
acme.sh 脚本在申请证书时有多种验证模式。其中一种叫 Standalone 模式(独立模式)。
在这种模式下,脚本需要自己模拟一个临时的 Web 服务器来证明域名的所有权。
为了实现这个功能,它依赖一个叫 socat 的网络工具。
系统检测到你没有安装 socat,所以给出了提示。

爆红是在提醒你,独立模式必须要安装socat。
独立模式的意思就是,你的VPS没有用NGINX或Caddy运行任何的之类的网站。如果你已经运行了这些网站,并且是要为这些网站申请SSL证书,那就按照文档里教程来。acme会自动把验证文件放在你的NGINX或Caddy能读取到的地方,从而进行验证,申请SSL证书。

但我的VPS上没有运行任何网站,我只是申请一个SSL证书给其他程序使用,所以我得用socat 模式,(应该还可以使用DNS验证,本教程只教你用socat模式)

2.安装socat

apt install -y socat

3.申请证书
有两种,一种是使用80端口申请,一种是443端口。哪个端口闲置就用哪个

acme.sh --issue --standalone -d example.com -d www.example.com -d cp.example.com
#80
acme.sh --issue --alpn -d example.com -d www.example.com -d cp.example.com
#443
acme.sh --issue --standalone -d speedtest.hw.de \
--cert-file      /etc/hysteria/cert.pem  \
--key-file       /etc/hysteria/key.pem  \
--fullchain-file /etc/hysteria/fullchain.pem \
--reloadcmd     "systemctl restart hysteria-server"
#示例 使用独立80端口申请证书,并放在指定目录,并且重启指定程序

证书会每60天重新申请一次,只要你的80端口没被占用,就会自动续期,并且存放在你指定的位置

acme.sh --list
#可以查看你具体运行了哪些项目
chmod 644 /etc/hysteria/key.pem /etc/hysteria/fullchain.pem
#你还有可能遇到其他程序无法读取,因为证书需要root权限读取,可以放开权限

下面是使用DNS API 方式申请证书。无需任何端口

export CF_Token=""
export CF_Zone_ID=""
#先设置好上面的CF API 令牌,再运行下面的
acme.sh --issue --dns dns_cf -d 域名 \
--cert-file      /opt/AdGuardHome/ssl/cert.pem  \
--key-file       /opt/AdGuardHome/ssl/key.pem  \
--fullchain-file /opt/AdGuardHome/ssl/fullchain.pem \
--reloadcmd     "/opt/AdGuardHome/AdGuardHome -s restart"