基于acme.sh从Let's encrypt生成免费且自动更新的SSL证书
0x01 安装 acme.sh
$ curl https://get.acme.sh | sh
# Or
$ wget -O - https://get.acme.sh | sh
执行上面的命令,它会:
从 GitHub 上下载 sh 脚本并执行
把文件解压到用户的 ~/.acme.sh目录下
给命令行设置一个acme.sh的 alias 别名
最后注册一个 cron 定时任务来自动更新证书。
安装完成后要自行重启命令行,或者重新加载一下.bashrc
文件(source ~/.bashrc
)。
然后看一下有没有生效。
$ acme.sh -h
https://github.com/acmesh-official/acme.sh
v2.8.6
Usage: acme.sh command ...[parameters]....
Commands:
--help, -h Show this help message.
--version, -v Show version info.
--install Install acme.sh to your system.
--uninstall Uninstall acme.sh, and uninstall the cron job.
--upgrade Upgrade acme.sh to the latest code from https://github.com/acmesh-official/acme.sh.
--issue Issue a cert.
--signcsr Issue a cert from an existing csr.
--deploy Deploy the cert to your server.
--install-cert Install the issued cert to apache/nginx or any other server.
0x02 签发 SSL 证书
签发 SSL
证书需要证明这个域名是属于你的,即域名所有权,一般有两种方式验证:http
和 dns
验证。
通过 acme.sh
可以签发单域名、多域名、泛域名证书,还可以签发 ECC 证书。为了简单起见,这里以单域名证书为例,后面再拓展一下好了。
下面任意一种方式只要安装成功了就行!
Standalone mode
这种方式下,acme.sh 会自己建立一个服务器来
完成签发。主要适合的是没有建立服务器的情况,不过其实有服务器的话只要暂时关闭,不造成端口冲突就能使用。
acme.sh --issue -d www.3pub.cn --standalone
签发 ECC
证书
默认签发的都是基于 RSA 密钥加密的证书,而 ECC (Elliptic Curve Cryptography, 椭圆曲线密码) 密钥的保密性比 RSA 更好,密钥长度更短,更能对抗量子解密等,目前现代的操作系统和浏览器都支持 ECC 证书了(Windows XP 及其之前的就算了)。Let's Encrypt
提供了 ECDSA
证书的签发,且 acme.sh
也支持。
我看网上的教程基本没讲 ECC 证书的签发,这里就来整一下呗!
其实只需要加上一个以 ec-
为前缀的 --keylength
参数(或 -k
)就可以了。理论上上面的各种验证方式都适用。
acme.sh --issue -w /opt/webserver/typecho -d www.3pub.cn -k ec-256 --force
支持以下长度的证书,一般就用 ec-256
就行了。
创建 SSL 目录 将生成的key
和 cer
, cp 至 新建目录
mkdir -p /usr/local/nginx/ssl
chmod -R 777 /usr/local/nginx/ssl
cp /root/.acme.sh/www.3pub.cn_ecc/www.3pub.cn.key /usr/local/nginx/ssl/3pub.key
cp /root/.acme.sh/www.3pub.cn_ecc/www.3pub.cn.cer /usr/local/nginx/ssl/3pub.cer
vim /usr/local/nginx/conf/conf.d/typecho.conf
修改 nginx 配置
server
{
listen 80;
server_name www.3pub.cn;
# 重定向到https
rewrite ^(.*)$ https://$host$request_uri;
}
server {
listen 443;
server_name www.3pub.cn;
set $root /typecho;
root $root;
ssl on;
ssl_certificate /usr/local/nginx/ssl/3pub.cer;
ssl_certificate_key /usr/local/nginx/ssl/3pub.key;
# 下面直接照写
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
root $root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
很无语 通过 占用 80端口 启动服务 自己检测通过
acme.sh --issue -d www.3pub.cn --standalone --force
[root@xxx ~]# acme.sh --issue -d www.3pub.cn --standalone --force
[Mon Feb 19 11:21:32 CST 2024] Using CA: https://acme.zerossl.com/v2/DV90
[Mon Feb 19 11:21:32 CST 2024] Standalone mode.
[Mon Feb 19 11:21:32 CST 2024] LISTEN 0 128 *:80 *:* users:(("nginx",pid=21719,fd=15),("nginx",pid=8041,fd=15))
[Mon Feb 19 11:21:32 CST 2024] tcp port 80 is already used by (("nginx",pid=21719,fd=15),("nginx",pid=8041,fd=15))
[Mon Feb 19 11:21:32 CST 2024] Please stop it first
[Mon Feb 19 11:21:32 CST 2024] _on_before_issue.
如果用了反代之类的不是 80 端口,则可以手动指定。
acme.sh --issue -d www.3pub.cn --standalone --force --httpport 88
[Mon Feb 19 11:24:20 CST 2024] get authz objec with invalid status, please try again later.
[Mon Feb 19 11:24:20 CST 2024] _authorizations_seg='https://acme.zerossl.com/v2/DV90/authz/nzrGorBqTconk20mHFUZ2g'
[Mon Feb 19 11:24:20 CST 2024] {"identifier":{"type":"dns","value":"www.3pub.cn"},"status":"invalid","expires":"2024-03-20T01:47:24Z","challenges":[{"type":"http-01","url":"https://acme.zerossl.com/v2/DV90/chall/61vb2C969GYiHZP06eZuHg","status":"invalid","error":{},"token":"ehiTpZgfvtlh5jzWAJXkpsnjaElut_1KKlh9tcTSboQ"}]}
[Mon Feb 19 11:24:20 CST 2024] Please add '--debug' or '--log' to check more details.
[Mon Feb 19 11:24:20 CST 2024] See: https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh
自动更新并没研究米哦你告白
参考文献 :
https://miaotony.xyz/2020/03/28/Server_IssueACertWithACME/#toc-heading-14
https://moe.best/tutorial/acme-le-wc.html
https://juejin.cn/post/7136001093720342542