Skip to content

qugemingzizhemefeijin/openresty-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Install 安装流程

nginx-1.13.6.tar.gz openresty-1.13.6.1.tar.gz

yum -y install pcre pcre-devel libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl openssl openssl-devel GeoIP GeoIP-devel

wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
tar -zxvf openresty-1.13.6.1.tar.gz

cd bundle/LuaJIT-2.1-20171103/
make clean && make && make install

返回到bundle目录
wget -q -O ngx_cache_purge.2.3.tar.gz https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -zxvf ngx_cache_purge.2.3.tar.gz

wget -q -O nginx_upstream_check_module.v0.3.0.tar.gz https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -zxvf nginx_upstream_check_module.v0.3.0.tar.gz

安装openresty
./configure --prefix=/usr/local/openresty-1.13.6.1 --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
gmake && gmake install

cd /usr/local/
ln -s openresty-1.13.6.1/ openresty

安装nginx
wget -q http://nginx.org/download/nginx-1.13.6.tar.gz
tar -zxvf nginx-1.13.6.tar.gz

export LUAJIT_LIB=/usr/local/openresty/luajit/lib
export LUAJIT_INC=/usr/local/openresty/luajit/include/luajit-2.1

./configure --prefix=/usr/local/nginx-1.13.6 <br/> --with-ld-opt="-Wl,-rpath,/usr/local/openresty/luajit/lib" <br/> --add-module=/root/openresty-1.13.6.1/bundle/ngx_devel_kit-0.3.0 <br/> --add-module=/root/openresty-1.13.6.1/bundle/ngx_lua-0.10.11 <br/> --add-module=/root/openresty-1.13.6.1/bundle/headers-more-nginx-module-0.33 <br/> --add-module=/root/openresty-1.13.6.1/bundle/iconv-nginx-module-0.14 <br/> --add-module=/root/openresty-1.13.6.1/bundle/form-input-nginx-module-0.12 <br/> --add-module=/root/openresty-1.13.6.1/bundle/array-var-nginx-module-0.05 <br/> --add-module=/root/openresty-1.13.6.1/bundle/nginx_upstream_check_module-0.3.0 <br/> --add-module=/root/openresty-1.13.6.1/bundle/echo-nginx-module-0.61 <br/> --with-http_ssl_module <br/> --with-http_v2_module <br/> --with-http_stub_status_module

make -j2 && make install


配置nginx openresty

nginx.conf中http模块新增

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
lua_package_path "/opt/openresty/openresty-example/lualib/?.lua;;";#lua 模块
lua_package_cpath "/opt/openresty/openresty-example/lualib/?.so;;";#c模块
include /opt/openresty/openresty-example/lua.conf;
}

将原来的server区块删除掉
新建lua.conf放入server区块

#lua.conf
server {
listen 80;
server_name _;

location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua;
}
}

nginx的conf目录新建lua文件夹
vi test.lua

写入此行
ngx.say("hello world");

执行
/usr/local/nginx/sbin/nginx -t

如果报错误
/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

可以执行此段
echo "/usr/local/lib" >> /etc/ld.so.conf.d/usr_local_lib.conf

ldd nginx后查看由
libluajit-5.1.so.2 => not found
变为
libluajit-5.1.so.2 => /usr/local/lib/libluajit-5.1.so.2 (0x00007f72d2e3c000)

再次执行
/usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx-1.13.6/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.13.6/conf/nginx.conf test is successful

则代表安装成功

Download lua-resty-template

cd lualib/resty
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua
mkdir html
cd html
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua

Install Redis SSDB Twemproxy

