acme.sh从 letsencrypt 生成SSL免费证书并自动更新证书
acme.sh 实现了 acme 协议, 可以从 letsencrypt 生成免费的证书
ACME 协议:
Automatic Certificate Management Environment 自动化证书管理环境
文档:
- github: https://github.com/acmesh-official/acme.sh
- gitee: https://gitee.com/neilpang/acme.sh
- 中文文档: https://github.com/acmesh-official/acme.sh/wiki/说明
- 中文wikis: https://gitee.com/neilpang/acme.sh/wikis/pages
目录
-
- 1、安装acme.sh
-
- 方式一:从github安装
- 方式二:从gitee安装
- 2、申请证书
-
- 方式一:Webroot模式
- 方式二:手动DNS验证申请
- 3、安装/copy证书
- 4、其他命令
1、安装acme.sh
方式一:从github安装
curl https://get.acme.sh | sh -s email=my@example.com# 或者
wget -O - https://get.acme.sh | sh -s email=my@example.com
方式二:从gitee安装
如果国内网络环境不好,可以从gitee下载安装
https://gitee.com/neilpang/acme.sh
wget https://gitee.com/neilpang/acme.sh/repository/archive/master.zip
unzip master.zipcd acme.sh-master./acme.sh --install \\
--email my@example.com
安装过程中会 自动完成
以下操作
1、安装目录:~/.acme.sh
$ ls ~/.acme.sh
account.conf
acme.sh
acme.sh.csh
acme.sh.env
deploy
dnsapi
notify
2、执行文件路径:~/.acme.sh/acme.sh
3、环境变量修改
$ cat ~/.bashrc
. "~/.acme.sh/acme.sh.env"$ cat ~/.cshrc
source "~/.acme.sh/acme.sh.csh"$ cat ~/.tcshrc
source "~/.acme.sh/acme.sh.csh"
4、添加crontab定时任务
12 0 * * * "~/.acme.sh"/acme.sh --cron --home "~/.acme.sh" > /dev/null
查看安装结果
$ source ~/.bashrc$ acme.sh --version
https://github.com/acmesh-official/acme.sh
v3.0.5
2、申请证书
方式一:Webroot模式
只需要指定域名, 并指定域名所在的网站根目录
acme.sh --issue \\
--domain example.com \\
--domain www.example.com \\
--webroot /home/wwwroot/example.com
每60天会自动更新
方式二:手动DNS验证申请
手动在域名上添加一条 txt 解析记录, 验证域名所有权
acme.sh --issue --dns \\
--domain example.com \\
--yes-I-know-dns-manual-mode-enough-go-ahead-please# 在域名解析管理中,添加dns记录值
Add the following TXT record:
[2023年 04月 08日 星期六 21:57:13 CST] Domain: '_acme-challenge.example.com'
[2023年 04月 08日 星期六 21:57:13 CST] TXT value: 'o4MgobqPJ53E4RxRjO0d_mKSwGaNnjDZDlQxIW6iIXc'# 添加完txt解析之后,重新生成证书
acme.sh --renew \\
--domain example.com \\
--yes-I-know-dns-manual-mode-enough-go-ahead-please
注意:使用这种方式 acme.sh 将
无法自动更新证书
3、安装/copy证书
acme.sh --install-cert \\
--domain example.com \\
--key-file /usr/local/nginx/conf/ssl/www.example.com.key \\
--fullchain-file /usr/local/nginx/conf/ssl/www.example.com.pem \\
--reloadcmd "service nginx force-reload"
4、其他命令
# 查看帮助
acme.sh -h# 查看证书列表
acme.sh --list
Main_Domain KeyLength SAN_Domains CA Created Renew
example.com "2048" www.example.com ZeroSSL.com 2023-04-05T12:54:46Z 2023-06-04T12:54:46Z# 查看域名配置
acme.sh --info --domain example.com# 切换默认证书申请服务器
# 默认使用 ZeroSSL,以下示例切换 Let's Encrypt
acme.sh --set-default-ca --server letsencrypt