OpenResty安装Nginx和Lua环境

一、安装Nginx

  1. 安装依赖
yum install -y readline-devel pcre-devel openssl-devel gcc
  1. 下载并解压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 
  1. 安装插件
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  
  1. 安装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目录。

  1. 查看是否安装成功
[root@redismaster nginx]# /usr/local/tools/nginx/nginx/sbin/nginx -v
nginx version: openresty/1.7.7.2

看到版本号即安装成功!

二、配置Lua

  1. 启动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
  1. 给Nginx配置文件添加Lua头信息
# 在http节点下添加下面两行配置
lua_package_path "/usr/local/tools/nginx/lualib/?.lua;;";  
lua_package_cpath "/usr/local/tools/nginx/lualib/?.so;;"; 
  1. 测试是否成功
[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
  1. 在/usr/local/tools/nginx/nginx/conf下创建lua.conf文件
touch lua.conf
  1. 在lua.conf文件中添加脚本
server {  
    listen       80;  
    server_name  _;  
}  
  1. 在nginx.conf的http部分添加:
include lua.conf;
  1. 验证脚本是否正确
[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
  1. 在lua.conf的server部分添加:
location /lua {  
    default_type 'text/html';  
    content_by_lua 'ngx.say("hello world")';  
} 
/usr/servers/nginx/sbin/nginx -t 
  1. 重新nginx加载配置
/usr/servers/nginx/sbin/nginx -s reload 
  1. 访问http: http://10.1.100.20/lua

  2. 配置测试脚本

vi /usr/servers/nginx/conf/lua/helloworld.lua
ngx.say("hello world"); 
  1. 修改lua.conf,在server节点中配置:
location /lua {  
    default_type 'text/html';  
    content_by_lua_file conf/lua/helloworld.lua; 
}
  1. 测试并重启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
  1. 访问路径
http://10.1.100.20/lua

在这里插入图片描述
14. 看到hello world标明配置成功!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值