OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关(摘自官网)。本文将会介绍如何在Centos7上,安装Nginx+Lua的开发环境,并运行一个“Hello World”示例。

一、环境安装

1.1 创建工作路径

  我计划将Openresty安装到/usr/servers下,首先创建这个文件夹。

[root@localhost ~]# mkdir -p /usr/servers
[root@localhost ~]# cd /usr/servers/
[root@localhost servers]#

1.2 安装依赖库

[root@localhost servers]# yum install readline-devel pcre-devel openssl-devel perl  make gcc -y

1.3 下载Nginx及要安装的模块

  其中,ngx_cache_purge模块用于清理nginx缓存,nginx_upstream_check_module用于ustream的健康检查。

[root@localhost servers]# pwd
/usr/servers
[root@localhost servers]# wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
[root@localhost servers]# tar -xzvf ngx_openresty-1.7.7.2.tar.gz
[root@localhost servers]# cd /usr/servers/ngx_openresty-1.7.7.2/bundle
[root@localhost bundle]# pwd
/usr/servers/ngx_openresty-1.7.7.2/bundle
[root@localhost bundle]# wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
[root@localhost bundle]# tar -xvf 2.3.tar.gz
[root@localhost bundle]# cd /usr/servers/ngx_openresty-1.7.7.2/bundle
[root@localhost bundle]# pwd
/usr/servers/ngx_openresty-1.7.7.2/bundle
[root@localhost bundle]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
[root@localhost bundle]# tar -xvf v0.3.0.tar.gz

1.4 安装LuaJIT

[root@localhost bundle]# cd /usr/servers/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-/
[root@localhost LuaJIT-2.1-]# pwd
/usr/servers/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-
[root@localhost LuaJIT-2.1-]# make clean && make && make install
[root@localhost LuaJIT-2.1-]# ln -sf luajit-2.1.-alpha /usr/local/bin/luajit
[root@localhost LuaJIT-2.1-]# lua -v
Lua 5.1. Copyright (C) - Lua.org, PUC-Rio
[root@localhost LuaJIT-2.1-]# luajit -v
LuaJIT 2.1.-alpha -- Copyright (C) - Mike Pall. http://luajit.org/

1.5 安装ngx_openresty

[root@localhost LuaJIT-2.1-]# cd /usr/servers/ngx_openresty-1.7.7.2
[root@localhost ngx_openresty-1.7.7.2]# pwd
/usr/servers/ngx_openresty-1.7.7.2
[root@localhost ngx_openresty-1.7.7.2]# ./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3./ -j2
[root@localhost ngx_openresty-1.7.7.2]# make && make install

  至此,基本的环境已经安装完成了,nginx可执行文件为:/usr/servers/nginx/sbin/nginx,可以通过/usr/servers/nginx/sbin/nginx -V 命令来查看nginx的版本及安装的模块。

二、环境配置及示例

2.1 配置nginx.conf

  nginx.conf是Nginx的主配置文件,所有的配置都是从这里开始的。我的计划是将lua脚本、依赖库配置、与lua相关的server、location等配置都放到外部的目录中,这样不用每次都动到nginx.conf。修改/usr/servers/nginx/conf/nginx.conf,如下:

[root@localhost ~]# cat /usr/servers/nginx/conf/nginx.conf
worker_processes ; error_log logs/error.log;
events {
worker_connections ;
} http { include mime.types;
default_type text/html;
#lua模块路径
lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/example/lualib/?.so;;"; #c模块
include /usr/example/example.conf;
}

2.2 配置example.conf

[root@localhost ~]# mkdir /usr/example
[root@localhost ~]# vim /usr/example/example.conf
[root@localhost ~]# cat /usr/example/example.conf
server {
listen ;
server_name _; location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/example/lua/test.lua;
}
}

2.3 示例——test.lua

[root@localhost ~]# mkdir /usr/example/lua
[root@localhost ~]# vim /usr/example/lua/test.lua
[root@localhost ~]# cat /usr/example/lua/test.lua
ngx.say("hello world");

2.4 验证

  首先启动nginx,你会发现nginx给了一个警告,这句警告是因为在/usr/example/example.conf的第7行上,我们关闭了lua_code_cache。这个配置默认是打开的,即Nginx在启动的时候会将lua脚本都cache起来,这样系统在调用这些脚本时就会很快。而关闭这个配置的话,nginx每次调用都会重新load一遍,会对性能有一定的影响。因为我们是在开发环境,我们不希望更改脚本之后就重启nginx,所以就把这个配置给关闭了。

[root@localhost ~]# /usr/servers/nginx/sbin/nginx
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/example/example.conf:

接下来在浏览器中访问对应的路径,你会得到如下的结果。(提示:若没有出现,可以检查一下系统防火墙)

备注:

