一、安装Nginx
- 安装依赖
yum install -y readline-devel pcre-devel openssl-devel gcc
- 下载并解压OpenResty
cd /usr/local/tools
wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar -xzvf ngx_openresty-1.7.7.2.tar.gz
- 安装插件
cd ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0.tar.gz
- 安装Nginx
# 进入到根目录
cd /usr/local/tools/ngx_openresty-1.7.7.2
# 添加配置项
./configure --prefix=/usr/local/tools/nginx --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
# 编译安装
make && make install
执行完此步骤之后会在/usr/local/tools路径中生成一个nginx目录。
- 查看是否安装成功
[root@redismaster nginx]# /usr/local/tools/nginx/nginx/sbin/nginx -v
nginx version: openresty/1.7.7.2
看到版本号即安装成功!
二、配置Lua
- 启动Nginx
# 启动Nginx
[root@redismaster sbin]# ./nginx
# 查看是否启动成功
[root@redismaster sbin]# ps -ef | grep nginx
root 9059 1 0 22:58 ? 00:00:00 nginx: master process ./nginx
nobody 9060 9059 0 22:58 ? 00:00:00 nginx: worker process
root 9062 2622 0 22:58 pts/0 00:00:00 grep --color=auto nginx
- 给Nginx配置文件添加Lua头信息
# 在http节点下添加下面两行配置
lua_package_path "/usr/local/tools/nginx/lualib/?.lua;;";
lua_package_cpath "/usr/local/tools/nginx/lualib/?.so;;";
- 测试是否成功
[root@redismaster nginx]# ./nginx/sbin/nginx -t
nginx: the configuration file /usr/local/tools/nginx/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/tools/nginx/nginx/conf/nginx.conf test is successful
- 在/usr/local/tools/nginx/nginx/conf下创建lua.conf文件
touch lua.conf
- 在lua.conf文件中添加脚本
server {
listen 80;
server_name _;
}
- 在nginx.conf的http部分添加:
include lua.conf;
- 验证脚本是否正确
[root@redismaster nginx]# ./nginx/sbin/nginx -t
nginx: the configuration file /usr/local/tools/nginx/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/tools/nginx/nginx/conf/nginx.conf test is successful
- 在lua.conf的server部分添加:
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")';
}
/usr/servers/nginx/sbin/nginx -t
- 重新nginx加载配置
/usr/servers/nginx/sbin/nginx -s reload
-
访问http: http://10.1.100.20/lua
-
配置测试脚本
vi /usr/servers/nginx/conf/lua/helloworld.lua
ngx.say("hello world");
- 修改lua.conf,在server节点中配置:
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/helloworld.lua;
}
- 测试并重启Nginx
[root@redismaster nginx]# ./nginx/sbin/nginx -t
nginx: the configuration file /usr/local/tools/nginx/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/tools/nginx/nginx/conf/nginx.conf test is successful
[root@redismaster nginx]# ./nginx/sbin/nginx -s reload
- 访问路径
http://10.1.100.20/lua
14. 看到hello world标明配置成功!!!