# 新手上路

## OpenResty

**安装**

从下载页 [Download](http://openresty.org/cn/download.html)下载最新的 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：

```
openresty -c /data/wwwroot/work/conf/nginx.conf
```

index.lua的内容：

```
ngx.say('Hello World')
```

访问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源码：

```
wget http://www.lua.org/ftp/lua-5.1.5.tar.gz
tar zxvf lua-5.1.5.tar.gz
cd lua-5.1.5
```

打开Makefile，可以看到如下信息：

```
# makefile for installing Lua
# see INSTALL for installation instructions
# see src/Makefile and src/luaconf.h for further customization

# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

# Your platform. See PLATS for possible values.
PLAT= none

# Where to install. The installation starts in the src and doc directories,
# so take care if INSTALL_TOP is not an absolute path.
INSTALL_TOP= /usr/local/lua
INSTALL_BIN= $(INSTALL_TOP)/bin
INSTALL_INC= $(INSTALL_TOP)/include
INSTALL_LIB= $(INSTALL_TOP)/lib
INSTALL_MAN= $(INSTALL_TOP)/man/man1
```

将INSTALL\_TOP修改为你既定的安装目录后保存：

```
INSTALL_TOP = /usr/local/lua
```

安装：

```
make macosx         #编译平台设置为macosx，其他平台直接将macosx替换即可，如make linux
make macosx install  #安装平台设置为macosx
```

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

```
[root@jmao lua-5.1.5]# lua -v
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
```

## **Luarocks安装**

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

下载并解压Luarocks源码:

```
wget http://luarocks.github.io/luarocks/releases/
tar -xzvf luarocks-2.4.3.tar.gz 
cd luarocks-2.4.3
```

编译并安装：

```
./configure --prefix=/usr/local/luarocks --with-lua=/usr/local/lua
make build
make install
```

**Luarocks 使用初探**

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

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

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

```
luarocks [--from=<server> | --only-from=<server>] [--to=<tree>] [VAR=VALUE]... <command> [<argument>]
```

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

```
https://luarocks.org
https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/
http://luafr.org/moonrocks/
http://luarocks.logiceditor.com/rocks
```

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

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 时的配置


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://john-mao.gitbook.io/my-handbook/openresty/xin-shou-shang-lu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
