菜单开关

周梦康 发表于 2017-09-18 2157 次浏览 标签 : NginxLua

免费领取阿里云优惠券 我的直播 - 《PHP 进阶之路》

我只在我本地测试环境安装了 openresty,自己服务器上使用的 nginx,不想装 openresty 了,所以想只增加一个模块,所以记录下。

首先安装 lua

[root@VM_132_97_centos soft]# wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
[root@VM_132_97_centos soft]# tar xzvf LuaJIT-2.0.4.tar.gz
[root@VM_132_97_centos soft]# cd LuaJIT-2.0.4
[root@VM_132_97_centos LuaJIT-2.0.4]# make install PREFIX=/usr/local/luajit
[root@VM_132_97_centos LuaJIT-2.0.4]# export LUAJIT_LIB=/usr/local/luajit/lib
[root@VM_132_97_centos LuaJIT-2.0.4]# export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
[root@VM_132_97_centos LuaJIT-2.0.4]# pwd
/data/soft/LuaJIT-2.0.4

然后安装lua-nginx-module

[root@VM_132_97_centos nginx-1.10.1]# nginx -V
nginx version: nginx/1.10.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6

[root@VM_132_97_centos nginx-1.10.1]# pwd
/data/lnmp1.0-full/nginx-1.10.1
[root@VM_132_97_centos nginx-1.10.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

[root@VM_132_97_centos nginx-1.10.1]# mkdir modules
[root@VM_132_97_centos nginx-1.10.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  modules  objs  README  src
[root@VM_132_97_centos nginx-1.10.1]# cd modules/
[root@VM_132_97_centos modules]# ls
[root@VM_132_97_centos modules]# git clone https://github.com/openresty/lua-nginx-module.git
Initialized empty Git repository in /data/lnmp1.0-full/nginx-1.10.1/modules/lua-nginx-module/.git/
remote: Counting objects: 18961, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 18961 (delta 12), reused 2 (delta 0), pack-reused 18927
Receiving objects: 100% (18961/18961), 12.09 MiB | 3.45 MiB/s, done.
Resolving deltas: 100% (13179/13179), done.
[root@VM_132_97_centos modules]# ls
lua-nginx-module
[root@VM_132_97_centos modules]# cd ..
[root@VM_132_97_centos nginx-1.10.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  modules  objs  README  src
[root@VM_132_97_centos nginx-1.10.1]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=./modules/lua-nginx-module/


Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@VM_132_97_centos nginx-1.10.1]# make
[root@VM_132_97_centos nginx-1.10.1]# cd objs/
[root@VM_132_97_centos objs]# ll -alh
总用量 5.4M
drwxr-xr-x  4 root root 4.0K 9月  18 13:08 .
drwxr-xr-x 10 1001 1001 4.0K 9月  18 11:44 ..
drwxr-xr-x  3 root root 4.0K 9月  18 13:02 addon
-rw-r--r--  1 root root  19K 9月  18 13:02 autoconf.err
-rw-r--r--  1 root root  61K 9月  18 13:02 Makefile
-rwxr-xr-x  1 root root 5.2M 9月  18 13:08 nginx
-rw-r--r--  1 root root 5.3K 9月  18 13:08 nginx.8
-rw-r--r--  1 root root 6.7K 9月  18 13:02 ngx_auto_config.h
-rw-r--r--  1 root root  657 9月  18 13:02 ngx_auto_headers.h
-rw-r--r--  1 root root 5.5K 9月  18 13:02 ngx_modules.c
-rw-r--r--  1 root root  34K 9月  18 13:08 ngx_modules.o
drwxr-xr-x  9 root root 4.0K 9月  29 2016 src

替换二进制文件

不要执行make install否则会把一些配置文件都覆盖初始化了。手动粘贴二进制文件。

[root@VM_132_97_centos objs]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp:是否覆盖"/usr/local/nginx/sbin/nginx.old"? yes

[root@VM_132_97_centos objs]# cp ./nginx /usr/local/nginx/sbin/
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? yes
cp: 无法创建普通文件"/usr/local/nginx/sbin/nginx": 文本文件忙
[root@VM_132_97_centos objs]# service nginx stop
Stopping nginx daemon: nginx.
[root@VM_132_97_centos objs]# cp ./nginx /usr/local/nginx/sbin/
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? yes
[root@VM_132_97_centos objs]# service nginx start
Starting nginx daemon: nginx/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
nginx already running.

错误排查

查看文件引用

[root@VM_132_97_centos sbin]# ldd /usr/local/nginx/sbin/nginx
    linux-vdso.so.1 =>  (0x00007fff30ff3000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003b6b600000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00000033dbe00000)
    libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003b6d600000)
    libluajit-5.1.so.2 => not found
    libm.so.6 => /lib64/libm.so.6 (0x0000003b6c200000)
    libpcre.so.0 => /usr/local/lib/libpcre.so.0 (0x00007f3fe2aac000)
    libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00000031ace00000)
    libz.so.1 => /lib64/libz.so.1 (0x0000003b6be00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003b6ae00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003b6aa00000)
    libfreebl3.so => /usr/lib64/libfreebl3.so (0x0000003b6da00000)

添加软连接

[root@VM_132_97_centos sbin]# ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
[root@VM_132_97_centos sbin]# service nginx start
Starting nginx daemon: nginx.

web 浏览没问题

嗨,老铁,欢迎来到我的博客!

如果觉得我的内容还不错的话,可以关注下我在 segmentfault.com 上的直播。我主要从事 PHP 和 Java 方面的开发,《深入 PHP 内核》作者之一。

[视频直播] PHP 进阶之路 - 亿级 pv 网站架构的技术细节与套路 直播中我将毫无保留的分享我这六年的全部工作经验和踩坑的故事,以及会穿插着一些面试中的 考点难点加分点

评论列表