跳至主要內容

ssl证书

wangdx大约 7 分钟

参考网址

手动申请 Let's Encrypt 通配符证书open in new window

手动申请 Let's Encrypt 通配符证书open in new window

https://github.com/joohoi/acme-dns-certbot-joohoi/tree/master
2.
通过 Certbot 申请泛域名 HTTPS 证书及配置自动更新
https://www.jianshu.com/p/034b891cf395

安装和自动更新

1.添加库和安装
sudo apt-add-repository ppa:certbot/certbot
sudo apt install certbot
certbot --version
2. acme dns certbot
curl -o /etc/letsencrypt/acme-dns-auth.py https://raw.githubusercontent.com/joohoi/acme-dns-certbot-joohoi/master/acme-dns-auth.py
chmod 0700 /etc/letsencrypt/acme-dns-auth.py
# 修改文件 在第一行末尾添加3:
vim /etc/letsencrypt/acme-dns-auth.py


#!/usr/bin/env python3
. . .

3. 生成证书
sudo certbot certonly --manual --manual-auth-hook /etc/letsencrypt/acme-dns-auth.py --preferred-challenges dns --debug-challenges -d \*.your-domain -d your-domain
4.自动更新
sudo certbot renew
/etc/letsencrypt/renewal/dada.fun.conf


# renew_before_expiry = 30 days
version = 0.28.0
archive_dir = /etc/letsencrypt/archive/dada.fun
cert = /etc/letsencrypt/live/dada.fun/cert.pem
privkey = /etc/letsencrypt/live/dada.fun/privkey.pem
chain = /etc/letsencrypt/live/dada.fun/chain.pem
fullchain = /etc/letsencrypt/live/dada.fun/fullchain.pem

# Options used in the renewal process
[renewalparams]
manual_public_ip_logging_ok = True
manual_auth_hook = /root/certbot-wildcard/au.sh
server = https://acme-v02.api.letsencrypt.org/directory
pref_challs = dns-01,
authenticator = manual
account = 6656c0feac6c44e53d8b89e9afb6712b
pre_hook = systemctl stop nginx.service
post_hook = systemctl start nginx.service

5.注释定时任务 我们选择 timers 来自动更新,所以把 cron 任务注释掉:
$ vim /etc/cron.d/certbot
6.
#Certbot 在 /lib/systemd/system/ 下生成了两个文件:certbot.service 和 certbot.timer,一个是服务,一个是定时器。
每天 0 点和 12 点激活 certbot.service。其实不需要这么频繁的更新证书,而且在更新证书的时候我们加了钩子来关闭和开启 Nginx 服务,所以最好是在凌晨没人访问网站的时候更新证书,我们稍微修改一下:

[Unit]
Description=Run certbot every 05:00

[Timer]
OnCalendar=*-*-* 05:00:00
Persistent=true

[Install]
WantedBy=timers.target

7.
systemctl daemon-reload
systemctl restart certbot.timer

Certbot 申请证书

#1.安装certbot 方式二:sudo snap install --classic certbot  sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo apt -y install certbot python3-certbot-nginx

#2.运行证书申请命令
certbot certonly --manual --preferred-challenges dns -d wyix.top -d \*h.wyix.top:558
#3.证书更新
#创建脚本
....au.sh
echo "recode:"$CERTBOT_VALIDATION
curl -k https://dnsapi.cn/Record.Modify -d "login_email=${ACCOUNT}&login_password=${PASSWORD}&domain_id=${DOMAIN_ID}&record_id=${REC_BBS}&sub_domain=_acme-challenge.${RECORD}&record_line=默认&record_type=TXT&value=$CERTBOT_VALIDATION"
sleep 20
....
#关于如何查询domain_id 和record_id
#获得域名id
curl -k https://dnsapi.cn/Domain.List -d "login_email=${ACCOUNT}&login_password=${PASSWORD}"
#获得域名id下的记录列表
curl -k https://dnsapi.cn/Record.List -d "login_email=${ACCOUNT}&login_password=${PASSWORD}&domain_id=${DOMAIN_ID}"




#3.卸载certbot
sudo apt -y remove certbot python3-certbot-nginx





certbot certonly --manual --preferred-challenges dns -d wyix.top -d *h.wyix.top:558

添加 TXT

Certbot 自动更新续期 HTTPS 证书详解

1.certbot certificates 查看当前的所有的证书信息,会得到如下信息,其中 Expiry Date 就是有效期。

默认的情况,证书到期前 30 天,执行 certbot renew 证书才会更新,每个证书有个配置文件。默认在文件夹 /etc/letsencrypt/renewal 下,有每个证书的信息。

cat /etc/letsencrypt/renewal/www.liuhaolin.com.conf

  1. 定时任务自动更新

光能更新证书还不够,谁吃饱没事每三个月登录一次服务器更新证书呀。在服务器上设置定时任务自动更新才是完美的方案。Linux 的定时任务有两种方法: cron 和 systemd/timers,任何一种都能实现定时的更新任务,可爱的 Certbot 两种都设置了,我们分别看看。

Cron


vim /etc/cron.d/certbot

# 每月的 1,7,21,28号, 4点30 更新证书
30 4 1,7,21,28 * * /usr/bin/certbot renew
# 每月的 1,7,21,28号, 5点30 重新启动nginx 服务器
30 5 1,7,21,28 * * systemctl reload nginx

#打开文件可以看到 Certbot 设定的任务记录:
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew
#每天运行两次 certbot -q renew,-q 是安静模式,只把 error 写入日志。我们选择 timers 来自动更新,所以把 cron 任务注释掉:
#0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

Timers

