logo
0
0
WeChat Login
docs: 添加 OpenResty 本地安装文档和脚本

OpenResty Docker 镜像

项目简介

本项目提供了 OpenResty 的两种使用方式:

  1. Docker 镜像:基于 Alpine Linux 的 OpenResty Docker 镜像,包含了完整的 OpenResty 环境和配置
  2. 本地安装:支持在 Ubuntu/Debian/CentOS/RHEL 系统上一键安装 OpenResty

快速开始

构建镜像

docker build -t docker.cnb.cool/zhiqiangwang/openresty:latest .

运行容器

docker run -d -p 80:80 --name openresty docker.cnb.cool/zhiqiangwang/openresty:latest

配置说明

主配置文件

  • nginx.conf - 主 Nginx 配置文件,定义了全局配置和包含了 conf.d 目录下的所有配置文件

站点配置

  • conf.d/default.conf - 默认站点配置,监听 80 端口,根目录为 html

环境变量

环境变量说明默认值
NGINX_CONF自定义 Nginx 配置文件路径或 URL
PATH系统路径,包含 OpenResty 相关目录已配置
LUA_PATHLua 模块搜索路径已配置
LUA_CPATHLua C 模块搜索路径已配置

自定义配置

方法 1:使用本地配置文件

docker run -d -p 80:80 -v /path/to/nginx.conf:/etc/nginx/nginx.conf -e NGINX_CONF=/etc/nginx/nginx.conf docker.cnb.cool/zhiqiangwang/openresty:latest

方法 2:使用远程配置文件

docker run -d -p 80:80 -e NGINX_CONF=https://example.com/nginx.conf docker.cnb.cool/zhiqiangwang/openresty:latest

持久化

持久化静态文件

docker run -d -p 80:80 -v /path/to/html:/usr/local/openresty/nginx/html docker.cnb.cool/zhiqiangwang/openresty:latest

持久化配置文件

docker run -d -p 80:80 -v /path/to/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf -v /path/to/conf.d:/usr/local/openresty/nginx/conf/conf.d docker.cnb.cool/zhiqiangwang/openresty:latest

构建详情

  • 基础镜像:Alpine 3.15
  • OpenResty 版本:最新版(从 OpenResty 官方源安装)
  • 包含的依赖:curl, wget, ca-certificates, apache2-utils
  • 日志重定向:标准输出和标准错误

基本认证配置

本镜像包含了 apache2-utils 包,其中包含了 htpasswd 命令,可用于创建基本认证的密码文件。

创建密码文件

# 在容器中创建密码文件 docker exec -it openresty htpasswd -bc /usr/local/openresty/nginx/conf/.htpasswd admin admin # 或者在宿主机创建密码文件,然后挂载到容器 touch .htpasswd htpasswd -bc .htpasswd admin admin docker run -d -p 80:80 -v $(pwd)/.htpasswd:/usr/local/openresty/nginx/conf/.htpasswd docker.cnb.cool/zhiqiangwang/openresty:latest

配置基本认证

在 Nginx 配置文件中添加以下配置,启用基本认证:

server { listen 80; server_name localhost; # 启用基本认证 auth_basic "Restricted Area"; auth_basic_user_file /usr/local/openresty/nginx/conf/.htpasswd; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

使用基本认证运行容器

docker run -d -p 80:80 -v $(pwd)/.htpasswd:/usr/local/openresty/nginx/conf/.htpasswd -v $(pwd)/default.conf:/usr/local/openresty/nginx/conf/conf.d/default.conf docker.cnb.cool/zhiqiangwang/openresty:latest

运行示例

基本运行

docker run -d -p 80:80 docker.cnb.cool/zhiqiangwang/openresty:latest

自定义端口

docker run -d -p 8080:80 docker.cnb.cool/zhiqiangwang/openresty:latest

挂载卷

docker run -d -p 80:80 -v $(pwd)/html:/usr/local/openresty/nginx/html docker.cnb.cool/zhiqiangwang/openresty:latest

使用自定义配置

docker run -d -p 80:80 -e NGINX_CONF=https://raw.githubusercontent.com/example/nginx-config/master/nginx.conf docker.cnb.cool/zhiqiangwang/openresty:latest

本地安装

快速安装

支持一键安装 OpenResty,兼容 Ubuntu、Debian、CentOS 和 RHEL 系统。

curl -o- https://cnb.cool/zhiqiangwang/openresty/-/git/raw/main/install.sh | bash

支持的系统

系统版本架构
Ubuntu18.04 及以上amd64, arm64
Debian10 及以上amd64, arm64
CentOS7 及以上amd64, arm64
RHEL7 及以上amd64, arm64

安装脚本功能

安装脚本会自动完成以下操作:

  1. 系统检测:自动识别操作系统类型、版本和 CPU 架构
  2. 依赖安装:安装必要的依赖包(wget、gnupg、ca-certificates 等)
  3. GPG 公钥导入
    • 对于 Ubuntu 22+ / Debian 12+:使用新的 GPG 公钥方式
    • 对于旧版本:使用传统的 apt-key 方式
  4. 软件源添加:根据系统和架构添加对应的 OpenResty 官方源
  5. 软件安装:自动更新包列表并安装最新版本的 OpenResty

手动使用安装脚本

如果您想先下载脚本再运行:

# 下载脚本 wget https://cnb.cool/zhiqiangwang/openresty/-/git/raw/main/install.sh # 添加执行权限 chmod +x install.sh # 运行脚本 sudo ./install.sh

安装后的验证

# 查看 OpenResty 版本 openresty -v # 启动 OpenResty sudo systemctl start openresty # 设置开机自启 sudo systemctl enable openresty # 查看 OpenResty 状态 sudo systemctl status openresty

目录结构

安装完成后,OpenResty 的主要目录结构:

目录说明
/usr/local/openresty/OpenResty 安装根目录
/usr/local/openresty/nginx/Nginx 相关文件
/usr/local/openresty/nginx/conf/配置文件目录
/usr/local/openresty/nginx/html/默认网站根目录
/usr/local/openresty/nginx/logs/日志文件目录
/usr/local/openresty/lualib/Lua 库目录

常用命令

# 启动 OpenResty sudo openresty # 停止 OpenResty sudo openresty -s stop # 重启 OpenResty sudo openresty -s reload # 测试配置文件 sudo openresty -t # 查看版本信息 openresty -v

故障排除

1. 如果安装过程中出现 GPG 公钥错误

# 手动导入 GPG 公钥 wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/openresty.gpg

2. 如果软件源添加失败

# Ubuntu/Debian - 手动添加源 sudo tee /etc/apt/sources.list.d/openresty.list <<EOF deb http://openresty.org/package/ubuntu $(lsb_release -sc) main EOF # CentOS/RHEL - 手动添加源 sudo wget -O /etc/yum.repos.d/openresty.repo https://openresty.org/package/centos/openresty.repo

3. 如果服务无法启动

# 检查配置文件语法 sudo openresty -t # 查看错误日志 sudo tail -f /usr/local/openresty/nginx/logs/error.log

卸载

如需卸载 OpenResty:

# Ubuntu/Debian sudo apt-get remove openresty # CentOS/RHEL sudo yum remove openresty

脚本详细信息

  • 脚本名称:install.sh
  • 作者:zhiqiang
  • 最后更新:2025-05-15
  • 功能:支持多系统、多架构的一键安装