OpenResty简介

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搭建

关于OpenResty的搭建,可以参考官方提供的网址进行搭建。http://openresty.org/cn/installation.html,我们采用源码安装的方式进行安装。

官方提供了源码安装的方式:http://openresty.org/cn/linux-packages.html

  • 安装依赖库:
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel pcre- devel gcc openssl openssl-devel per perl wget
  • 下载安装包:
wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
  • 解压安装包
tar -xf openresty-1.11.2.5.tar.gz
  • 进入安装包安装
#进入安装包 
cd openresty-1.11.2.5 
#安装 注意空格,具体名字按照说明来;需要安装ngx_cache_purge 简单安装的话可以不需要该工具
./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_stub_status_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --add-module=/usr/local/openrestyDir/ngx_cache_purge-2.3/
#编译并安装 
make && make install

说明:

--prefix=/opt/openresty:安装路径 
--with-luajit:安装luajit相关库,luajit是lua的一个高效版,LuaJIT的运行速度比标准Lua快数十 倍。
--without-http_redis2_module:现在使用的Redis都是3.x以上版本,这里不推荐使用Redis2,表示 不安装redis2支持的lua库 
--with-http_stub_status_module:Http对应状态的库 
--with-http_v2_module:对Http2的支持 
--with-http_gzip_static_module:gzip服务端压缩支持 
--with-http_sub_module:过滤器,可以通过将一个指定的字符串替换为另一个字符串来修改响应 
--add-module=/usr/local/openrestyDir/ngx_cache_purge-2.3/:Nginx代理缓存清理工具 需要提前下载解压

关于每个模块的具体作用,大家可以参考腾讯云的开发者手册:https://cloud.tencent.com/developer/doc/1158

安装完成后,在 /usr/local/openrestry/nginx 目录下是安装好的nginx,以后我们将在该目录的nginx下实现网站发布。

centos openresty centos openresty 搭建网站_java

  • 配置环境变量:
vi /etc/profile 
export PATH=/usr/local/openresty/nginx/sbin:$PATH 
source /etc/profile
  • 开启启动

linux系统结构 /lib/systemd/system/ 目录,该目录自动存放启动文件的配置位置,里面一般包含有xxx.service ,例如 systemctl enable nginx.service ,就是调用/lib/systemd/system/nginx.service 文件,使nginx开机启动。

我们可以创建 /usr/lib/systemd/system/nginx.service ,在该文件中编写启动nginx脚本:

[Service] 
Type=forking 
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid 
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t 
ExecStart=/usr/local/openresty/nginx/sbin/nginx 
ExecReload=/bin/kill -s HUP $MAINPID 
ExecStop=/bin/kill -s QUIT $MAINPID 
PrivateTmp=true 

[Install] 
WantedBy=multi-user.target

执行systemctl daemon-reload :重新加载某个服务的配置文件

执行 systemctl enable nginx.service:开机启动

执行systemctl start nginx.service :启动nginx

访问自己服务器的地址即可,效果如下:(nginx默认端口80)

centos openresty centos openresty 搭建网站_lua_02