使用 acme.sh 为网站生成永久免费证书

分类: 系统
日期: 2021-12-29
浏览: 27360

HTTP 2.0 时代几乎所有的网站都是 https 访问方式了,想要实现 https 访问,安全证书就是绕不过去的坎,域名服务商一般都会提供了免费证书注册,网上也可以搜索很多,常见的免费证书的颁发机构有 亚洲诚信Let's EncryptZoreSSL 等。关于免费证书的优缺点,我给分析了一下:

优点:

  • 免费

缺点:

  • 个数限制
  • 时间限制
  • 无法申请通配证书

只要有一个 免费 的优点就够了,缺点都是可以克服克服的😂。而克服这些缺点的英雄就是 acme.shacme.sh 实现了 acme 协议, 可以从 Let's EncryptZoreSSLbuypasssslcom 生成免费的证书。

1.安装 acme.sh

以下三种任选一种即可,把 my@example.com 修改成自己的邮箱。

安装过程不会污染任何功能和文件,所有的修改都限制在安装目录中:~/.acme.sh/

(1)通过 https://get.acme.sh 安装
curl https://get.acme.sh | sh -s email=my@example.com

或者

wget -O - https://get.acme.sh | sh -s email=my@example.com
(1)通过 GitHub 安装
curl https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m my@example.com

或者

wget -O - https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m my@example.com
(3)通过 git clone 安装
# 使用加速通道
git clone https://github.com.cnpmjs.org/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install -m my@example.com
安装过程进行了以下几步:

(1)acme.sh 安装到 home 目录下

~/.acme.sh/

(2)创建一个 bashalias

alias acme.sh=~/.acme.sh/acme.sh

(3)创建 cronjob,每天 0:00 自动检测所有证书,如果快过期了,会自动更新证书。

2.生成证书

以下两种任选一种即可,把 mydomain.com 更换成自己的域名。

如何生成泛域名证书,请参阅 使用 acme.sh 生成免费的泛域名证书

(1)http 方式

指定域名,并指定网站根目录。 acme.sh 会全自动生成验证文件,并放到网站的根目录,自动完成验证。最后会删除验证文件,整个过程没有任何副作用。

acme.sh --issue -d mydomain.com -d www.mydomain.com --webroot /home/wwwroot/mydomain.com/

如果使用 apache 服务器, acme.sh 可以智能的从 apache 的配置中自动完成验证,不需要指定网站根目录:

acme.sh --issue -d mydomain.com --apache

如果使用 nginx 服务器,或者反向代理,acme.sh 可以智能的从 nginx 的配置中自动完成验证,不需要指定网站根目录:

acme.sh --issue -d mydomain.com --nginx

注意!无论是 apache 还是 nginx 模式,acme.sh 不会自动修改配置文件,需要手动修改配置文件,否则无法访问 https

如果还没有运行任何 web 服务,80 端口是空闲的,那么 acme.sh 还能假装自己是一个 webserver,临时监听 80 端口,完成验证:

acme.sh --issue -d mydomain.com --standalone
(2)DNS 方式

好处:不需要任何服务器, 不需要任何公网 IP, 只需要 DNS 的解析记录即可完成验证。

坏处:如果不同时配置 Automatic DNS API 则 acme.sh 将无法自动更新证书。

acme.sh --issue --dns -d mydomain.com \
 --yes-I-know-dns-manual-mode-enough-go-ahead-please

acme.sh 会生成相应的解析记录,到域名解析中添加 TXT 记录,解析成功后,重新生成证书。

acme.sh --renew -d mydomain.com \
  --yes-I-know-dns-manual-mode-enough-go-ahead-please

DNS 方式的真正强大之处在于可以使用域名解析商提供的 api 自动添加 TXT 记录完成验证。

acme.sh 目前支持几十种域名服务商:dnsapi

以 dnspod 为例, 需要先登录到 dnspod 账号, 生成 api idapi key

export DP_Id="1234"

export DP_Key="qwertyuiopasdfghjkl"

acme.sh --issue --dns dns_dp -d mydomain.com -d www.mydomain.com

acme.sh 会自动生成证书,并且会记录 api idapi key 以后再使用 dnspod api 时就不需要再指定了。

3.复制/安装 证书

注意!默认生成的证书都放在安装目录下:~/.acme.sh/,不要直接使用此目录下的文件,这里面的文件都是内部使用, 而且目录结构可能会变化。

正确的使用方法是使用 --install-cert 命令,指定目标位置,证书文件会被复制到相应的位置。

Apache
acme.sh --install-cert -d example.com \
--cert-file      /path/to/certfile/in/apache/cert.pem  \
--key-file       /path/to/keyfile/in/apache/key.pem  \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd     "service apache2 force-reload"
Nginx
acme.sh --install-cert -d example.com \
--key-file       /path/to/keyfile/in/nginx/key.pem  \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd     "service nginx force-reload"

4.更新证书

证书在 60 天以后会自动更新,无需任何操作。

5.更新 acme.sh

升级 acme.sh 到最新版

acme.sh --upgrade

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

acme.sh  --upgrade  --auto-upgrade

关闭自动更新

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

6.出错怎么办

在命令后添加 --debug--debug 2 比如:

acme.sh --issue -d mydomain.com --nginx --debug 

# 或

acme.sh --issue -d mydomain.com --nginx --debug 2

或者查阅日志

~/.acme.sh/acme.sh.log
版权声明
作者:不二
来源:不二博客
文章版权归作者所有,未经允许请勿转载。
共有 14 条评论
DewittPes

AI Generated Content (AIGC), a new form of content creation after Professional-generated Content (PGC) and User-generated Content (UGC), is created using artificial intelligence to fully utilize its technical advantages in creativity, expression, iteration, dissemination, and personalization, and to create new forms of digital content generation and interaction. With the development of technology, AI writing, AI music composition, AI video generation, AI voice synthesis, and the recent trend of AI painting on the Internet have brought a wave of discussion to the creative field. With just a few keywords inputted, a painting can be generated within seconds.

2023-05-14 08:19:17 回复
Danielfer

第一借錢網擁有全台最多的借錢資訊

https://168cash.com.tw/

2023-05-18 02:26:37 回复
Williamaxole
2023-05-18 07:36:49 回复
FrancisRuilt
2023-05-18 13:22:10 回复
Georgebuiff
2023-05-19 04:13:40 回复
Alexisbaing
2023-05-20 04:11:36 回复
DouglasMen
2023-05-22 03:10:12 回复
LouisOpelf

https://94forfun.com

英雄聯盟世界大賽、線上電競投注

2023-05-23 04:25:21 回复
发表评论