第02章 - 安装与快速入门
2.1 安装前准备
2.1.1 系统要求
acme.sh 对环境要求极低,主要依赖以下工具:
| 工具 | 用途 | 说明 |
|---|---|---|
curl 或 wget |
下载脚本、与 CA API 通信 | 二选一即可,优先使用 curl |
cron |
自动续期定时任务 | 大多数 Linux 发行版默认安装 |
openssl |
生成密钥、CSR | 几乎所有 Linux 都有,一般无需额外安装 |
socat(可选) |
Standalone TLS-ALPN 模式 | 仅在使用 –alpn 模式时需要 |
2.1.2 检查依赖
# 检查 curl
curl --version
# 检查 wget(如果没有 curl)
wget --version
# 检查 openssl
openssl version
# 检查 cron(systemd 环境下也有)
which crontab
crontab -l
2.1.3 更新 CA 证书(重要!)
在 CentOS/RHEL 较旧版本上,系统 CA 证书可能过期,导致 acme.sh 无法正常连接 CA 服务器:
# CentOS / RHEL
yum update ca-certificates -y
# Debian / Ubuntu
apt update && apt install -y ca-certificates
# Alpine
apk update && apk add ca-certificates
2.1.4 注意事项
- acme.sh 不需要 root 权限即可安装和运行(以当前用户身份安装)
- 如果需要绑定 80/443 端口或修改 Nginx/Apache 配置,则需要相应权限
- 建议以 root 用户安装(方便后续操作 Nginx、Apache 等配置)
- 在中国大陆服务器上,建议预先测试能否访问 CA 服务器
2.2 安装方法
2.2.1 方法一:通过 get.acme.sh 一键安装(推荐)
# 使用 curl(推荐)
curl https://get.acme.sh | sh -s email=your@email.com
# 使用 wget
wget -O - https://get.acme.sh | sh -s email=your@email.com
将 your@email.com 替换为你的真实邮箱地址。该邮箱用于:
- 注册 CA 账户(接收证书相关通知)
- Let’s Encrypt / ZeroSSL 在证书即将过期时发送提醒邮件
2.2.2 方法二:从 GitHub 主分支直接安装
# 使用 curl
curl https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m your@email.com
# 使用 wget
wget -O - https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m your@email.com
提示:在中国大陆访问
raw.githubusercontent.com可能不稳定,建议使用方法一或方法三。
2.2.3 方法三:克隆仓库后安装(适合中国大陆)
# 克隆仓库
git clone --depth 1 https://github.com/acmesh-official/acme.sh.git
# 进入目录
cd acme.sh
# 执行安装
./acme.sh --install -m your@email.com
如果 GitHub 访问较慢,也可以先下载 zip 包再解压安装:
# 下载 zip
wget https://github.com/acmesh-official/acme.sh/archive/refs/heads/master.zip -O acme.sh-master.zip
# 解压
unzip acme.sh-master.zip
cd acme.sh-master
# 安装
./acme.sh --install -m your@email.com
2.2.4 方法四:高级自定义安装
如果需要将 acme.sh 安装到非默认路径,或分离配置文件和证书存储路径:
./acme.sh --install \
--home /opt/acme.sh \
--config-home /opt/acme.sh/data \
--cert-home /etc/ssl/acme \
--accountemail "your@email.com" \
--nocron
参数说明:
| 参数 | 默认值 | 说明 |
|---|---|---|
--home |
~/.acme.sh |
acme.sh 脚本安装目录 |
--config-home |
与 --home 相同 |
配置文件和账户信息存储目录(需要可写) |
--cert-home |
--config-home |
证书文件存储目录 |
--accountemail |
— | 注册 CA 账户的邮箱 |
--nocron |
— | 不安装 cron 定时任务 |
2.3 安装后配置
2.3.1 安装后自动完成的操作
安装脚本会自动执行以下操作:
- 创建安装目录:默认为
~/.acme.sh/ - 复制脚本:将
acme.sh复制到安装目录 - 创建 Shell 别名:在
~/.bashrc、~/.zshrc等文件中添加acme.sh命令别名 - 安装 cron 定时任务:每天定时检查并续期即将过期的证书
2.3.2 使 Shell 别名立即生效
安装完成后,需要重新加载 Shell 配置使 acme.sh 命令生效:
# 如果使用 bash
source ~/.bashrc
# 如果使用 zsh
source ~/.zshrc
# 或直接使用完整路径(无需重新加载)
~/.acme.sh/acme.sh --version
2.3.3 验证安装
# 查看版本信息
acme.sh --version
# 查看帮助信息
acme.sh --help
# 查看当前配置信息
acme.sh --info
正常输出示例:
https://github.com/acmesh-official/acme.sh
v3.0.x
2.3.4 查看 cron 定时任务
crontab -l
安装后应能看到类似以下的 cron 条目:
23 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
每天 00:23 执行一次检查(时间是随机选取的,避免 CA 服务器高峰)。
2.4 快速开始:申请第一张证书
以下以最简单的 Webroot 模式为例,快速申请一张证书。
2.4.1 前提条件
- 已有可用的域名(如
example.com) - 域名 DNS 已解析到当前服务器
- 服务器 80 端口可访问(HTTP-01 验证需要)
- Nginx 或 Apache 已安装并运行
2.4.2 申请证书(Nginx Webroot 示例)
假设 Nginx 的网站根目录为 /var/www/html:
# 申请证书
acme.sh --issue -d example.com -d www.example.com -w /var/www/html
申请成功后输出类似:
[Mon 15 Jan 2024 10:00:00] Your cert is in: /root/.acme.sh/example.com/example.com.cer
[Mon 15 Jan 2024 10:00:00] Your cert key is in: /root/.acme.sh/example.com/example.com.key
[Mon 15 Jan 2024 10:00:00] The intermediate CA cert is in: /root/.acme.sh/example.com/ca.cer
[Mon 15 Jan 2024 10:00:00] And the full chain certs is in: /root/.acme.sh/example.com/fullchain.cer
2.4.3 安装证书到 Nginx
# 创建 SSL 证书存储目录
mkdir -p /etc/nginx/ssl
# 安装证书
acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/example.com.key \
--fullchain-file /etc/nginx/ssl/example.com.fullchain.pem \
--reloadcmd "systemctl reload nginx"
2.4.4 配置 Nginx HTTPS
编辑 Nginx 站点配置文件(如 /etc/nginx/conf.d/example.com.conf):
server {
listen 80;
server_name example.com www.example.com;
# HTTP 重定向到 HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/example.com.fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
root /var/www/html;
index index.html;
}
重载 Nginx 配置:
nginx -t && systemctl reload nginx
2.5 卸载 acme.sh
如果需要完全卸载 acme.sh:
# 卸载 acme.sh
acme.sh --uninstall
# 卸载后手动删除目录(可选)
rm -rf ~/.acme.sh/
卸载操作会:
- 移除 Shell 别名
- 删除 cron 定时任务
- 但不会删除
~/.acme.sh/目录(需手动删除)
2.6 常见安装问题
2.6.1 中国大陆网络问题
在中国大陆服务器上安装 acme.sh 时,可能遇到以下问题:
问题 1:无法访问 get.acme.sh
# 解决方案:使用镜像或直接 git clone
git clone https://gitee.com/neilpang/acme.sh.git # Gitee 镜像(速度更快)
cd acme.sh
./acme.sh --install -m your@email.com
问题 2:安装后申请证书时无法连接 ZeroSSL
# 解决方案:切换默认 CA 为 Let's Encrypt
acme.sh --set-default-ca --server letsencrypt
问题 3:Let’s Encrypt 服务器也访问困难
部分国内服务器的出口 IP 可能被 Let’s Encrypt 限速。可以:
- 换一个运营商的服务器
- 使用 DNS-01 验证(对网络连通性要求更低)
- 使用代理(不建议在 acme.sh 中直接设置)
2.6.2 权限问题
# 如果提示权限不足,确保当前用户有执行权限
chmod +x ~/.acme.sh/acme.sh
# 如果使用非 root 用户,确保有对应目录的写权限
ls -la ~/.acme.sh/
2.6.3 cron 无法运行
在 Docker 容器或某些最小化系统中,cron 可能未安装或未运行:
# Ubuntu/Debian
apt install -y cron
systemctl enable cron
systemctl start cron
# CentOS/RHEL
yum install -y cronie
systemctl enable crond
systemctl start crond
2.7 小结
本章介绍了 acme.sh 的安装方法和初始配置:
- 推荐使用
curl https://get.acme.sh | sh -s email=your@email.com一键安装 - 中国大陆可使用 Gitee 镜像克隆后安装
- 安装后通过
source ~/.bashrc使别名生效 - 安装完成即自动配置好每日续期的 cron 任务
- 快速申请证书只需
--issue+--install-cert两步
下一章将详细介绍 HTTP 验证方式的各种模式。