OpenResty 安装指南

前言

以下是安装 OpenResty(基于 Nginx 的增强版 Web 平台)的详细步骤,涵盖主流通用系统环境及验证方法:

一、安装前准备

1. 系统要求

  • 支持系统:Ubuntu/Debian、CentOS/RHEL、macOS(Homebrew)、Windows(WSL)
  • 依赖工具 :wget、gcc、make、perl

2. 安装依赖包(以 Ubuntu/Debian 为例)

bash 复制代码
sudo apt update
sudo apt install -y libpcre3-dev libssl-dev perl make build-essential zlib1g-dev

二、通过官方仓库安装(推荐)

1. 添加 OpenResty 官方仓库

bash 复制代码
# 导入 GPG 密钥
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -

# 添加 APT 源
echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list

# 更新仓库索引
sudo apt update

2. 安装 OpenResty

bash 复制代码
sudo apt install -y openresty

3. 验证安装

bash 复制代码
openresty -v  # 应显示版本号(如 openresty/1.21.4.1)

三、源码编译安装(自定义需求)

1. 下载源码包

bash 复制代码
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
tar -xzvf openresty-1.21.4.1.tar.gz
cd openresty-1.21.4.1

2. 配置编译选项

bash 复制代码
./configure \
  --prefix=/usr/local/openresty \
  --with-luajit \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_stub_status_module \
  --with-pcre-jit

3. 编译并安装

bash 复制代码
make -j$(nproc)  # 使用多核加速编译
sudo make install

4. 配置环境变量

bash 复制代码
echo 'export PATH=/usr/local/openresty/nginx/sbin:$PATH' >> ~/.bashrc
source ~/.bashrc

四、基本操作命令

操作 命令
启动服务 sudo systemctl start openresty
停止服务 sudo systemctl stop openresty
重载配置 sudo systemctl reload openresty
设置开机自启 sudo systemctl enable openresty
检查配置文件语法 sudo nginx -t

五、验证运行状态

1. 创建测试配置文件

编辑 /usr/local/openresty/nginx/conf/nginx.conf,添加以下内容:

nginx 复制代码
server {
    listen 80;
    server_name localhost;
    
    location /hello {
        default_type text/plain;
        content_by_lua_block {
            ngx.say("Hello, OpenResty!")
        }
    }
}

2. 重载服务并测试

bash 复制代码
sudo nginx -s reload
curl http://localhost/hello  # 应输出 "Hello, OpenResty!"

六、常见问题解决

  1. 端口冲突

    • 修改 nginx.conf 中的 listen 端口号(如改为 8080)
    • 检查占用端口的进程:sudo lsof -i :80
  2. 权限不足

    • 使用 sudo 执行命令

    • 确保 /usr/local/openresty 目录权限正确:

      bash 复制代码
      sudo chown -R www-data:www-data /usr/local/openresty
  3. 模块缺失

    • 重新编译时添加所需模块(如 --with-http_gzip_static_module)

七、卸载方法

bash 复制代码
# 仓库安装
sudo apt remove openresty && sudo apt autoremove

# 源码安装
sudo rm -rf /usr/local/openresty

结语

通过以上步骤即可完成 OpenResty 的安装与基础配置。建议优先使用官方仓库安装以简化维护流程,需要深度定制时再选择源码编译。

相关推荐
Ruiery7 小时前
Linux 6.6内核 CPU 深度解析(七):调度器启动 — 从单核到多核,调度器分阶段点亮
linux·运维·服务器
50万马克的面包7 小时前
CSDN-栈队列数组-知识点整理
linux·运维·服务器
福兮说9 小时前
设计稿是 #4A7C6F,页面量出来是 #4B7C6F:HEX、HSL、透明度、canvas 来回转的七个坑
前端·javascript·css·canvas
郑州光合科技余经理10 小时前
海外版外卖加盟:总站与分站配送规则怎么分开管
java·开发语言·前端·后端·uni-app·php·ai编程
91刘仁德10 小时前
HTTP协议详解:从URL到HTTP服务器的完整实战
服务器·网络·笔记·网络协议·http
小呆呆66610 小时前
副业搞起来,小说,漫画,漫剧的成本优化思路
前端·后端·面试
Dovis(誓平步青云)10 小时前
浇水提醒刚弹出又消失,植物状态别只存一个百分比
开发语言·前端·javascript·pdf·ecmascript·电脑
码艺-Alimjan11 小时前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_12 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒12 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端