nginx学习篇(二)nginx的基本应用
一、安装
-
一般系统中已经装了了make和g++,无须再装(可以省略)
安装make:yum -y install autoconf automake make
安装g++: yum -y install gcc gcc-c++ -
安装nginx的依赖库
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum install -y openssl openssl-devel -
安装LuaJIT(这里我要用到lua脚本所以需要安装,不是必须的)
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -zxvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make install PREFIX=/usr/local/LuaJIT -
/etc/profile 文件中加入环境变量
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0 -
安装ngx_devel_kit和lua-nginx-module这两个模块
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
解压
tar -zxvf v0.3.0
tar -zxvf v0.10.9rc7 -
下载nginx源码包
wget http://nginx.org/download/nginx-1.9.0.tar.gz
tar -zxvf nginx-1.9.0.tar.gz
cd nginx-1.9.0
得到下图:
-
准备编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/apps/nginx/ngx_devel_kit-0.3.0 --add-module=/usr/apps/nginx/lua-nginx-module-0.10.9rc7 -
编译安装
make&&make install -
加载lua库,加入到ld.so.conf文件
vi /etc/ld.so.conf
/usr/local/lib
/usr/local/LuaJIT/lib
到这里安装基本上已经结束了。
二、启动
进入nginx下的sbin目录:
./nginx -c /usr/local/nginx/conf/nginx.conf -t ----------------------->检测
./nginx -c /usr/local/nginx/conf/nginx.conf ------------------------->启动
./nginx -c /usr/local/nginx/conf/nginx.conf -s reload ------------->配置文件修改后重启
三、遇到的问题
第一次启动的时候报错:
/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
百度了下试了好几种方法,最后添加了环境变量才好的:
export LD_LIBRARY_PATH=/usr/local/LuaJIT/lib
nginx是起来了,看了下进程也有了
curl http://localhost:80/ 也可以访问
但是就是在自己主机上访问不了虚拟机的nginx,ip也能ping通,网上百度下,可能是防火墙的原因,所以增加了过滤,但还是不行,索性就关掉防火墙然而还是一样。头疼。。。。后来灵机一动,会不会是浏览器的原因,果然换了Google浏览器就可以访问了,==!
四、检测lua脚本
我在配置文件的nginx.conf中加了一个location用来匹配路径检测lua脚本是否可行:
location /lua {
set $lua “hello,world”;
content_by_lua ’
ngx.header.content_type=“text/plain”;
ngx.say(ngx.var.lua); ';
}
在浏览器上访问