OpenResty环境搭建

作者: ironxu 2017-01-26 00:11:13

OpenResty 是一个基于Nginx 与Lua 的高性能Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。本文是根据 openresty 官网(http://openresty.org/cn/)指南搭建OpenResty 服务的笔记。


 


1、安装 OpenResty


系统 Ubuntu 16.04 LTS


1.1、安装依赖库


apt install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential


1.2、编译安装 OpenResty


下载地址 https://openresty.org/en/download.html


编译安装命令


tar -xzvf openresty-VERSION.tar.gz


cd openresty-VERSION/


./configure


make


sudo make install


VERSION 是下载的openresty 具体版本号。


make install 后应用在 /usr/local/openresty/bin/openresty 目录。


 


2、启动服务


准备项目目录


mkdir ~/work


cd ~/work


mkdir logs/ conf/


2.1、配置 nginx.conf


OpenResty 是Nginx 与Lua 的封装,最终用运行的还是nginx。


~/work/conf/nginx.conf


worker_processes  1;


error_log logs/error.log;


events {


worker_connections 1024;


}


http {


server {


listen 8080;


# 条件1: 基于 lua 模块响应文本


location / {


default_type text/html;


content_by_lua '


ngx.say("

hello, world

")


';


}


# 条件2: 打印参数


location /param {


set $foo hello;


echo "foo: $foo";


echo "request_method: $request_method";


echo "remote_addr: $remote_addr";


}


}


}


2.2、启动 nginx


启动 nginx


/usr/local/openresty/nginx/sbin/nginx -p ~/work/ -c ~/work/conf/nginx.conf


如果觉得每次使用/usr/local/openresty/nginx/sbin/nginx 不方便,可以设置环境变量:


~/.bashrc


PATH=/usr/local/openresty/nginx/sbin:$PATH


export PATH


执行 source ~/.bashrc 使配置生效。


2.3、测试 nginx


测试条件1: 基于 lua 模块响应文本


curl http://localhost:8080/


// 输出如下


hello, world


测试条件2: 打印参数


curl localhost:8080/param


// 输出如下


foo: hello


request_method: GET


remote_addr: 127.0.0.1


注意 因为我们手动指定了nginx 的配置文件和执行临时目录,所以重启nginx 时,也需要带上相关配置。重启命令如下:


sudo /usr/local/openresty/nginx/sbin/nginx -p ~/work/ -c ~/work/conf/nginx.conf -s reload


 


参考


openresty 官网指导文档(http://openresty.org/cn/installation.html)


 


本文永久更新地址:http://www.linuxdiyf.com/linux/27934.html

相关资讯