Nginx中安装Lua模块

本文详细介绍了在Nginx中安装Lua模块的步骤,包括安装LuaJIT、配置环境变量、下载ngx_devel_kit和lua-nginx-module、重新编译Nginx等。同时,针对安装过程中可能遇到的OpenSSL和libluajit-5.1.so.2错误提供了解决方案。

1.基本安装步骤

在安装Lua模块时,建议先查看一下下面列举的问题。另外先安装openssl-devel软件。

(1)首先通过nginx -V命令检查

(2)进入opt/download目录下(个人习惯将下载的内容放在这个目录)

# 下载LuaJIT安装包
[root@xfcy download]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
# 解压LuaJIT
[root@xfcy download]# tar -zvxf LuaJIT-2.0.5.tar.gz
# 进入解压后的LuaJIT-2.0.5目录,执行下面命令(建议设置名称LuaJIT为小写,否则容易出错)
[root@xfcy LuaJIT-2.0.5]# make install  PREFIX=/usr/local/luajit

(3)配置环境变量

# 使用 vim /etc/profile 打开配置文件,配置下面内容
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
# 然后保存配置
source  /etc/profile 

(4)下载ngx_devel_kitlua-nginx-module

# 下载ngx_devel_kit 到/opt/download目录
[root@xfcy download]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
# 解压
[root@xfcy download]# tar -zxvf v0.3.0.tar.gz

# 下载lua-nginx-module 到/opt/download目录
[root@xfcy download]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
# 解压
[root@xfcy download]# tar -zxvf v0.10.9rc7.tar.gz

(5)下载nginx,(如果之前安装过nginx,建议版本一致),重新编译

# 下载nginx-1.16.1版本
[root@xfcy download]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 解压
[root@xfcy download]# tar -zxvf nginx-1.16.1.tar.gz
# 进入nginx重新编译,为了不出错,建议先用nginx -V,得到下面配置的前半部分
[root@xfcy nginx-1.16.1]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/ngx_devel_kit-0.3.0 --add-module=/opt/download/lua-nginx-module-0.10.9rc7
# 注意上面最后的两个需要填写ngx_devel_kit-0.3.0和lua-nginx-module-0.10.9rc7绝对路径

# 重新编译
[root@xfcy nginx-1.16.1]# make
[root@xfcy nginx-1.16.1]# make install

# 加载lua库,加入到ld.so.conf文件
[root@xfcy nginx-1.16.1]# echo "/usr/local/luajit/lib" >> /etc/ld.so.conf
[root@xfcy nginx-1.16.1]# ldconfig

(6)检查是否成功

# 检查
[root@xfcy nginx-1.16.1]# nginx -t
# 重启
[root@xfcy nginx-1.16.1]# systemctl restart nginx

(7)如果成功,将会显示下面结果。

2. 常见问题

(1)报OpenSSL错误

……
checking for OpenSSL library ... not found
checking for OpenSSL library in /usr/local/ ... not found
checking for OpenSSL library in /usr/pkg/ ... not found
checking for OpenSSL library in /opt/local/ ... not found
……

解决方法

# 安装openssl-devel
yum install openssl-devel

(2)报libluajit-5.1.so.2错误。

[root@xfcy nginx-1.16.1]# nginx -t
nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such fileectory

解决方法

# 加载lua库,加入到ld.so.conf文件
[root@xfcy nginx-1.16.1]# echo "/usr/local/luajit/lib" >> /etc/ld.so.conf
[root@xfcy nginx-1.16.1]# ldconfig

以上便是在nginx中安装Lua模块中出现的问题。

3. 参考文献

  1. 慕课手记-Nginx编译安装Lua模块

  2. 简书-Nginx添加Lua扩展

### 如何在没有网络连接的情况下安装 NginxLua 模块 要在无网络环境下完成 NginxLua 模块安装,可以按照以下方式操作: #### 准备阶段 1. **准备必要的软件包** 需要提前准备好以下组件并传输到目标服务器: - Nginx 安装包及其源码。 - `ngx_devel_kit` 和 `lua-nginx-module` 插件的压缩包。 - Lua 运行环境(如 LuaJIT 或标准 Lua 版本)。如果使用的是特定版本的 Lua,则需确保其兼容性[^1]。 2. **创建安装路径** 在目标机器上手动创建所需的安装目录结构。例如: ```bash mkdir -p /opt/nginx/modules/ ``` 3. **上传所需文件至目标主机** 将之前下载的所有必要文件通过 SCP、FTP 或其他工具传送到目标服务器上的指定目录中,比如 `/opt/download/`。 #### 编译与安装过程 1. **解压所有相关资源** 使用命令逐一解压已上传的各模块和插件文件: ```bash cd /opt/download/ tar -zxvf nginx-version.tar.gz tar -zxvf ngx_devel_kit-vX.X.X.tar.gz tar -zxvf lua-nginx-module-vX.X.X.tar.gz ``` 2. **编译前配置选项设置** 转入 Nginx 源代码根目录运行 configure 命令时加入支持 Lua 功能的相关参数: ```bash ./configure --prefix=/usr/local/nginx \ --add-module=/path/to/ngx_devel_kit \ --add-module=/path/to/lua-nginx-module ``` 此处需要注意替换实际路径以及确认 OpenSSL 库是否正确加载[^2]。 3. **执行构建流程** 开始正式制作可执行二进制程序: ```bash make && make install ``` 4. **验证安装成功与否** 启动服务后检查日志输出是否有错误提示;另外也可以尝试访问测试页面来进一步判断功能可用状态。 #### 处理常见问题 对于某些特殊场景可能会遇到类似下面这种报错情况:“unknown 'connection_upgrade' variable”,此时只需修改默认配置模板即可解决此状况: ```conf map $http_upgrade $connection_upgrade { default upgrade; '' close; } ``` 记得把这段定义放置于全局 http{}区块内部以便生效[^4]。 --- ### 提供一段简单的脚本来自动化部分重复劳动项 以下是基于 Bash Shell 实现的一个小型辅助函数用于简化上述几个关键环节的操作逻辑演示: ```bash #!/bin/bash NGINX_SRC="/opt/download/nginx" MODULES_DIR="/opt/nginx/modules" function setup_env() { echo "Creating necessary directories..." mkdir -p ${MODULES_DIR}/{nxt_dev_kit,lua_mod} cp *.tar.* "${MODULES_DIR}/" || exit 1 pushd "$MODULES_DIR/" extract_files popd } function extract_files(){ for f in *; do case "$f" in *.gz ) gunzip -c "$f" | tar xf - ;; *) continue ;; esac done } setup_env echo "Environment prepared successfully." ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值