centos下还可以通过yum的方式安装openresty,但在国内的网络环境下,这种方式时灵时不灵(国外的主机上就没有问题),我怀疑是国内网络的问题。有兴趣的同学可以尝试下。具体命令如下:

yum install yum-utils
yum-config-manager --add-repo https://openresty.org/yum/centos/OpenResty.repo
yum install openresty

安装OpenResty开发环境的更多相关文章

  1. 安装Nginx+Lua+OpenResty开发环境配置全过程实例

    安装Nginx+Lua+OpenResty开发环境配置全过程实例 OpenResty由Nginx核心加很多第三方模块组成,默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用. ...

  2. Linux安装LAMP开发环境及配置文件管理

    Linux主要分为两大系发行版,分别是RedHat和Debian,lamp环境的安装和配置也会有所不同,所以分别以CentOS 7.1和Ubuntu 14.04做为主机(L) Linux下安装软件,最 ...

  3. win使用MSYS2安装Qt开发环境

    原文链接 MSYS2 下载地址: pacman的具体用法 有pacman的具体使用方法.我们首先对系统升级 我们首先对系统升级 pacman -Syu 就会检测整个系统可以升级的组件,并自动下载安装, ...

  4. Appium移动自动化测试(二)--安装Android开发环境

    继续Appium环境的搭建. 第二节  安装Android开发环境 如果你的环境是MAC那么可以直接跳过这一节.就像我们在用Selenium进行web自动化测试的时候一样,我们需要一个浏览器来执行测试 ...

  5. Appium移动自动化测试(二)--安装Android开发环境(转)

    Appium移动自动化测试(二)--安装Android开发环境 2015-06-04 17:30 by 虫师, 35299 阅读, 23 评论, 收藏, 编辑 继续Appium环境的搭建. 第二节   ...

  6. 如何用docker安装laravel开发环境

    如何用docker安装laravel开发环境 看laravel框架的官方文档安装部分时,发现需要安装特别多软件,估计还有许多复杂的配置,官方推荐使用Laravel Homestead虚拟机进行安装,但 ...

  7. [树莓派(raspberry pi)] 02、PI3安装openCV开发环境做图像识别(详细版)

    前言 上一篇我们讲了在linux环境下给树莓派安装系统及入门各种资料 ,今天我们更进一步,尝试在PI3上安装openCV开发环境. 博主在做的过程中主要参考一个国外小哥的文章(见最后链接1),不过其教 ...

  8. 一键安装Android开发环境

    一键安装Android开发环境 1 下载tadp-3.0r4-linux-x64.run 进入下面的地址下载: https://developer.nvidia.com/gameworksdownlo ...

  9. 阿里云学生服务器搭建网站-Ubuntu16.04安装php开发环境

    阿里云学生服务器搭建网站(2)-Ubuntu16.04安装php开发环境  优秀博文:https://www.linuxidc.com/Linux/2016-10/136327.htm https:/ ...

随机推荐

  1. [踩坑日记]spring mvc

    目录 找不到javax.servlet.ServletException的类文件 idea 清除tomcat缓存 IOException parsing XML document from Servl ...

  2. JS中常用的Math方法

    1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...

  3. Linux i2c子系统(二) _通过i2c-dev.c访问设备的方法

    另外一种驱动 应用层除了使用上述的使用i2c_driver接口来访问i2c设备,Linux内核还提供了一种简单粗暴的方式--直接通过虚拟i2c设备驱动的方式,即上一篇中的i2c-dev提供的方式,这种 ...

  4. re:从零开始的数位dp

    起源:唔,,前几天打cf,edu50那场被C题虐了,决定学学数位dp.(此文持续更新至9.19) ps:我也什么都不会遇到一些胡话大家不要喷我啊... 数位dp问题:就是求在区间l到r上满足规定条件的 ...

  5. java学习之路--简单基础的面试题

    1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...

  6. java对excel文件内容读写修改操作

    Read.java package domain; import java.io.FileInputStream; import java.io.InputStream; import jxl.Cel ...

  7. React 学习之路 (一)

    先说一说对React的体验,总结 首先react相对angular来说入手简单暴力,在学习的这段时间里发现: 我们每天做的事就是在虚拟DOM上创建元素然后在渲染到真实的DOM中 渲染到真实DOM上的R ...

  8. 概率DP求解例题

    1,逆推状态:山东省赛2013年I题 Problem I: The number of steps Description Mary stands in a strange maze, the maz ...

  9. [No0000195]NoSQL还是SQL?这一篇讲清楚

    随着大数据时代的到来,越来越多的网站.应用系统需要支撑海量数据存储,高并发.高可用.高可扩展性等特性要求. 传统的关系型数据库在应付这些已经显得力不从心,并暴露了许多难以克服的问题. 由此,各种各样的 ...

  10. Postman 快速入门之脚本

    1.学习中心,官方文档 https://learning.getpostman.com/docs/postman/scripts/test_scripts/ 2.基于Postman的API自动化测试 ...