环境:本地VM虚拟机+centos环境搭建
1.准备openresty依赖
yum install readline-devel pcre-devel openssl-devel gcc
2.安装openresty
有问题可以用yum install wget和yum install perl解决
wget https://openresty.org/download/ngx_openresty-1.9.7.2.tar.gz
tar -xvf ngx_openresty-1.9.7.2.tar.gz
cd ngx_openresty-1.9.7.2
// 设置安装目录
./configure --prefix=/usr/local/openresty (默认集成了lua环境)
gmake
gmake install
3.测试
安装完成后去到安装目录:
cd /usr/local/openresty/nginx/sbin ./nginx //启动nginx,./nginx -s reload重启
在浏览器输入Linux的ip地址即可访问ngnix
4.配置lua脚本访问
vim /usr/local/openresty/nginx/conf/nginx.conf
加入如下代码:
location /lua { default_type text/text; content_by_lua 'ngx.say("hello world lua")'; }
重新启动nginx: ./nginx -s reload
访问http://192.168.143.11/lua (替换为linux ip地址)
页面出现hello world lua ,表示安装成功
我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。
5.nginx + lua简单使用示例
vim /lua/test.lua
内容如下:
ngx.say("hello world");
修改nginx.conf,添加如下内容
location /lua { default_type 'text/html'; content_by_lua_file /lua/test.lua; }
访问http://192.168.143.11/lua (替换为linux ip地址)
页面出现hello world lua ,表示安装成功