OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
OpenResty 相关资料的链接地址:
地址一:http://openresty.org/
地址二:http://www.oschina.net/question/28_60461
地址三:https://github.com/openresty/lua-nginx-module
1)到OpenResty官方网站下载安装包,这里我们下载ngx_openresty-1.9.3.1.tar.gz稳定版本即可。
2)将下载好的ngx_openresty-1.9.3.1.tar.gz安装包上传至Linux相应的目录下。
[hadoop@master ~]$ ls ngx_openresty-1.9.3.1.tar.gz [hadoop@master ~]$ tar -zxvf ngx_openresty-1.9.3.1.tar.gz [hadoop@master ~]$ ll ngx_openresty-1.9.3.13)对ngx_openresty-1.9.3.1进行configure
[hadoop@master ~]$ cd ngx_openresty-1.9.3.1 [hadoop@master ~]$ ll bundle configure README.markdown [hadoop@master ~]$ su root //切换到root用户 Password: [root@master ngx_openresty-1.9.3.1]# ll bundle configure README.markdown [root@master ngx_openresty-1.9.3.1]# ./configure在安装过程中可能不会一帆风顺,经常报错缺少jar包,但是大家不用担心, 缺什么jar包,我们安装什么jar就可以了。比如在configure的过程中,报错缺少如下jar包。
...... checking for PCRE library ... not found ......那我们该怎么办呢?我们按照下面的操作安装相应的jar包即可。
[root@master ngx_openresty-1.9.3.1]# yum list pcre-devel //查看pcre开发工具是否安装 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn //可安装包,说明这个包还没有安装,需要我们安装 Available Packages pcre-devel.i686 7.8-7.el6 base [root@master ngx_openresty-1.9.3.1]# yum install -y pcre-devel //在线安装即可 [root@master ngx_openresty-1.9.3.1]# ./configure //重新configure即可如果configure还没有成功,我们继续检查是否缺少其它包,重复上面的操作即可。如果没有没有报错我们就继续。
[root@master ngx_openresty-1.9.3.1]# make //执行make操作 [root@master ngx_openresty-1.9.3.1]# make install //执行make install操作在make install成功之后,我们可以从打印日志看到它默认安装在/usr/local/openresty目录下, 然后我们进入该目录查看一下。
[root@master ngx_openresty-1.9.3.1]# cd /usr/local/openresty [root@master openresty]# ll bin luajit lualib nginx [root@master openresty]# cd nginx/ [root@master nginx]# ll conf //配置文件(配置指令) html //静态页面 logs //日志文件 sbin //主程序4)对openresty 启动和停止操作
启动openresty
[root@master nginx]# cd sbin/ [root@master sbin]# ll nginx [root@master sbin]# .nginx //启动 [root@master sbin]# ps -aux | grep nginx | grep -v grep //查看nginx进程是否启动 Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ root 6167 0.0 0.0 52484 1336 ? Ss 17:39 0:00 nginx: master process ./nginx nobody 6168 0.0 0.0 52844 1908 ? S 17:39 0:00 nginx: worker processopenresty启动参数:
-c 为 Nginx 指定一个配置文件,来代替缺省的。
-t 测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
-v 显示 nginx 的版本。
-V 显示 nginx 的版本,编译器版本和配置参数。
停止openresty
[root@master nginx]# kill -QUIT `cat /usr/local/openresty/nginx/logs/nginx.pid` //关闭 [root@master sbin]# ps -aux | grep nginx | grep -v grep //查看nginx进程是否关闭 Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQNginx的信号控制:
TERM,INT 快速关闭
QUIT 从容关闭
HUP 平滑重启,重新加载配置文件
USR1 重新打开日志文件,在切割日志的时候用途较大
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程
1)准备工作目录
[root@master ~]# su hadoop //切换到hadoop用户 [hadoop@master ~]# mkdir work //创建一个work目录 [hadoop@master ~]# cd work/ //进入work目录 [hadoop@master work]# mkdir conf logs //创建两个子目录2)准备nginx配置文件
[hadoop@master work]# ls conf logs [hadoop@master work]# cd conf/ [hadoop@master conf]# vi nginx.conf //创建一个nginx.conf文件 worker_processes 1;//work 进程数指定为1 error_log logs/error.log;//日志目录 events{ worker_connections 1024;//为每个work指定最大连接数 } http{ server{ listen 8080; //监听端口 location /{ default_type text/html;//默认类型 content_by_lua ' ngx.say("hello,world
") //具体内容 '; } } }3)配置环境变量
[hadoop@master conf]# su root [root@master conf]# vi /etc/profile export PATH=$PATH:/usr/local/openresty/nginx/sbin保存退出,然后通过以下命令使文件生效。
[root@master conf]# source /etc/profile4)启动nginx
[root@master conf]# su hadoop [hadoop@master conf]# cd .. [hadoop@master work]# nginx -p `pwd` -c conf/nginx.conf [hadoop@master work]# ps -aux | grep nginx | grep -v grep Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ hadoop 6167 0.0 0.0 52484 1336 ? Ss 17:39 0:00 nginx: master process ./nginx hadoop 6168 0.0 0.0 52844 1908 ? S 17:39 0:00 nginx: worker process5)访问hello world
openresty启动之后,我们可以输入地址:master:8080,在web访问下访问并查看效果。