1.安装acme.sh
普通用户和 root 用户都可以安装使用. 安装很简单, 一个命令:
1
| curl https://get.acme.sh | sh -s email=my@example.com
|
2.生成证书
acme.sh 实现了 acme 协议支持的所有验证协议. 一般有两种方式验证: http 和 dns 验证. dns验证又分为手动和自动两种方式,我使用的是dns自动验证方式。其它方式请参考acme.sh官方文档,会放在文末。
我的域名在阿里云上,使用dns自动验证域名需要在阿里云开一个RAM账号,地址为: https://ram.console.aliyun.com/users,添加完RAM用户后,权限只需要给 AliyunDNSFullAccess(管理云解析(DNS)的权限) 权限即可。
在系统环境变量文件中添加如下环境变量,一般在~/.bashrc 文件中添加:
<key> 和 <secret> 替换为刚添加的 RAM 账号的 key 和 secret
1 2
| export Ali_Key="<key>" export Ali_Secret="<secret>"
|
至此,准备工作完成。
域名托管在其它平台的,请看这里找到对应的方案: https://github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_ali
2.1申请证书
以域名 xiuyuan.xin 为例,申请二级域名demo.xiuyuan.xin 的SSL证书。
1
| /root/.acme.sh/acme.sh --issue --dns dns_ali -d demo.xiuyuan.xin
|
如果出错,请在上边的命令添加 --log 即可查看详细日志。
2.2安装证书
把生成的证书安装到指定的目录,本例为:/etc/nginx/cert
1 2 3 4 5 6
| /root/.acme.sh/acme.sh --installcert \ -d demo.xiuyuan.xin \ --key-file /etc/nginx/cert/demo.xiuyuan.xin.key \ --fullchain-file /etc/nginx/cert/demo.xiuyuan.xin.crt \ --reloadcmd "systemctl restart nginx.service"
|
2.3刷新证书时间
一般无需手动刷新,acme.sh会自动更新证书有效期。
1
| /root/.acme.sh/acme.sh --renew -d demo.xiuyuan.xin --dns dns_ali
|
3.简化操作脚本
为了方便我使用,少敲一些命令,我写了个简化脚本。使用方法看脚本吧,写的很清晰了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #!/bin/bash
function show_help { echo "使用方法: $0 <域名> <操作>" echo "此脚本专门生成 xiuyuan.xin 域名的子域名的SSL证书使用,如需生成其它域名证书,请参考此脚本写法及 acme.sh 文档 (https://github.com/acmesh-official/acme.sh)" echo "操作:" echo " new - 生成证书 example: ym demo new 就是申请demo.xiuyuan.xin 这个二级域名的证书" echo " install - 安装证书 example ym demo install 会把demo.xiuyuan.xin 证书安装在/etc/nginx/cert 目录下" echo " renew - 更新证书 example ym demo renew 更新demo.xiuyuan.xin 域名证书有效期,一般无需手动操作,acme.sh 定时任务会自动处理" echo " -h, -help - 显示此帮助信息" }
while [[ "$1" == -* ]]; do case "$1" in -h|-help) show_help exit 0 ;; *) echo "无效的选项: $1" show_help exit 1 ;; esac shift done
YU_MING=$1
OP=$2
if [ "$OP" == "" ]; then echo "缺少操作参数" show_help exit 1 elif [ "$OP" == "new" ]; then /root/.acme.sh/acme.sh --issue --dns dns_ali -d ${YU_MING}.xiuyuan.xin exit 1 elif [ "$OP" == "install" ]; then /root/.acme.sh/acme.sh --installcert \ -d ${YU_MING}.xiuyuan.xin \ --key-file /etc/nginx/cert/${YU_MING}.xiuyuan.xin.key \ --fullchain-file /etc/nginx/cert/${YU_MING}.xiuyuan.xin.crt \ --reloadcmd "systemctl restart nginx.service" exit 1 elif [ "$OP" == "renew" ]; then /root/.acme.sh/acme.sh --renew -d ${YU_MING}.xiuyuan.xin --dns dns_ali exit 1 else echo "无效的操作: $OP" show_help exit 1 fi
|
acme.sh 官方中文文档