当前位置: 首页 > news >正文

安装OpenResty开发环境

  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-20150120/
[root@localhost LuaJIT-2.1-20150120]# pwd
/usr/servers/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120
[root@localhost LuaJIT-2.1-20150120]# make clean && make && make install  
[root@localhost LuaJIT-2.1-20150120]# ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit  
[root@localhost LuaJIT-2.1-20150120]# lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
[root@localhost LuaJIT-2.1-20150120]# luajit -v
LuaJIT 2.1.0-alpha -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/

1.5 安装ngx_openresty

[root@localhost LuaJIT-2.1-20150120]# 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.0/ -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
1; error_log logs/error.log; events { worker_connections 1024; } 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       80;
    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:7

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

 

备注:

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

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

 

相关文章:

  • Vc中的文件路径问题
  • Servlet Filter
  • 通过WCF服务,采用多线程技术上传大文件到SharePoint文档库解决方案(初稿)
  • 6月21日云栖精选夜读:国内首位!Node.js社区将阿里云工程师张秋怡吸纳为CTC成员...
  • ASP.NET存储Session的StateServer
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • 在VC中使用自定义资源,FindResource,LoadResource,LockResource
  • 第0课 从0开始
  • Table转换Div+CSS_工具下载
  • 创网杯知识竞赛的软件源码
  • PHP扩展之STOMP-安装
  • IP地址获得主机名称
  • python class和class(object)用法区别
  • Eclipse Error Cannot change version of project facet Dynamic Web Moudle to 3.0
  • asp.net控件开发基础(1)
  • [PHP内核探索]PHP中的哈希表
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • Angular 响应式表单 基础例子
  • css属性的继承、初识值、计算值、当前值、应用值
  • ES6语法详解(一)
  • Go 语言编译器的 //go: 详解
  • Java反射-动态类加载和重新加载
  • vuex 学习笔记 01
  • webpack+react项目初体验——记录我的webpack环境配置
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 前端面试之CSS3新特性
  • 嵌入式文件系统
  • 深入浏览器事件循环的本质
  • 思维导图—你不知道的JavaScript中卷
  • 在weex里面使用chart图表
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #QT项目实战(天气预报)
  • #vue3 实现前端下载excel文件模板功能
  • (06)金属布线——为半导体注入生命的连接
  • (3)nginx 配置(nginx.conf)
  • (9)STL算法之逆转旋转
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (五)MySQL的备份及恢复
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .net mvc部分视图
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • /proc/stat文件详解(翻译)
  • [ vulhub漏洞复现篇 ] struts2远程代码执行漏洞 S2-005 (CVE-2010-1870)
  • []利用定点式具实现:文件读取,完成不同进制之间的
  • [<MySQL优化总结>]
  • [1]-基于图搜索的路径规划基础
  • [BUUCTF NewStarCTF 2023 公开赛道] week4 crypto/pwn
  • [CC2642r1] ble5 stacks 蓝牙协议栈 介绍和理解
  • [CentOs7]搭建ftp服务器(2)——添加用户
  • [EFI]英特尔 冥王峡谷 NUC8i7HVK 电脑 Hackintosh 黑苹果efi引导文件