Certbot 在 /etc/systemd/system/ 下生成了两个文件:certbot.service 和 certbot.timer,一个是服务,一个是定时器。

最终简化

root用户下执行
安装有certbot
在systemd服务(现在服务器一般都在,相确认下的话type systemctl)
可将命令简化如下:
certbot renew --renew-hook "systemctl reload nginx"

对证书进行自动更新

使用 crontab -e 增加两条定时任务

20 4 21 * * /home/certbot-auto renew -v
40 4 21 * * /usr/sbin/nginx -t && killall nginx && /usr/sbin/nginx
详情

自动更新命令详解 官方给出的命令如下:

root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew --renew-hook "systemctl reload nginx"

命令说明:
命令意为: 以root权限执行命令,如果certbot命令存在且可执行,并且/run/systemd/system目录不存在,那么sleep一个随机时间后,更新证书(certbot renew),执行后通过金子调用命令:systemctl reload nginx

root以root权限执行
test -x: 判断文件是否存在且可执行
-a: test命令的参数,逻辑与
\\!: test命令的参数,逻辑非
-d: 目录是否存在
&&: 前面的命令执行成功,才执行后面的命令
perl xxx: 随机休眠
certbot -q: 不输出执行结果日志
certbot -q renew: 执行HTTPS证书更新
--renew-hook: 如果执行成功的话执行钩子指定的命令
把-q去掉后有两种结果:

执行成功(日志没有抓到)

你执行得太频繁了,服务端不给你更新证书,一般30天才给你更新证书

日志类似如下:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/caowd.com.conf
​- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/caowd.com/fullchain.pem expires on 2023-11-29 (skipped)
No renewals were attempted.
No hooks were run.
​- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


这个命令我奇怪的一点是: ! -d /run/systemd/system

这一句的意思是: 检测 /run/systemd/system 目录是否存在,不存在为真, 此时才执行后面的命令

如果 systemd 服务不存在才执行,明显不太对

另外一个奇怪的地方是: perl -e 'sleep int(rand(43200))'

shell 有直接 sleep 的命令,等价的命令是: sleep $(($RANDOM % 43200))

为什么不直接用这个, 而且为什么要 sleep

教你秒建受信任的本地 SSL 证书,彻底解决开发测试环境的无效证书警告烦恼!

您是否厌倦了在本地开发项目中使用不受信任的 SSL 证书?维护自己的证书颁发机构(CA)是一个痛苦的事情,这过程中需要用到神秘的程序和命令。在本指南中,我将向您展示一种在没有 CA 的情况下在本地开发计算机上使用受信任 SSL 证书的简单方法。

mkcert 是一个简单的零配置工具,由 Filippo Valsorda 使用 Go 编写,用于制作具有您喜欢的任何名称的本地可信开发证书,无需任何配置。这将对您有所帮助,因为无法从可信证书颁发机构获取没有有效 DNS 记录的本地名称的证书。让我们深入研究安装和使用 mkcert。

项目地址:https://github.com/FiloSottile/mkcert

# 1.如何在 Ubuntu / Debian 上安装 mkcert
#要在任何 Ubuntu 或 Debian 系统上安装 mkcert,首先要安装 certutil 依赖项:
apt-get updatesudo apt install wget libnss3-tools
#安装完成后,下载 mkcert 来自 Github 的二进制包。检查 mkcert 发布页面以获取最新版本。在撰写本文时,最新版本是。v1.4.4
#export VER="v1.4.4" && wget -O mkcert https://github.com/FiloSottile/mkcert/releases/download/${VER}/mkcert-${VER}-linux-amd64

curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64"
chmod +x mkcert-v*-linux-amd64
sudo cp mkcert-v*-linux-amd64 /usr/local/bin/mkcert

mkcert -version

如何使用 mkcert 生成本地受信任的 SSL 证书

#1.您需要首先在系统信任库中安装本地 CA.
mkcert -install
#2.完成后,您可以开始为您的域名生成 SSL 证书。例如,我将生成一个对以下名称有效的新证书。
"mydomain.com"
"*.mydomain.co"
"myapp.net"
"localhost"
"127.0.0.1"
"::1"
#你可以使用如下命令对上面的域名生成证书。
mkcert wyix.com '*.wyix.com' myapp.net localhost 127.0.0.1 ::1
#由于服务器上已经装了nginx,命令中可以加入 -cert-file 和 -key-file 参数,将文件直接生成到对应的目录里:
mkcert -cert-file /mnt/resource/ssl/server.crt -key-file /mnt/resource/ssl/server.key wyix.com '*.wyix.com' yix.top '*.yix.top' 192.168.16.8 127.0.0.1 ::1 localhost
#mkcert的认证机构安装到服务器上
mkcert -install
#查看CA证书的位置
mkcert -CAROOT

该目录中有两个文件:rootCA-key.pem 和 rootCA.pem。将 rootCA.pem 复制到 PC 上,并将其后缀改为 .crt。

双击 rootCA.crt,根据提示安装证书,步骤如下:

测试 mkcert 证书

# nginx.conf

Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/wyix.online/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/wyix.online/privkey.pem
This certificate expires on 2024-05-21.
These files will be updated when the certificate renews.

NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.
We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

certbot certonly  --test-cert -d h.wyix.top --manual --preferred-challenges dns


_acme-challenge         TXT     "AOuRcwkvyyKOT_ZKQs8xrrEVGxs1AAOGMqwdvnj0ERs"

dig _acme-challenge.xxx.xxx txt

ll /etc/letsencrypt/live/xxx.xxx/

ll /etc/letsencrypt/archive/xxx.xxx/

certbot certonly --manual --email xxx@xxxx.xxx --preferred-challenges  dns -d *.xxx.xxx
上次编辑于: