acme.sh 快速实现 https 证书颁发与自动续期

200810-acme.sh 快速实现 https 证书颁发与自动续期

acme.sh快速实现https证书颁发与自动续期

借助acem.sh来迅速实现 let's encrypt 的泛域名 ssl 证书颁发与续期,基本上五分钟就可以解决战斗

本文主要内容来自 acme.sh 的官方 wiki,一切以官方说明为准: acme wiki[1]

I. 安装步骤

1. 登录服务器

登录到某台 linux 服务器,我这里以 Centos 举例说明

sshxxx@xxx

#切换root账号
su

2. 安装acme.sh

yuminstallsocat-y
curlhttps://get.acme.sh|sh
cd~/.acme.sh/

3. 申请密钥

到域名购买服务商,申请 api key,用于后期的 txt 记录验证

DNSPod

密钥申请完毕之后,如下操作导入命令

#DNSPod
exportDP_Id="id"
exportDP_Key="key"

阿里云

ALY_KEY 和 ALY_TOKEN:阿里云 API key 和 Secrec 官方申请文档[2]

申请完毕之后,如下操作

exportAli_Key="key"
exportAli_Secret="secret"

godaddy

  • GODADDY_KEY 和 GODADDY_TOKEN:GoDaddy API 密钥官方申请文档[3]
exportGD_Key="key"
exportGD_Secret="secret"

其他

至于其他平台,应该如何导入 API key,可以参考下面的文档,这里不一一说明了

  • https://github.com/acmesh-official/acme.sh/wiki/dnsapi[4]

4. 证书生成

#请注意,--dns后面的参数,一般来讲后缀就是上面的导入key的前缀
#如果不确定,到上面的github连接中去找

#针对hhui.top域名生成通配的证书
./acme.sh--issue--dnsdns_ali-d*.hhui.top

证书生成之后,会在.acme.sh目录下,新生成一个 *.hhui.top(就是我们上面指定的通配域名) 文件夹,证书在里面

5. 安装证书

接下来将我们的证书安装到 nginx(当然也可以是 tomcat),下面的脚本除了安装之外,也添加了一个自动更新的任务(一般来说,60 天以后会自动更新,并会强制重启 nginx 使新的证书生效,可以通过 crontab -e看到对应的定时任务)

./acme.sh--installcert-d*.hhui.top--key-file/etc/nginx/ssl/key.pem--fullchain-file/etc/nginx/ssl/cert.pem--reloadcmd"servicenginxforce-reload"

6. nginx 配置

然后就是配置 nginx,支持 https

下面是一个基础的 nginx 配置实例

server{
server_nameblog.hhui.top;
root/home/yihui/xxx;
indexindex.html;

gzipon;
gzip_buffers324K;
gzip_comp_level6;
gzip_min_length100;
gzip_typesapplication/javascripttext/csstext/xml;
gzip_disable"MSIE[1-6]\.";#配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_varyon;

location~*^.+\.(ico|gif|jpg|jpeg|png)nbsp;{
access_logoff;
expires1d;
}

location~*^.+\.(css|js|txt|xml|swf|wav|pptx)nbsp;{
access_logoff;
expires10m;
}

location/{
try_files$uri$uri/@router;
}

location@router{
rewrite^.*nbsp;/index.htmllast;
}

listen443ssl;
ssl_certificate/etc/nginx/ssl/cert.pem;
ssl_certificate_key/etc/nginx/ssl/key.pem;
ssl_staplingon;
ssl_stapling_verifyon;
resolver8.8.8.88.8.4.41.1.1.1valid=60s;
resolver_timeout2s;
}

server{
if($host=blog.hhui.top){
return301https://$host$request_uri;
}

listen80;
server_nameblog.hhui.top;
return404;
}

参考资料

[1]

acme wiki: https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

[2]

API key 和 Secrec 官方申请文档: https://help.aliyun.com/knowledge_detail/38738.html

[3]

API 密钥官方申请文档: https://developer.godaddy.com/getstarted

[4]

https://github.com/acmesh-official/acme.sh/wiki/dnsapi: https://github.com/acmesh-official/acme.sh/wiki/dnsapi