插件窝 干货文章 Linux如何编译安装openresty?

Linux如何编译安装openresty?

OpenResty Nginx 安装 编译 415    来源:    2025-05-11

在Linux上编译安装OpenResty的详细步骤

OpenResty是一个基于Nginx和Lua的高性能Web平台,下面是在Linux系统上从源码编译安装OpenResty的完整步骤。

1. 安装依赖

首先安装编译OpenResty所需的依赖包:

Ubuntu/Debian系统

sudo apt-get update
sudo apt-get install -y build-essential libpcre3-dev libssl-dev perl make git zlib1g-dev

CentOS/RHEL系统

sudo yum install -y gcc make pcre-devel openssl-devel perl git zlib-devel

2. 下载OpenResty源码

wget https://openresty.org/download/openresty-<version>.tar.gz
tar -xzvf openresty-<version>.tar.gz
cd openresty-<version>

<version>替换为最新的版本号,例如1.21.4.1

3. 配置编译选项

./configure --prefix=/usr/local/openresty \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_stub_status_module \
            --with-http_gzip_static_module \
            --with-pcre-jit \
            --with-threads \
            --with-file-aio

常用配置选项说明: - --prefix:指定安装目录 - --with-http_ssl_module:启用SSL支持 - --with-http_v2_module:启用HTTP/2支持 - --with-pcre-jit:启用PCRE JIT编译 - --with-threads:启用线程支持

4. 编译和安装

make -j$(nproc)  # 使用所有CPU核心进行编译
sudo make install

5. 设置环境变量

为了方便使用,将OpenResty添加到系统PATH中:

echo 'export PATH=/usr/local/openresty/nginx/sbin:$PATH' >> ~/.bashrc
source ~/.bashrc

6. 验证安装

openresty -v

应该会显示OpenResty的版本信息。

7. 启动OpenResty

sudo /usr/local/openresty/nginx/sbin/nginx

8. 可选配置

创建systemd服务

创建服务文件/etc/systemd/system/openresty.service

[Unit]
Description=OpenResty
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

然后启用服务:

sudo systemctl enable openresty
sudo systemctl start openresty

常见问题解决

  1. 缺少依赖错误

    • 根据错误提示安装相应的开发包
  2. 端口冲突

    • 修改/usr/local/openresty/nginx/conf/nginx.conf中的监听端口
  3. 权限问题

    • 确保有权限访问安装目录
    • 使用sudo运行安装命令
  4. 模块未启用

    • 重新配置时添加相应的--with-xxx-module选项

通过以上步骤,您应该已经成功在Linux系统上编译安装了OpenResty。