通用Api基于lua的线上部署说明

2016/05/28 lua

通用Api基于lua 线上部署说明

部署步骤:

一.安装luajit环境

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
scp /usr/local/src/LuaJIT-2.0.4.tar.gz root@10.199.131.13:/usr/local/src/
cd /usr/local/src
tar zxf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make
指定安装位置
make install PREFIX=/usr/local/luajit2.0.4

二.升级ngxin1.9以上(新版本略过)

/usr/local/nginx/sbin/nginx -s stop
mkdir -p /home/install_shell/
scp /home/install_shell/install_wm_nginx_1.10.2.sh root@10.199.131.12:/home/install_shell/
rm -rf /usr/local/nginx
cd /home/install_shell/
sh install_wm_nginx_1.10.2.sh 
ln -s /usr/local/nginx-1.10.2 /usr/local/nginx
rm -rf /usr/local/nginx/conf/*
cp -r /usr/local/nginx-1.7.3/conf/* /usr/local/nginx/conf/
/usr/local/nginx/sbin/nginx

三.拷备luacode代码

该目录包含lua代码、及第三方lua库(如redis,cjson等,其中cjson是基于c的扩展so文件已经编译好了)

scp /home/luacode.tar.gz root@10.199.131.12:/home/
cd /home
tar zxf luacode.tar.gz
cp /home/luacode/vendor/cjson/lua-cjson-2.1.0/cjson.so /usr/local/luajit2.0.4/lib/lua/5.1/

四.安装nginx的lua模块

目前已经将ngx_http_lua_module,ngx_devel_kit (NDK) module这2个模块,编译成了so动态加载模块,直接拷备使用(需要nginx1.9以上版本支持)

mkdir -p /usr/local/nginx/modules/
scp /usr/local/nginx/modules/* root@10.199.131.12:/usr/local/nginx/modules/

五.nginx配置

vim /usr/local/nginx/conf/nginx.conf

在main环境中,最开始加入
	# load lua module
	load_module modules/ndk_http_module.so;
	load_module modules/ngx_http_lua_module.so;

在http环境中加入

	#指定刚下载的redis扩展程序存放目录
	lua_package_path "/home/luacode/vendor/?.lua;;";
	#指定so模式的lua扩展包,基于c编译的,如cjson包
	lua_package_cpath '/usr/local/luajit2.0.4/lib/lua/5.1/?.so;;';
	#lua nginx worker共享缓存
	lua_shared_dict data 1000m;
	init_by_lua_file /home/luacode/application/api/init.lua;

vim /usr/local/nginx/conf/vhost/qt.baidu.com.conf

新加入以下配置
    location /api {
        default_type 'text/plain';
        #GETm~V[0m~Om~Z~D请0m~B0m~Lm~@~Z0m~Glua解m~^~P
        if ($request_method = "GET") {
            content_by_lua_file /home/luacode/application/api/content.lua;
        }
        if ($request_method != "GET") {
            #proxy_pass http://backend;
            rewrite ^(.*)$ /index.php?$1 last;
        }
    }

    location ~ /backend/(.*) {
        #internal;
        rewrite /backend/(.*) /index.php?$1 last;
        #rewrite /backend/(.*) $1 break;
        #proxy_pass http://backend;
    }
	
修改原配置为(加入location范围内):
location / {
    if (!-e $request_filename) {
        rewrite  ^/uploads/(.*) /uploads/$1 last;
        rewrite  ^/static/(.*)  /static/$1 last;
        rewrite  ^/(.*) /index.php?$1 last;
        break;
    }
}

Search

    Table of Contents