用 acme.sh 获取免费 SSL 证书
acme.sh --issue 签发证书,然后配合重载命令使用 acme.sh --install-cert,让续期自动部署。acme.sh:安装
curl https://get.acme.sh | sh -s email=admin@example.com
source ~/.bashrc
安装程序会添加一个每日 cron 任务。如果你的环境在意这一点,请在把脚本通过管道交给 shell 之前先检查它。然后设置 CA,因为较新版本的 acme.sh 默认使用 ZeroSSL:
acme.sh --set-default-ca --server letsencrypt
签发证书
| 方法 | 命令 | 适用场景 |
|---|---|---|
| Webroot | acme.sh --issue -d example.com -d www.example.com -w /var/www/html | 站点已在 80 端口运行 |
| Standalone | acme.sh --issue -d example.com --standalone | 没有任何程序占用 80 端口 |
| DNS API | acme.sh --issue -d example.com -d '*.example.com' --dns dns_cf | 通配符,或 80 端口未开放 |
对于 Cloudflare DNS,先用 export CF_Token="..." 导出 API 令牌,以及插件文档中说明的账户或区域 ID。acme.sh 会为续期保存它们。
为你的服务器安装证书
不要让服务器指向 acme.sh 的内部文件夹。把文件复制到稳定的路径,然后重载:
acme.sh --install-cert -d example.com \
--key-file /etc/ssl/example.com/privkey.pem \
--fullchain-file /etc/ssl/example.com/fullchain.pem \
--reloadcmd "systemctl reload nginx"
在 Nginx 或 Apache 配置中使用相同的路径。acme.sh 会记住这些设置,并在每次续期后运行重载命令。
acme.sh:续期
cron 任务每天调用 acme.sh --cron,并续期到期的证书。用以下命令测试和检查:
acme.sh --list
acme.sh --renew -d example.com --force
请谨慎使用 --force,因为重复签发可能触及速率限制。用SSL 检查工具核对结果。
acme.sh 还是 Certbot?
| acme.sh | Certbot | |
|---|---|---|
| 依赖 | 纯 shell 脚本 | Python 或 snap |
| DNS 提供商插件 | 许多内置 | 独立插件 |
| 自动编辑 Web 服务器配置 | 否 | 是,需配合插件 |
另一个选择请参见 Certbot 指南。如果你只需要一个快速的一次性证书,TryFreeSSL.com 可以在浏览器中签发。
故障排查
- 加上
--debug运行,查看验证详情。 - 用 DNS Lookup 检查 DNS 是否指向该服务器。
- 确保重定向没有破坏 HTTP 验证路径。
- 触发速率限制?请等待,并在测试时使用带
--staging的 staging 服务器。
相关阅读:用 Certbot 获取证书、自动化续期和HTTP-01 验证。
常见问题
应该用 root 运行 acme.sh 吗?
请以拥有证书文件的用户运行,并只授予该用户重载服务所需的权限。在简单的服务器上使用 root 很常见。
证书存放在哪里?
存放在 acme.sh 自己的主目录中。务必使用 --install-cert 部署副本。
它支持通配符吗?
支持,通过 DNS 验证。
如何迁移到另一台服务器?
复制 acme.sh 的账户和域名文件夹,或在新主机上签发新证书。
运行 acme.sh 需要 root 吗?
只有安装到系统路径和重载 Web 服务器时才需要。客户端以普通用户运行,cron 任务也可以在该用户下运行。
证书存放在哪里?
默认在 ~/.acme.sh/
它支持通配符吗?
支持,通过其某个 DNS API 插件完成 DNS-01 验证。HTTP-01 无法签发通配符证书。
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.