# Let's Encrypt 从申请到配置一劳永逸

`Let's Encrypt` 是一个由非营利性组织 互联网安全研究小组（ISRG）提供的免费、自动化和开放的证书颁发机构（CA）。 简单的说，借助 `Let's Encrypt` 颁发的证书可以为我们的网站免费启用 HTTPS(SSL/TLS) 。

我这里直接使用第三方客户端 `acme.sh` 申请，官方推荐 `Certbot` 客户端来签发证书，在尝试了 `Certbot` 和 `acme.sh` 后，推荐使用 `acme.sh` 。

## 安装acme

```
curl https://get.acme.sh | sh
```

## 获取证书

`acme.sh` 实现了 `acme` 协议支持的所有验证协议. 一般有两种方式验证: `http` 和 `dns` 验证，如若允许建议使用 `dns` 验证，`http` 验证在此不再赘述，如有需要点击[传送门](https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E#2-生成证书)。`dns` 验证强大之处在于，可以自动配置DNS，不用去域名后台操作解析记录了，我的域名是在阿里注册的，下面给出阿里云解析的例子，其他地方注册的请参考这里自行修改： [传送门](https://github.com/Neilpang/acme.sh/wiki/dnsapi)

请先前往阿里云后台获取 `App_Key` 跟 `App_Secret` [传送门](https://ak-console.aliyun.com/#/accesskey)

### 设置 `key`

```
# 替换成从阿里云后台获取的密钥
export Ali_Key="Ali_Key"
export Ali_Secret="Ali_Secret"
```

### 获取证书

```
# 泛域名获取
# 泛域名获取时，根域名（wurenstudio.cn）如果也想使用泛域名证书的话，则必须获取根域名证书，如下
acme.sh --issue --dns dns_ali -d wurenstudio.cn -d *.wurenstudio.cn
# 单域名获取
acme.sh --issue --dns dns_ali -d wurenstudio.cn
```

第一次获取证书会进行dns验证，期间需要等待20s，等待后则可生成成功。&#x20;

![等待20s](https://wddzzz-1251134396.cos.ap-beijing.myqcloud.com/hexo/TIM图片20190811183203.png%20)

![生成成功](https://wddzzz-1251134396.cos.ap-beijing.myqcloud.com/hexo/Snipaste_2019-08-11_18-32-54.jpg%20)

生成的证书放在相应的目录下（见上上图）: /www/server/panel/vhost/cert/\*.wurenstudio.cn/

## 配置证书

证书文件介绍：

```
.key 是证书私钥文件
.pem 是证书文件 Nginx 服务器，在阿里云证书中与.crt文件一样
.crt 是证书文件 Apache 服务器 _public.crt(证书)_chain.crt（证书链或中间证书）
.pfx 一般适合Tomcat/IIS服务器，暂不介绍
```

> 参考地址：<https://help.aliyun.com/knowledge_detail/95505.html?spm=5176.2020520163.cas.26.1d32VlfJVlfJ5b>

### Nginx/Tengine 服务器

打开`Nginx`安装目录下`conf`目录下的`nginx.conf`文件：

```
#以下属性中ssl开头的属性与证书配置有直接关系
#其它属性请结合自己的实际情况复制或调整
server {
 listen 443;
 server_name localhost;
 ssl on;
 root html;
 index index.html index.htm;
 # 证书公钥配置
 ssl_certificate   cert/fullchain.pem;
 # 证书私钥配置
 ssl_certificate_key  cert/privkey.key;
 ssl_session_timeout 5m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 location / {
     root html;
     index index.html index.htm;
 }
}
```

### Apache 服务器

打开`apache`安装目录下`conf`目录中的`httpd.conf`文件，找到以下内容去掉注释：

```
LoadModule ssl_module modules/mod_ssl.so
#(如果找不到请确认是否编译过 openssl 插件)
Include conf/extra/httpd-ssl.conf
```

打开`apache`安装目录下`conf.d/ssl.conf`，在配置文件中查找以下配置语句:

```
# 添加 SSL 协议支持协议，去掉不安全的协议
SSLProtocol all -SSLv2 -SSLv3
# 修改加密套件如下
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
# 证书公钥配置
SSLCertificateFile cert/fullchain.crt
# 证书私钥配置
SSLCertificateKeyFile cert/privkey.key
# 证书链配置
SSLCertificateChainFile cert/chain.crt

### 或者以下文件格式
SSLCertificateFile cert/fullchain.pem
SSLCertificateKeyFile cert/privkey.pem
SSLCertificateChainFile cert/chain.pem
```

PS：配置完成后都需要重启相关服务

## 自动续期

目前 `Let's Encrypt` 的证书有效期是 90 天，到期时 `acme.sh` 会自动续期，安装时就已经创建了 `crontab` 定时任务，无需任何操作。

也可以强制续签证书：

```
acme.sh --renew -d example.com --force
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.wddzzz.top/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
