安装OpenResty

一、前言:什么是 OpenResty?

你是否听说过:

  • Nginx + Lua 实现 API 网关?
  • Nginx 层直接调用 Redis/MySQL
  • 单机扛住 百万级 QPS 的高并发系统?

OpenResty® 就是实现这些能力的"秘密武器"!

它是一个基于 Nginx + LuaJIT 的高性能 Web 平台,由国内开发者章亦春(agentzh)主导开发,被广泛应用于:

  • API 网关(如 Kong 底层)
  • 动态限流、鉴权
  • WAF(Web 应用防火墙)
  • 高性能反向代理

本文将手把手教你在 Linux 上安装 OpenResty,并验证是否成功!


二、准备工作:系统要求

OpenResty 支持主流 Linux 发行版,推荐使用:

  • ✅ CentOS 7/8/9
  • ✅ Ubuntu 20.04/22.04
  • ✅ Debian 10+

⚠️ 注意不要与已安装的 Nginx 冲突!建议卸载系统自带 Nginx。

检查并卸载旧版 Nginx(可选)

bash 复制代码
# CentOS/RHEL
sudo yum remove nginx

# Ubuntu/Debian
sudo apt remove nginx nginx-common

三、方式一:官方推荐 ------ 使用包管理器安装(推荐!)

3.1 CentOS / RHEL 安装

bash 复制代码
# 1. 添加 OpenResty 官方仓库
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

# 2. 安装 OpenResty
sudo yum install -y openresty

# 3. 安装命令行工具(可选)
sudo yum install -y openresty-resty

3.2 Ubuntu / Debian 安装

bash 复制代码
# 1. 导入 GPG 公钥
wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/openresty.gpg

# 2. 添加 APT 仓库
echo "deb [signed-by=/usr/share/keyrings/openresty.gpg] http://openresty.org/package/debian $(lsb_release -sc) openresty" \
    | sudo tee /etc/apt/sources.list.d/openresty.list

# 3. 更新并安装
sudo apt update
sudo apt install -y openresty

优势:自动解决依赖、一键升级、路径规范。


四、方式二:源码编译安装(高级用户)

适用于需要自定义模块或最新版本的场景。

4.1 安装依赖

bash 复制代码
# CentOS
sudo yum install -y gcc make git pcre-devel openssl-devel

# Ubuntu
sudo apt install -y build-essential git libpcre3-dev libssl-dev

4.2 下载并编译

bash 复制代码
# 1. 下载源码(以 1.25.3.1 为例)
wget https://openresty.org/download/openresty-1.25.3.1.tar.gz
tar -zxvf openresty-1.25.3.1.tar.gz
cd openresty-1.25.3.1

# 2. 配置(默认安装到 /usr/local/openresty)
./configure

# 3. 编译 & 安装
make -j$(nproc)
sudo make install

📌 默认安装路径:/usr/local/openresty/


五、验证安装是否成功

5.1 启动 OpenResty

bash 复制代码
# 包管理安装(systemd)
sudo systemctl start openresty
sudo systemctl enable openresty  # 开机自启

# 源码安装
sudo /usr/local/openresty/nginx/sbin/nginx

5.2 检查进程

bash 复制代码
ps aux | grep nginx
# 应看到 master 和 worker 进程

5.3 访问默认页面

bash 复制代码
curl http://localhost

若返回 Welcome to OpenResty!,说明安装成功!

🔍 默认网页路径:/usr/local/openresty/nginx/html/index.html(源码安装)

/usr/share/openresty/html/index.html(包管理安装)


六、常用目录结构(包管理安装)

路径 说明
/usr/bin/openresty 主程序(软链接到 nginx)
/etc/openresty/ 配置文件目录
/usr/share/openresty/ 默认 HTML、Lua 脚本等
/var/log/openresty/ 日志目录

💡 提示 :配置文件主入口为 /etc/openresty/nginx.conf


七、第一个 Lua 脚本:Hello World!

编辑配置文件:

bash 复制代码
sudo vim /etc/openresty/nginx.conf

server 块中添加:

XML 复制代码
location /hello {
    default_type 'text/plain';
    content_by_lua_block {
        ngx.say("Hello, OpenResty!")
    }
}

重载配置:

bash 复制代码
sudo openresty -s reload

测试:

bash 复制代码
curl http://localhost/hello
# 输出:Hello, OpenResty!

🎉 恭喜!你已成功运行第一个 Lua 脚本!


八、常见问题解决

❌ 问题 1:openresty: command not found

  • 原因:PATH 未包含 OpenResty 路径

  • 解决

    bash 复制代码
    # 临时生效
    export PATH=/usr/local/openresty/bin:$PATH
    
    # 永久生效(写入 ~/.bashrc)
    echo 'export PATH=/usr/local/openresty/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc

❌ 问题 2:端口 80 被占用

  • 解决 :修改 nginx.conf 中的 listen 80;listen 8080;

❌ 问题 3:权限拒绝(Permission denied)

  • 原因:SELinux 或 AppArmor 限制

  • 临时关闭 SELinux(仅测试环境)

    bash 复制代码
    sudo setenforce 0

九、结语

感谢您的阅读!如果你有任何疑问或想要分享的经验,请在评论区留言交流!

相关推荐
猫头虎1 个月前
web开发常见问题解决方案大全:502/503 Bad Gateway/Connection reset/504 timed out/400 Bad Request/401 Unauthorized
运维·前端·nginx·http·https·gateway·openresty
ZAEQgyKFs1 个月前
基于RBF神经网络的车速预测模型及其在混动汽车能量管理中的应用研究
openresty
FJW0208141 个月前
《Nginx 高级应用:变量、Rewrite、反向代理与 OpenResty 扩展》(3)
运维·nginx·openresty
杨了个杨89822 个月前
Tengine与OpenResty
openresty
曹天骄2 个月前
OpenResty 源站安全隔离设计在边缘计算架构中的工程实践
安全·边缘计算·openresty
快乐肚皮2 个月前
OpenResty:Nginx的进化之路
nginx·junit·openresty
landonVM2 个月前
OpenResty 的性能优化配置建议
性能优化·openresty
我发在否2 个月前
OpenResty > Lua断点调试
vscode·lua·断点·openresty·luapanda
oMcLin2 个月前
如何在 CentOS 7 上通过配置和调优 OpenResty,提升高并发 Web 应用的 API 请求处理能力?
前端·centos·openresty