acme.sh 快速实现 https 证书颁发与自动续期
200810-acme.sh 快速实现 https 证书颁发与自动续期
借助acem.sh来迅速实现 let's encrypt 的泛域名 ssl 证书颁发与续期,基本上五分钟就可以解决战斗
本文主要内容来自 acme.sh 的官方 wiki,一切以官方说明为准: acme wiki[1]
I. 安装步骤
1. 登录服务器
登录到某台 linux 服务器,我这里以 Centos 举例说明
ssh?xxx@xxx
#?切换root账号
su
2. 安装acme.sh
yum?install?socat?-y
curl??https://get.acme.sh?|?sh
cd?~/.acme.sh/
3. 申请密钥
到域名购买服务商,申请 api key,用于后期的 txt 记录验证
DNSPod
密钥申请完毕之后,如下操作导入命令
#?DNSPod
export?DP_Id="id"
export?DP_Key="key"
阿里云
ALY_KEY 和 ALY_TOKEN:阿里云 API key 和 Secrec 官方申请文档[2]。
申请完毕之后,如下操作
export?Ali_Key="key"
export?Ali_Secret="secret"
godaddy
- GODADDY_KEY 和 GODADDY_TOKEN:GoDaddy API 密钥官方申请文档[3]
export?GD_Key="key"
export?GD_Secret="secret"
其他
至于其他平台,应该如何导入 API key,可以参考下面的文档,这里不一一说明了
- https://github.com/acmesh-official/acme.sh/wiki/dnsapi[4]
4. 证书生成
#?请注意,--dns后面的参数,一般来讲后缀就是上面的导入key的前缀
#?如果不确定,到上面的github连接中去找
#?针对?hhui.top?域名生成通配的证书
./acme.sh?--issue?--dns?dns_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?????"service?nginx?force-reload"
6. nginx 配置
然后就是配置 nginx,支持 https
下面是一个基础的 nginx 配置实例
server?{
????server_name?blog.hhui.top;
????root?/home/yihui/xxx;
????index?index.html;
????gzip?on;
????gzip_buffers?32?4K;
????gzip_comp_level?6;
????gzip_min_length?100;
????gzip_types?application/javascript?text/css?text/xml;
????gzip_disable?"MSIE?[1-6]\.";?#配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
????gzip_vary?on;
????location?~*?^.+\.(ico|gif|jpg|jpeg|png)$?{
????????access_log???off;
????????expires??????1d;
????}
????location?~*?^.+\.(css|js|txt|xml|swf|wav|pptx)$?{
????????access_log???off;
????????expires??????10m;
????}
????location?/?{
????????try_files?$uri?$uri/?@router;
????}
????location?@router?{
????????rewrite?^.*$?/index.html?last;
????}
????listen?443?ssl;
????ssl_certificate?/etc/nginx/ssl/cert.pem;
????ssl_certificate_key?/etc/nginx/ssl/key.pem;
????ssl_stapling?on;
????ssl_stapling_verify?on;
????resolver?8.8.8.8?8.8.4.4?1.1.1.1?valid=60s;
????resolver_timeout?2s;
}
server?{
????if?($host?=?blog.hhui.top)?{
????????return?301?https://$host$request_uri;
????}
????listen?80;
????server_name?blog.hhui.top;
????return?404;
}
参考资料
[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