lua-resty-acme: ACMEv2 客户端和 Let's Encrypt 证书的自动化管理

2019-09-21 07:06:12 +08:00
 fffonion

我又来骗星了 https://github.com/fffonion/lua-resty-acme

相比于现有实现,不需要安装任何第三方程序或者 C 扩展,所有依赖均为 Lua 和 FFI 实现。

安装

使用 opm:

opm install fffonion/lua-resty-acme

opm 中没有 luaossl 库,所以这种安装使用的是基于 FFI 的 Openssl 后端;需要 OpenResty 链接了大于等于 1.1 版本的 OpenSSL。

如果有完整的编译工具链,可以使用 luarocks 安装,需要安装 OpenSSL 的头文件:

luarocks install lua-resty-acme

使用

以 /etc/openresty 目录为例,如果目录不存在,请自行修改。

生成一个账户密钥

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out /etc/openresty/account.key

生成一个默认证书

openssl req -newkey rsa:2048 -nodes -keyout /etc/openresty/default.pem -x509 -days 365 -out /etc/openresty/default.key

在 Nginx 配置的 http 节插入以下内容

resolver 8.8.8.8;

lua_shared_dict acme 16m;
lua_shared_dict autossl_events 128k;

init_by_lua_block {
    require("resty.acme.autossl").init({
        -- setting the following to true
        -- implies that you read and accepted https://letsencrypt.org/repository/
        tos_accepted = true,
        -- uncomment following for first time setup
        -- staging = true,
        -- uncomment folloing to enable RSA + ECC double cert
        -- domain_key_types = { 'rsa', 'ecc' },
        account_key_path = "/etc/openresty/account.key",
        account_email = "此处填写邮箱",
    })
}
init_worker_by_lua_block {
    require("resty.acme.autossl").init_worker()
}

# required to verify Let's Encrypt API
lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
lua_ssl_verify_depth 2;

CentOS/Fedora 等系统的根证书在 /etc/ssl/certs/ca-bundle.crt ,请根据实际情况修改 lua_ssl_trusted_certificate。

首次配置时,建议将 init_by_lua_block 中的 staing = true 取消注释,以防错误过多触发限流;测试通过后再加回注释使用生产 API。

在需要使用证书的 server 节插入

server {
    server_name example.com;

    # fallback certs, make sure to create them before hand
    ssl_certificate /etc/openresty/default.pem;
    ssl_certificate_key /etc/openresty/default.key;

    ssl_certificate_by_lua_block {
        require("resty.acme.autossl").ssl_certificate()
    }

    location /.well-known {
        content_by_lua_block {
            require("resty.acme.autossl").serve_http_challenge()
        }
    }
}

配置完成后,reload nginx。

双证书配置见 https://yooooo.us/2019/lua-resty-acme

福利:

# RSA
echo q |openssl s_client -connect 随机子域名.acme.yooooo.us -port 8443 -cipher ECDHE-RSA-AES128-GCM-SHA256|openssl x509 -noout -text|grep -P "Public Key Algorithm|After"
# ECC
echo q |openssl s_client -connect 随机子域名.acme.yooooo.us -port 8443 -cipher ECDHE-ECDSA-AES128-GCM-SHA256|openssl x509 -noout -text|grep -P "Public Key Algorithm|After"

首次请求后等 10 秒钟,即可收到新证书加密的链接

欢迎讨论和提供建议!

3801 次点击
所在节点    分享创造
9 条回复
hanxiV2EX
2019-09-21 09:07:32 +08:00
不错的样子,star 了。
Remember
2019-09-21 09:08:42 +08:00
acme 了解一下,shell 的,搞了好几年了
noqwerty
2019-09-21 10:55:38 +08:00
一直在用 certbot,懒得手动弄这些了
fffonion
2019-09-21 11:49:46 +08:00
@noqwerty 这个比 certbot 更自动化,可以尝试一下 = )
Cbdy
2019-09-21 11:52:59 +08:00
目前用 acme.sh
littlespider89
2019-09-21 14:35:42 +08:00
k8s ingress + cert-manager, 真的香
justing
2019-09-23 08:06:33 +08:00
我也用的是 acme.sh ,配合 https://www.cloudflare.com 的 dns api,完全不用任何配置,无脑操作。
tufu9441
2019-09-24 08:52:28 +08:00
@justing 这个姿势真是非常舒适
tinypig
2022-09-19 21:44:46 +08:00
@fffonion 仓库文档里的 example config 似乎有点错误?

location = /.well-known { }
是不是应该修改为:
location ^~ /.well-known { }

如果用文档里的写法,Let's encrypt 会在校验域名请求时出现 404 错误。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/602718

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX