1987WEB视界-分享互联网热点话题和事件

您现在的位置是:首页 > 域名 > 正文

域名

还在为买SSL证书发愁?看看这个免费SSL证书生成器

1987web2023-09-27域名71

[TOC]

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

  • letsencrypt.org/zh-cn/docs/

客户端

Let’s Encrypt 使用 ACME 协议来验证您对给定域名的控制权并向您颁发证书。要获得 Let’s Encrypt 证书,您需要选择一个要使用的 ACME 客户端软件。Let’s Encrypt 不控制或审查第三方客户端,也不能保证其安全性或可靠性。

官方提供了几种证书的申请方式方法

  • letsencrypt.org/zh-cn/docs/…

certbot

它既可以仅为您获取证书,也可以帮助您安装证书(如果您需要的话)。它易于使用,适用于许多操作系统,并且具有出色的文档。

certbot.eff.org/

acme.sh

目前 Lets Encrypt 免费证书客户端最简单、最智能的 shell 脚本,可以自动发布和续订 Lets Encrypt 中的免费证书

  • github.com/acmesh-offi…

安装acme.sh

1. 自动安装

curl https://get.acme.sh | sh (网络问题可能失败)

2. 手动安装

gitclonehttps://github.com/acmesh-official/acme.sh.gitcd./acme.sh
./acme.sh --install
复制代码

安装过程如下:

  1. 默认安装到当前用户的主目录$HOME下的.acme.sh文件夹中,即~/.acme.sh/,之后所有生成的证书也会按照域名放在这个目录下;
  2. 创建指令别名: alias acme.sh=~/.acme.sh/acme.sh, 通过acme.sh命令方便快速地使用 acme.sh 脚本
  3. 自动创建cronjob定时任务,每天 0:00 点自动检测所有的证书,如果快过期了,则会自动更新证书 每天 0:00 点自动检测所有的证书, 如果快过期了, 需要更新, 则会自动更新证书 0 0 * * * /root/.acme.sh/acme.sh --cron --home /root/.acme.sh > /dev/null 复制代码

3. 测试收否安装成功

user@owen-ubuntu:~$ acme.sh --versionhttps://github.com/acmesh-official/acme.sh
v2.8.8复制代码

如有版本信息输出则表示环境正常;如果提示命令未找到,执行source ~/.bashrc命令重载一下环境配置文件。

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

使用acme.sh生成证书

1. HTTP 方式

http 方式需要在你的网站根目录下放置一个文件, 以此来验证你的域名所有权,完成验证,只需要指定域名, 并指定域名所在的网站根目录,acme.sh 会全自动的生成验证文件, 并放到网站的根目录, 然后自动完成验证,该方式较适合独立域名的站点使用,比如博客站点等

./acme.sh  --issue  -d mydomain.com -d www.mydomain.com  --webroot  /home/wwwroot/mydomain.com/- issue 是acme.sh脚本用来颁发证书的指令;- d是 --domain的简称,其后面须填写已备案的域名;- w是 --webroot的简称,其后面须填写网站的根目录。复制代码

示例:

./acme.sh --issue -d devopsing.site -d www.devopsing.site --webroot /var/www/html/blog/

证书签发成功会有如下输出:

执行成功,默认为生成如下证书:

root@ecs-ubuntu18:/etc/nginx/sites-availablels~/.acme.sh/devopsing.site/-ltotal28-rw-r--r--1rootroot1587Dec1612:34ca.cer-rw-r--r--1rootroot1866Dec1612:34devopsing.site.cer-rw-r--r--1rootroot642Dec1612:34devopsing.site.conf-rw-r--r--1rootroot1001Dec1612:33devopsing.site.csr-rw-r--r--1rootroot232Dec1612:33devopsing.site.csr.conf-rw-r--r--1rootroot1679Dec1612:33devopsing.site.key-rw-r--r--1rootroot3453Dec1612:34fullchain.cer复制代码

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

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

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

2. DNS 方式

适合用于生成范解析证书

