curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz
tar zxf lua-5.3.0.tar.gz
cd lua-5.3.0
make linux test
make install
单行注释--:-- single line comment
多行注释--[[ --]]:--[[ multi line comment --]]
变量:默认是全局变量
用 . 定义方法时 object.func_name(arg1,arg2...),方法真正的函数签名形式为:object.func_name(arg1, arg2...)
用 : 定义方法时 object:func_name(arg1,arg2...),方法真正的函数签名形式为:object:func_name(self, arg1, arg2...)
使用 . 调用方法时 object.func_name(arg1,arg2...),真正调用方式为:object.func_name(arg1,arg2...)
使用 : 调用方法时 object:func_name(arg1,arg2...),真正调用方式为:object:func_name(self, arg1,arg2...)
定义:object.func_name(arg1,arg2...)
调用:object.func_name(arg1,arg2...)
定义:object:func_name(arg1,arg2...)
调用:object.func_name(object, arg1,arg2...) 或 object:func_name(arg1,arg2...)
代码示例:
function inherit(base, derive)
derive = derive or {}
if type(base) == "table" then
setmetatable(derive, base)
base.__index = base
derive._super = base
end
return derive
end
local base = {x=1}
function base:print()
print("base class", self.y)
end
local derive = inherit(base,{y=2})
function derive:print()
print("derive class")
end
function derive:test()
self._super:print() -- base class nil
self._super.print(self) -- base class 2
self.print() -- derive class
end
derive:test()
sudo apt-get install luarcoks
sudo apt-get install lua-sql-msyql-dev
-- 单引号
'hello world'
-- 双引号
"hello world"
-- 下面为多行字符串表示方法
[[
hello
world
]]
while(condition)
do
statements
end
for var=exp1,exp2,exp3 do for i=1, 10, 1 do
<执行体> print(i)
end end
for i, v in ipairs(a) do
print(v)
end
repeat
statements
until (condition) -- condition 为真结束循环
# break return只能为语句块的尾部 do return end
tonumber("12") -- 字符串转数字
print("10" + 1) --> 11 -- 隐式转换
tostring(12) -- 数字转字符串
print(10 .. 10) --> 1010 -- 隐式转换 注意第一个数字后需要有空格
rawget(table, index) --> 获取表指定字段,若不存在返回nil, 不会触发元方法index
rawset(table, key, value) --> 直接表字段赋值 绕过newindex元方法
wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz
tar -zxvf luarocks-2.4.1.tar.gz
cd luarocks-2.4.1
./configure --lua-suffix=jit --with-lua-include=/usr/local/include/luajit-2.0
sudo make bootstrap
whereis mysql # 查看include目录
luarocks install luasql-mysql MYSQL_INCDIR=/usr/include/mysql # MYSQL_INCDIR为上步查询目录
搭建 nginx lua 环境,其实是 nginx 能够解释执行 lua,因为需源编译安装 nginx 同时需将 lua 解释器链接进去,具体步骤如下:
1 下载如下4个安装包,并将解压目录放于 /ngx_lua 目录下(/ngx_lua 目录可更换,解压命令 tar -xzvf xxx.tar.gz)
2 源码安装 luajit 并导出 luajit 库和头文件的环境变量
# 安装luajit
cd /ngx_lua/LuaJIT-2.0.5
make
make install
# lib和include是默认放在/usr/local/lib和usr/local/include
# 导出环境变量
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
3 源码安装 nginx
cd /ngx_lua/nginx-1.13.4
./configure --prefix=/nginx --with-ld-opt="-L /usr/local/lib" --add-module=/ngx_lua/ngx_devel_kit-0.3.0 --add-module=/ngx_lua/lua-nginx-module-0.10.10
# --add-module="为module源码目录"
# 若缺库可尝试安装对应的库即可
# apt-get -qq -y install libpcre3
# apt-get -qq -y install libpcre3-dev
# apt-get -qq -y install zlib1g-dev
# apt-get -qq -y install libssl-dev
make -j2
make install
4 验证安装
cd /nginx/conf; vim nginx.conf # 编辑nginx配置文件, 添加如下内容:
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
# 启动nginx
/nginx/sbin/nginx
# 访问localhost/hello 返回hello lua页面内容

