114 lines
3.7 KiB
Markdown
114 lines
3.7 KiB
Markdown
---
|
||
title: 使用acme.sh 申请 Let's Encrypt 泛域名SSL证书
|
||
id: '333'
|
||
categories:
|
||
- 分享
|
||
tags:
|
||
- SSL证书
|
||
- acme.sh
|
||
- Let's Encrypt
|
||
- 泛域名
|
||
date: 2018-07-23 18:25:45
|
||
---
|
||
|
||
之前用ertbot方式获取Let's Encrypt泛域名证书,必须手动在添加DNS TXT 记录,也不能自动续签,使用起来颇为不便。
|
||
|
||
今天发现了github上的一个开源工具[acme.sh](https://github.com/Neilpang/acme.sh),是一个开源的Let’s Encrypt SSL申请工具,操作和流程比官方的申请方式简化很多,使用起来非常方便,推荐大家使用。欢迎大家给该工具打星。
|
||
|
||
### 安装acme.sh
|
||
|
||
```
|
||
|
||
curl https://get.acme.sh sh
|
||
```
|
||
|
||
执行完代码后,acme.sh会被自动安装到~/.acme.sh/中
|
||
|
||
### 设置DNS API
|
||
|
||
目前泛域名证书仅支持DNS验证,acme.sh通过DNS提供商的API自动完成操作,因此需要先设置DNS API,以阿里云为例。
|
||
|
||
先前往阿里云后台获取App\_Key跟App\_Secret, 在【用户信息管理】-【安全信息管理】创建SecretKey [传送门](https://ak-console.aliyun.com/#/accesskey),然后执行以下脚本
|
||
|
||
```
|
||
|
||
# 替换成从阿里云后台获取的密钥
|
||
export Ali_Key="LTA*********uV6"
|
||
export Ali_Secret="QKmsZgo*************8Htr6"
|
||
```
|
||
|
||
这个临时环境变量只需配置这一次,当成功申请证书后,API 信息会被自动保存在~/.acme.sh/account.conf,下次你使用acme.sh的时候会被自动使用
|
||
|
||
### 获取证书
|
||
|
||
```
|
||
|
||
# 换成自己的域名
|
||
acme.sh --issue --dns dns_ali -d dengtongyu.com -d *.dengtongyu.com
|
||
```
|
||
|
||
如果acme.sh,提示命令没找到,那就请再执行以下指令:
|
||
|
||
```
|
||
|
||
source ~/.bashrc
|
||
acme.sh --issue --dns dns_ali -d dengtongyu.com -d *.dengtongyu.com
|
||
```
|
||
|
||
或者先cd到acme.sh目录下,再执行文件
|
||
|
||
```
|
||
|
||
cd ~/.acme.sh
|
||
./acme.sh --issue --dns dns_ali -d dengtongyu.com -d *.dengtongyu.com
|
||
```
|
||
|
||
\--dns dns\_ali参数表示使用 阿里云 的 API,如果是其他解析商,请[点这里](https://github.com/Neilpang/acme.sh/blob/master/dnsapi/README.md)查看对应的参数
|
||
|
||
执行完代码后,会有120秒等待DNS生效,所以至少需要等待两分钟,等倒数结束后看到Cert success就是成功了
|
||
|
||

|
||
|
||
### 拷贝证书
|
||
|
||
前面证书生成以后, 接下来需要把证书 copy 到真正需要用它的地方.
|
||
|
||
注意, 默认生成的证书都放在安装目录下: ~/.acme.sh/, 请不要直接使用此目录下的文件, 例如: 不要直接让 nginx/apache 的配置文件使用这下面的文件. 这里面的文件都是内部使用, 而且目录结构可能会变化.
|
||
|
||
正确的使用方法是使用 --installcert 命令, 并指定目标位置, 然后证书文件会被copy到相应的位置:
|
||
|
||
```
|
||
|
||
acme.sh --installcert -d dengtongyu.com \
|
||
--key-file /etc/nginx/ssl/dengtongyu.key \
|
||
--fullchain-file /etc/nginx/ssl/fullchain.cer \
|
||
--reloadcmd "service nginx force-reload"
|
||
```
|
||
|
||
(一个小提醒, 这里用的是 service nginx force-reload, 不是 service nginx reload, 据测试, reload 并不会重新加载证书, 所以用的 force-reload)
|
||
|
||
### 在 Nginx 中 配置 Let’s Encrypt 证书
|
||
|
||
Nginx 配置文件片断:
|
||
|
||
```
|
||
|
||
server {
|
||
server_name xxx.com;
|
||
listen 443 http2 ssl;
|
||
ssl on;
|
||
ssl_certificate /etc/nginx/ssl/fullchain.cer;
|
||
ssl_certificate_key /etc/nginx/ssl/dengtongyu.key;
|
||
|
||
location / {
|
||
proxy_pass http://127.0.0.1:8080;
|
||
}
|
||
}
|
||
```
|
||
|
||
### 证书续期
|
||
|
||
Let's Encrypt 证书的有效期为三个月,acme.sh会每隔60天自动帮你续期,你无需任何操作
|
||
|
||
最后,更详细等内容请看[官方说明](https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E)
|