# Linux  SSL证书自动更新管理

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## 前言

&#x20;          对于网站而言，SSL加密非常重要，不仅可以加强网站的安全性，而且还能提高网站的SEO，浏览器不会提示用户站点是非安全的。

**acme.sh** 实现了 `acme` 协议, 可以从 letsencrypt 生成免费的证书.

GitHub项目地址：

{% embed url="<https://github.com/acmesh-official/acme.sh>" %}

[![FreeBSD](https://github.com/acmesh-official/acme.sh/actions/workflows/FreeBSD.yml/badge.svg)](https://github.com/acmesh-official/acme.sh/actions/workflows/FreeBSD.yml) [![MacOS](https://github.com/acmesh-official/acme.sh/actions/workflows/MacOS.yml/badge.svg)](https://github.com/acmesh-official/acme.sh/actions/workflows/MacOS.yml) [![Ubuntu](https://github.com/acmesh-official/acme.sh/actions/workflows/Ubuntu.yml/badge.svg)](https://github.com/acmesh-official/acme.sh/actions/workflows/Ubuntu.yml) [![Windows](https://github.com/acmesh-official/acme.sh/actions/workflows/Windows.yml/badge.svg)](https://github.com/acmesh-official/acme.sh/actions/workflows/Windows.yml) [![Solaris](https://github.com/acmesh-official/acme.sh/actions/workflows/Solaris.yml/badge.svg)](https://github.com/acmesh-official/acme.sh/actions/workflows/Solaris.yml)

[![Shellcheck](https://github.com/acmesh-official/acme.sh/workflows/Shellcheck/badge.svg)](https://github.com/acmesh-official/acme.sh/workflows/Shellcheck/badge.svg) [![PebbleStrict](https://github.com/acmesh-official/acme.sh/workflows/PebbleStrict/badge.svg)](https://github.com/acmesh-official/acme.sh/workflows/PebbleStrict/badge.svg) [![DockerHub](https://github.com/acmesh-official/acme.sh/workflows/Build%20DockerHub/badge.svg)](https://github.com/acmesh-official/acme.sh/workflows/Build%20DockerHub/badge.svg)

[![](https://camo.githubusercontent.com/52af31289b70753701a02c8ea706c7e7443ee88e3fcadd7ab85d08b70ab15d66/68747470733a2f2f6f70656e636f6c6c6563746976652e636f6d2f61636d6573682f616c6c2f62616467652e7376673f6c6162656c3d66696e616e6369616c2b636f6e7472696275746f7273)](https://opencollective.com/acmesh) [![Join the chat at https://gitter.im/acme-sh/Lobby](https://camo.githubusercontent.com/804392fbd00870d79774c9dadfa4d53c0213819aa27b3dc48a3dcba4686aac28/68747470733a2f2f6261646765732e6769747465722e696d2f61636d652d73682f4c6f6262792e737667)](https://gitter.im/acme-sh/Lobby?utm_source=badge\&utm_medium=badge\&utm_campaign=pr-badge\&utm_content=badge) [![Docker stars](https://camo.githubusercontent.com/a71fce147f8844bd2c73325a2b4c965ec05b73698e6d0da92dcc64ac0de74c9a/68747470733a2f2f696d672e736869656c64732e696f2f646f636b65722f73746172732f6e65696c70616e672f61636d652e73682e737667)](https://hub.docker.com/r/neilpang/acme.sh) [![Docker pulls](https://camo.githubusercontent.com/c967758db92f66227e899f121596e12d61e9c5120479552eb95a91eaa3af4258/68747470733a2f2f696d672e736869656c64732e696f2f646f636b65722f70756c6c732f6e65696c70616e672f61636d652e73682e737667)](https://hub.docker.com/r/neilpang/acme.sh)

## 一、通过 Certbot 自动更新证书

{% hint style="success" %}
&#x20;Certbot 是一个免费的开源软件工具，用于在手动管理的网站上自动使用[Let's Encrypt](https://letsencrypt.org/)证书以启用 HTTPS。
{% endhint %}

### 1、**CentOS 8** 安装 SNAP

```
dnf -y install epel-release
dnf upgrade
dnf -y install snapd
systemctl enable --now snapd
```

### **2、CentOS 7 安装** SNAP

#### 2.1 通过添加`epel`存储库并安装`copr yum`插件开始安装。

```
yum -y install epel-release yum-plugin-copr
```

#### 2.2 添加仓库

```
yum copr enable ngompa/snapcore-el7
```

#### 2.3 添加存储库后，安装`snapcore`包。

```
yum -y install snapd
```

#### 2.4 安装完成，启用`snapd socket`

```
systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap
```

#### 2.5 查询帮助页面

```
snap --help
```

### 3、更新 SNAP 到最新版本

```
snap install core
snap refresh core
ln -s /var/lib/snapd/snap /snap
```

### 4、使用 SNAP 安装 Certbot

```
snap install --classic certbot
```

### 5、建立 Certbot 的软连接

```
ln -s /snap/bin/certbot /usr/bin/certbot
```

### 6、使用 Certbot获取SSL证书，可以指定 –apache 或者 –nginx, 这两个是最常用的选项，这里使用 Nginx。

```
certbot --nginx
```

创建过程中会询问一些基础的问题，比如说 email、域名等。

![](/files/-Mev3O1NWedaqCZaOW0m)

### 7、使用 Certbot 进行自动更新

```
certbot renew --dry-run
```

## 二、通过 acme.sh 自动更新证书

#### 1、CentOS 终端下执行：

```
dnf -y install socat
git clone https://github.com/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install -m  myemail@example.com
```

#### 案例

```
git clone https://github.com/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install  \
--home ~/myacme \
--config-home ~/myacme/data \
--cert-home  ~/mycerts \
--accountemail  "my@example.com" \
--accountkey  ~/myaccount.key \
--accountconf ~/myaccount.conf \
--useragent  "this is my client."
```

#### 说明：

* `--home`是一个自定义的安装目录`acme.sh`。默认情况下，它安装到`~/.acme.sh`
* `-m`，--email  指定账户邮箱，只对'`--install`'和'`--update-account`'命令有效。
* `--config-home`是一个可写文件夹，acme.sh 将在那里写入所有文件（包括证书/密钥、配置），默认情况下，它在`--home`
* `--cert-home`是一个自定义目录，用于保存您颁发的证书，默认情况下，它保存在`--config-home`.
* `--accountemail` 是用于注册 Let's Encrypt 帐户的电子邮件，您将在此处收到续订通知电子邮件。
* `--accountkey`是保存您帐户私钥的文件。默认情况下，它保存在`--config-home`.
* `--user-agent` 是用于发送到 Let's Encrypt 的用户代理标头值。

#### 2、安装完成后，可输入`acme.sh -v`查看当前版本。

```
[root@VM-0-4-centos ~]# acme.sh -v
https://github.com/acmesh-official/acme.sh
v3.0.0
```

`acme.sh` 3.x版本默认使用Let’s Encrypt作为服务提供商，我们可以通过以下命令，将其更换为ZeroSSL：

```
acme.sh --set-default-ca  --server zerossl
```

#### &#x20;3、输入 `acme.sh --help` 即可查看 acme.sh 的帮助命令

## 三、申请证书

### 1、通过 HTTP 方式申请证书

{% hint style="info" %}
只需要指定域名, 并指定域名所在的网站根目录. acme.sh 会全自动的生成验证文件, 并放到网站的根目录, 然后自动完成验证. 最后会删除验证文件. 整个过程没有任何副作用.
{% endhint %}

```
# 生成 RSA 证书：
acme.sh --issue -d zhiqiang.cloud -w /etc/nginx/conf.d/ssl/
# 生成 ECC 证书：
acme.sh --issue -d zhiqiang.cloud -w /etc/nginx/conf.d/ssl/ --keylength ec-256
```

{% hint style="success" %}
`/etc/nginx/conf.d/ssl/` 为 `zhiqiang.cloud` 站点下的证书请求目录。

如果没有安装 web 服务器，acme.sh 也自带一个建议的web服务用于证书申请，前提是需要安装 socat ，80端口没有被占用。使用方法：`acme.sh --issue -d zhiqiang,cloud --standalone`
{% endhint %}

### 2、通过 DNS 方式申请证书

* **优点：**&#x4E0D;需要任何服务器，不需要任何公网 IP，只需要 DNS 的解析记录即可完成验证，而且可申请泛域名证书。
* **缺点：**&#x9700;要配合DNS解析服务商的API使用，否则 acme.sh 将无法自动更新证书，每次都需要手动再次重新解析验证域名所有权。

{% hint style="success" %}
SSL证书验证可通过DNS验证、文件验证等多种方式，为了方便多个域名申请以及后续证书更新，推荐使用DNS API方式，不过在使用前需要先进行设置。
{% endhint %}

#### 阿里云 DNS 申请方式

1、打开 <https://usercenter.console.aliyun.com/#/manage/ak> ，获取 `AccessKey ID` 和 `AccessKey Secret`

2、配置环境变量

```
[root@hezhiqiang ~]# export Ali_Key=---------(换成你自己的AccessKey ID)
[root@hezhiqiang ~]# export Ali_Secret=-------(换成你自己的AccessKey Secret)
```

3、申请证书

```
# RSA 证书
acme.sh --issue --dns dns_ali -d zhiqiang.cloud  -d *.zhiqiang.cloud
# ECC 证书
acme.sh --issue --dns dns_ali -d zhiqiang,cloud -d *.zhiqiang.cloud --keylength ec-256
```

> 申请证书后, key 和 秘钥会被 **明文** 保存在 `~/.acme.sh/account.conf` , 请妥善保管

> 如果需要重新申请证书: 请删除掉该目录下内容 `rm -rf /root/.acme.sh/zhiqiang.cloud/*`

#### 腾讯云 DNSPod 申请方式

&#x20;1、Token可通过：<https://console.dnspod.cn/account/token/token#> 获取。

```
export DP_Id="123456"
export DP_Key="abcdefxjhajk"
acme.sh --issue --dns dns_dp -d zhiqiang.cloud -d www.zhiqiang.cloud
```

* DP\_Id=” ”中请填写您的ID
* DP\_Key=” ”中请填写Token

#### 华为云 DNS 申请方式

```
export HUAWEICLOUD_Username=<Your Username> # Usually hwxxxxxx
export HUAWEICLOUD_Password=<Your Password>
export HUAWEICLOUD_ProjectID=<A Project ID> 
```

* HUAWEICLOUD\_Username:华为云用户名
* HUAWEICLOUD\_Password：华为云密码
* HUAWEICLOUD\_ProjectID：可通过[我的凭据](https://console.huaweicloud.com/iam/?region=cn-southwest-2#/mine/apiCredential)获取

目前支持 DNSPod、CloudXNS、Aliyun、jdcloud、 CloudFlare、 GoDaddy、 Azure、AWS 等国内外大多数主流DNS服务提供商。

> 查看更多DNS API 使用方式, 请查看 <https://github.com/acmesh-official/acme.sh/wiki/dnsapi>

## 四、ZeroSSL使用说明

申请证书之前，建议先在ZeroSSL官方网站：[https://zerossl.com/](https://zerossl.com/?fpr=xiaoz)注册账号，方便与`acme.sh`申请的证书进行绑定于关联。![](https://cdn.xiaoz.me/wp-content/uploads/2021/06/firefox_aySaK634GC.png)

![](/files/-MetwrsWhY3dhcFIkPDk)

{% hint style="danger" %}
**注意：**&#x901A;过ZeroSSL官网在线申请SSL证书,免费账户是有3个域的额度限制的，但通过`acme.sh`申请则没有这个限制，所以建议使用`acme.sh`进行申请，简单方便。
{% endhint %}

### 1、申请ZeroSSL泛域名SSL证书

首次使用需要先注册ZeroSSL账户（参考上一步），如果已经注册，下面的命令会自动关联账户，命令如下（**`myemail@example.com`改成你自己的ZeroSSL邮箱，不要乱填**）：

```
acme.sh  --register-account  -m myemail@example.com --server zerossl
```

以`zhiqiang.cloud`域名为例，该域名使用DNSPod解析，我已经在上一个步骤中设置好DNSPod解析DNS API，现在可以输入命令直接申请：

```
acme.sh --dns dns_dp --issue -d zhiqiang.cloud -d *.zhiqiang.cloud
```

### 2、多域名证书

acme.sh 可以同时申请多个域名.例如拥有域名`aaa.com`,`bbb.com`,`ccc.com`,只需要在命令后加上`-d 空格 域名`即可

#### 例如：

```
acme.sh --issue --server letsencrypt --dns dns_dp -d aaa.com -d *.aaa.com -d bbb.com -d *.bbb.com -d ccc.com -d *.ccc.com
```

{% hint style="danger" %}
多域名申请证书会将多个证书合并为一个证书,并存放到以第一个域名命名的文件夹内,证书信息仅显示第一个域名的信息.
{% endhint %}

## 五、自动部署

![](/files/-Meu2fZQlQEYy9p3Hu8_)

![](/files/-Meu9uIZLcfEwY6UH8ZE)

如果申请过程中没有报错，就可以将SSL证书拷贝到任何想要的位置进行使用，命令如下：

```
acme.sh  --installcert  -d  zhiqiang.cloud   \
        --key-file   /data/ssl/zhiqiang.cloud.key \
        --fullchain-file /data/ssl/zhiqiang.cloud.crt
```

#### 上述命令将`zhiqiang.cloud` SSL证书导出到了`/data/ssl`目录,前提是需要创建相关目录。

## 六、手动拷贝证书 <a href="#anchor-h1-2" id="anchor-h1-2"></a>

### 1、查询域名生成的证书路径.

```
~]# tree /root/.acme.sh/zhiqiang.cloud
/root/.acme.sh/zhiqiang.cloud
├── ca.cer
├── fullchain.cer
├── zhiqiang.cloud.cer
├── zhiqiang.cloud.conf
├── zhiqiang.cloud.csr
├── zhiqiang.cloud.csr.conf
└── zhiqiang.cloud.key

0 directories, 7 files
```

### 2、拷贝到 Nginx 目录.

> 仅供参考,请根据具体的域名进行替换

```
]# mkdir -p /etc/nginx/conf.d/ssl/zhiqiang.cloud
]# cp ~/.acme.sh/zhiqiang.cloud/* /etc/nginx/conf.d/ssl/zhiqiang.cloud
]# tree /etc/nginx/conf.d/ssl
/etc/nginx/conf.d/ssl
└── zhiqiang.cloud
    ├── ca.cer
    ├── fullchain.cer
    ├── zhiqiang.cloud.cer
    ├── zhiqiang.cloud.conf
    ├── zhiqiang.cloud.csr
    ├── zhiqiang.cloud.csr.conf
    └── zhiqiang.cloud.key

1 directory, 7 files
```

## 七、配置证书. <a href="#anchor-h1-3" id="anchor-h1-3"></a>

> 以 CentOS 环境为例, 其他环境未测试,但是应该比较类似.

### 1、新建Nginx配置文件

```
vim /etc/nginx/conf.d/zhiqiang.cloud.conf
```

### 2、写入以下内容

```
server {
    listen 80;                          # 代理端口, 通过此端口重定向到443访问, 不再通过8080端口
    server_name www.zhiqiang.cloud;     # 修改成你的域名或者注释掉，多个域名，以空格分开
    client_max_body_size 100m;          # 文件上传大小限制
    return 301 https://www.zhiqiang.cloud/; # 将 http 重定向 https


    listen 443 ssl;                     # 服务器开启443端口
    server_name www.zhiqiang.cloud;     # 修改成你的域名或者注释掉，多个域名，以空格分开

    ssl_certificate /etc/nginx/conf.d/ssl/zhiqiang.cloud/zhiqiang.cloud.cer;     # crt文件的路径
    ssl_certificate_key /etc/nginx/conf.d/ssl/zhiqiang.cloud/zhiqiang.cloud.key; # key文件的路径

    ssl_session_timeout 5m;              # 缓存有效期
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 安全链接可选的加密协议
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;  # 加密算法
    ssl_prefer_server_ciphers on;        # 使用服务器端的首选算法
    client_max_body_size 100m;           # 文件上传大小限制
    
    }
```

### 3、检测Nginx语法

```
[root@VM-0-4-centos ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
```

### 4、启动Nginx进行验证

```
systemctl restart nginx
```

## 八、验证证书配置成功. <a href="#anchor-h1-4" id="anchor-h1-4"></a>

打开浏览器  <https://zhiqiang.cloud/> 验证 HTTPS 证书

![](/files/-Meu8RxpF6_geUdZgtBO)

## 九、自动更新证书 <a href="#anchor-h1-5" id="anchor-h1-5"></a>

{% hint style="info" %}
ZeroSSL 的证书有效期为60天

目前手动添加DNS获取证书的方式无法自动更新，但是使用DNS API的方式进行获取证书可以在 60 天以后会自动更新, 你无需任何操作.
{% endhint %}

### 1、强制执行更新任务

```
acme.sh --cron -f
```

### 2、编写自动配置脚本 `vim ~/.acme.sh/auto-udate-ca.sh`

```
/root/.acme.sh/acme.sh --dns dns_dp --issue -d zhiqiang.cloud -d *.zhiqiang.cloud
cp ~/.acme.sh/zhiqiang.cloud/* /etc/nginx/conf.d/ssl/zhiqiang.cloud
systemctl restart nginx
```

```
[root@VM-0-4-centos ~]# chmod +x /root/.acme.sh/auto-udate-ca.sh
```

### 3、配置每3个月更新一次 `vim /etc/crontab`

```
* * * */3 * root /root/.acme.sh/auto-udate-ca.sh > /dev/null
```

## 十、acme升级

目前由于 acme 协议和 Let\`s CA 都在频繁的更新, 因此 acme.sh 也经常更新以保持同步.

升级 acme.sh 到最新版 :

```
acme.sh --upgrade
```

如果你不想手动升级, 可以开启自动升级:

```
acme.sh --upgrade --auto-upgrade
```

之后, acme.sh 就会自动保持更新了.

你也可以随时关闭自动更新:

```
acme.sh --upgrade --auto-upgrade 0
```

## 十一、故障排查

错误一：

![](/files/-MeufY808Pz5qLMsXIxq)

解决方案：

查询`libssl.so` 模块版本

```
ldconfig -v | grep libssl.so
```

#### 1. 新增 repo 源 <a href="#id-2.-e6-96-b0-e5-a2-9e-repo-e6-ba-90" id="id-2.-e6-96-b0-e5-a2-9e-repo-e6-ba-90"></a>

```
vim /etc/yum.repos.d/city-fan.repo
```

内容为：

```
[cityfan]
name=cityfan
baseurl=http://www.city-fan.org/ftp/contrib/yum-repo/rhel8/x86_64/
enabled=1
gpgcheck=0
```

#### 2. 更新 curl <a href="#id-3.-e6-9b-b4-e6-96-b0-curl" id="id-3.-e6-9b-b4-e6-96-b0-curl"></a>

```
dnf -y update curl libcurl
```

其它出错, 请添加 `debug log`：

```
acme.sh  --issue  .....  --debug 
```

或者：

```
acme.sh  --issue  .....  --debug  2
```

请参考：&#x20;

{% embed url="<https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh>" %}

本文并非完全的使用说明, 还有很多高级的功能, 更高级的用法请参看其他 wiki 页面.

{% embed url="<https://github.com/Neilpang/acme.sh/wiki>" %}


---

# 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://hezhiqiang.gitbook.io/about-the-author/linux-yun-wei-guan-li/linux-zi-dong-geng-xin-ssl-zheng-shu.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.