优势:不需要任何服务器, 不需要任何公网 ip, 只需要 dns 的解析记录即可完成验证 劣势:如果不同时配置 Automatic DNS API,使用这种方式 acme.sh 将无法自动更新证书,每次都需要手动再次重新解析验证域名所有权`

1. 生成证书记录

  • github.com/acmesh-offi…

注意,第一次执行时使用 --issue,-d 指定需要生成证书的域名

./acme.sh --issue -d *.example.com --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please

参数解释:

--dns[dns_hook]Usednsmanualmodeordnsapi.Defaultstomanualmodewhenargument is omitted.

--yes-I-know-dns-manual-mode-enough-go-ahead-please  Force use of dns manual mode.See:https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode复制代码

2. 在域名解析中手动添加TXT记录

如果第一次添加该域名,会提示如下信息,需要在DNS解析中添加TXT记录,用作判断你是否拥有域名使用权

[WedDec1616:04:49CST2020]Add the following TXT record:[WedDec1616:04:49CST2020]Domain:_acme-challenge.devopsing.site[WedDec1616:04:49CST2020]TXT value:-jEWdpI**************EVh01_a3ywrW426wmppjuDqXOs[WedDec1616:04:49CST2020]Pleasebeawarethatyouprepend_acme-challenge.beforeyourdomain[WedDec1616:04:49CST2020]so the resulting subdomain will be:_acme-challenge.devopsing.site[WedDec1616:04:49CST2020]PleaseaddtheTXTrecordstothedomains,andre-runwith--renew.[WedDec1616:04:49CST2020]Pleaseadd--debugor--logtocheckmoredetails.[WedDec1616:04:49CST2020]See:https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh复制代码

验证解析生效

user@owen-ubuntu:~$nslookup-q=TXT_acme-challenge.devopsing.siteServer:127.0.0.53Address:127.0.0.5353Non-authoritative answer:_acme-challenge.devopsing.sitetext="-jEWdpI****************1_a3ywrW426wmppjuDqXOs"Authoritative answers can be found from:复制代码

3. 重新生成证书

注意,这里第二次执行是用的是 --renew

./acme.sh --renew -d *.example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please

示例:

./acme.sh --issue -d *.devopsing.site --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please

root@ecs-ubuntu18:/var/log/nginxls~/.acme.sh/\*.devopsing.site/-ltotal28-rw-r--r--1rootroot1587Dec1616:09ca.cer-rw-r--r--1rootroot1846Dec1616:09*.devopsing.site.cer-rw-r--r--1rootroot613Dec1616:09*.devopsing.site.conf-rw-r--r--1rootroot980Dec1616:09*.devopsing.site.csr-rw-r--r--1rootroot211Dec1616:09*.devopsing.site.csr.conf-rw-r--r--1rootroot1679Dec1616:04*.devopsing.site.key-rw-r--r--1rootroot3433Dec1616:09fullchain.cer复制代码

4. 使用DNS API的模式进行证书申请 (支持自动更新)

dns 方式的真正强大之处在于可以使用域名解析商提供的 api 自动添加 txt 记录完成验证,acme.sh 目前支持 cloudflare, dnspod, cloudxns, godaddy 以及 ovh 等数十种解析商的自动集成。

  • github.com/acmesh-offi…

阿里云DNS API

首先获取阿里云的操作API 的 AccessKey ID和AccessKey Secret

exportAli_Key="key值"exportAli_Secret="key Secret"给出的 api id 和 api key 会被自动记录下,下次就不用再次执行上述命令acme.sh --issue --dns dns_ali -d *.example.com --force
复制代码

示例:

exportAli_Key="LTAI4F****i8qEeKeRios2r"exportAli_Secret="nIpymix0s****a0bJNgERE0QzjSrkF"acme.sh --issue --dns dns_ali -d *.devopsing.site --force
复制代码

DnsPod API

exportDP_Id="1234"exportDP_Key="sADDsdasdgdsf"acme.sh --issue  --dns dns_dp   -d *.example.com
复制代码

查看/删除证书

查看安装证书 acme.sh --list

删除证书 acme.sh remove

user@ecs-ubuntu18:~$ acme.shremovedevopsing.site
[Thu Dec 17 14:05:53 CST 2020] devopsing.siteisremoved, the key and cert files arein/home/user/.acme.sh/devopsing.site
[Thu Dec 17 14:05:53 CST 2020] You canremovethembyyourself.
复制代码

使用acme.sh安装证书

上面生成的证书放在了~/.acem.sh/目录,使用--installcert命令,指定目标位置,可将证书copy 到相应的位置

Nginx 示例

acme.sh--installcert -d .com \--key-file /etc/nginx/ssl/.key \--fullchain-file /etc/nginx/ssl/fullchain.cer \--reloadcmd "service nginx force-reload"复制代码

如果要直接加载配置,可以使用 --reloadcmd "service nginx force-reload",但是由于nginx 的配置可能不尽相同,所以一般选择手动 reload nginx

注意:Nginx 的配置 ssl_certificate 使用/etc/nginx/ssl/fullchain.cer ,而非 /etc/nginx/ssl/.cer ,否则 SSL Labs 的测试会报 Chain issues Incomplete 错误

server{listen443 ssl;server_namedemo.com;sslon;ssl_certificate/etc/nginx/ssl/fullchain.cer;ssl_certificate_key/etc/nginx/ssl/.key;复制代码

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"

更新证书

目前 Lets Encrypt 的证书有效期是90天,时间到了会自动更新,无需任何操作。但是,也可以强制续签证书:

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

注:手动添加DNS获取证书的方式无法自动更新,但是使用DNS API的方式进行获取证书可以在证书有效期后自动更新, 你无需任何操作

强制执行更新任务 acme.sh --cron -f

更新acme.sh

acme 协议和 letsencrypt CA 都在频繁的更新, 因此 acme.sh 也经常更新以保持同步。

手动更新: acme.sh --upgrade

开启自动更新:acme.sh --upgrade --auto-upgrade

取消自动更新: acme.sh --upgrade --auto-upgrade 0

删除acme.sh

user@owen-ubuntu:~$acme.sh--uninstall[2020年1218星期五15:55:11CST]Removingcronjob[2020年1218星期五15:55:11CST]LE_WORKING_DIR=/home/user/.acme.sh[2020年1218星期五15:55:11CST]Uninstalling alias from:/home/user/.bashrc[2020年1218星期五15:55:11CST]Thekeysandcertsarein"/home/user/.acme.sh",youcanremovethembyyourself.复制代码