新手上路

OpenResty

安装

从下载页 Download下载最新的 OpenResty® 源码包,并且像下面的示例一样将其解压:

tar -xzvf openresty-VERSION.tar.gz

解压后,编译,安装

./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module
make
make install

将/usr/local/openresty/bin加入PATH环境变量。

Hello World

创建nginx的配置文件nginx.conf:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    lua_package_path '/data/wwwroot/work/lib/?.lua;;'; # lua的包路径
    server {
        listen 80;
        location / {
            lua_code_cache off; # 关闭lua代码缓存,无需每次修改代码就重启nginx
            default_type text/html;
            content_by_lua_file '/data/wwwroot/work/index.lua';
        }
    }
}

启动nginx:

index.lua的内容:

访问localhost,看到Hello World则表示OpenResty安装成功。

Lua安装

该段参考:https://www.jianshu.com/p/196b5dad4e78

Openresty中Lua代码的执行是通过LuaJit解析和加速,而LuaJit基于Lua5.1x的ABI开发,Openresty官方明确指出使用LuaJit运行Lua代码是最优方案,所以毋庸置疑Lua5.1是最适合的,最新稳定版为Lua5.1.5。

下载并解压Lua源码:

打开Makefile,可以看到如下信息:

将INSTALL_TOP修改为你既定的安装目录后保存:

安装:

运行lua -v查看所安装的Lua版本:

Luarocks安装

该段参考:https://segmentfault.com/a/1190000003920034

下载并解压Luarocks源码:

编译并安装:

Luarocks 使用初探

命令行运行luarocks,或者luarocks help能看到相关luarocks的详细信息,大致分为以下6个段

1.NAME/名称显示 Luarocks 说明信息- LuaRocks main command-line interface

2.SYNOPSIS/概要显示luarocks命令参数使用格式:

3.GENERAL OPTIONS/通用选项被所有命令所支持的选项,包含指定搜索rocks/rockspecs的 server,默认的 server 搜寻顺序为:

另外选项还设置是否仅仅下载源码、是否显示安装过程、指定超时时间等。

4.VARIABLES/变量Variables from the "variables" table of the configuration file can be overriden with VAR=VALUE assignments.

5.COMMANDS/命令列表luarocks 的常规操作命令 install、search、list 等

6.CONFIGURATION/相关配置信息Lua 版本,rocks trees 等安装 luarocks 时的配置

Last updated

Was this helpful?