1.Redis安装
wget https://github.com/antirez/redis/archive/2.8.19.tar.gz
tar -xvf 2.8.19.tar.gz
cd redis-2.8.19/
make
后台启动Redis服务器
nohup /usr/servers/redis-2.8.19/src/redis-server /usr/servers/redis-2.8.19/redis.conf &
查看是否启动成功
ps -aux | grep redis
进入客户端
/usr/servers/redis-2.8.19/src/redis-cli -p 6379
执行如下命令
127.0.0.1:6379> set i 1
OK
127.0.0.1:6379> get i
"1"
通过如上命令可以看到我们的Redis安装成功。更多细节请参考http://redis.io/。

2.SSDB安装与使用
首先确保安装了g++,如果没有安装,如ubuntu可以使用如下命令安装
yum -y install gcc gcc-c++
wget -O ssdb.1.9.4.tar.gz https://github.com/ideawu/ssdb/archive/1.9.4.tar.gz
tar -zxvf ssdb.1.9.4.tar.gz
cd ssdb-1.9.4
make
mv ssdb-1.9.4 /usr/local
cd /usr/local/
ln -s ssdb-1.9.4 ssdb

后台启动SSDB服务器
nohup /usr/local/ssdb/ssdb-server /usr/local/ssdb/ssdb.conf &
查看是否启动成功
ps -aux | grep ssdb
进入客户端
/usr/local/ssdb/tools/ssdb-cli -p 8888
redis-cli -p 8888
因为SSDB支持Redis协议,所以用Redis客户端也可以访问
执行如下命令
127.0.0.1:8888> set i 1
OK
127.0.0.1:8888> get i
"1"
安装过程中遇到错误请参考http://ssdb.io/docs/zh_cn/install.html;对于SSDB的配置请参考官方文档https://github.com/ideawu/ssdb。

lua_code_cache

默认情况下lua_code_cache 是开启的,即缓存lua代码,即每次lua代码变更必须reload nginx才生效,如果在开发阶段可以通过lua_code_cache off;关闭缓存,这样调试时每次修改lua代码不需要reload nginx;但是正式环境一定记得开启缓存。

在lua.conf中
location /lua {
default_type 'text/html';
lua_code_cache off; #新增的是此行
content_by_lua_file conf/lua/test.lua;
}

开启后reload nginx会看到如下报警
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/servers/nginx/conf/lua.conf:8


错误日志


如果运行过程中出现错误,请不要忘记查看错误日志。
tail -f /usr/local/nginx/logs/error.log

到此我们的基本环境搭建完毕。


# nginx+lua项目构建
以后我们的nginx lua开发文件会越来越多,我们应该把其项目化,已方便开发。项目目录结构如下所示:

example
example.conf ---该项目的nginx 配置文件
lua ---我们自己的lua代码
test.lua
lualib ---lua依赖库/第三方依赖
*.lua
*.so

其中我们把lualib也放到项目中的好处就是以后部署的时候可以一起部署,防止有的服务器忘记复制依赖而造成缺少依赖的情况。

我们将项目放到到/usr/example目录下。

/usr/local/nginx/conf/nginx.conf配置文件如下(此处我们最小化了配置文件)

#user nobody;
worker_processes 2;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;

#lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/example/lualib/?.so;;"; #c模块
include /usr/example/example.conf;
}
通过绝对路径包含我们的lua依赖库和nginx项目配置文件。

/usr/example/example.conf配置文件如下

server {
listen 80;
server_name _;

location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/example/lua/test.lua;
}
}
lua文件我们使用绝对路径/usr/example/lua/test.lua

到此我们就可以把example扔svn或git上了。

相关资料

参考开涛的博客 (OpenResty + Lua 开发入门) http://jinnianshilongnian.iteye.com/blog/2190344
nginx与lua的执行顺序和步骤说明 http://blog.csdn.net/wlgy123/article/details/49815531
Lua简明教程 http://coolshell.cn/articles/10739.html
lua在线lua学习教程 http://book.luaer.cn/
Lua 5.1 参考手册 http://www.codingnow.com/2000/download/lua_manual.html
Lua 5.3 参考手册 http://cloudwu.github.io/lua53doc/

About

openresty example for lua

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published