跳至主要內容

安装OpenResty

soulballad环境配置CentOSCentOS约 1234 字大约 4 分钟

安装OpenResty

1. 简介

OpenResty 是一个基于 Nginxopen in new window 与 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 等都进行一致的高性能响应。

2. 在线安装

OpenResty 官方现在开始维护自己的打包虚机集合了,新的 linux 包仓库正在陆续登陆 openresty.org 官网。欢迎大家试用!原来老源的用户可以先禁用掉老的 openresty 源。

在 CentOS 系统上使用新的官方 yum 源:

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

# sudo yum install -y yum-utils
yum --disablerepo="*" --enablerepo="openresty" list available

在线安装 openstry

yum install openresty -y

默认会安装到 /usr/local/openresty/ 目录下, 目录下包含了 luajit, lualib, nginx, openssl, pcre, zlib 这些组件

3. 编译安装

关于OpenResty的搭建,可以参考官方提供的网址进行搭建。http://openresty.org/cn/installation.htmlopen in new window

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

1603446296664

安装OpenResty:

1)安装依赖库:

yum install libtermcap-devel ncurses-devel libevent-devel readline-devel pcre-devel gcc openssl openssl-devel per perl wget

2)下载安装包:

wget https://openresty.org/download/openresty-1.11.2.5.tar.gz

3)解压安装包

tar -xf openresty-1.11.2.5.tar.gz

4)进入安装包,并安装

#进入安装包
cd openresty-1.11.2.5

#安装
./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/gupao/ngx_cache_purge-2.3/

#编译并安装
make && make install

说明:

  • --prefix=/usr/local/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/gupao/ngx_cache_purge-2.3/: Nginx代理缓存清理工具

关于每个模块的具体作用,大家可以参考 腾讯云开发者手册open in new window

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

1603448530004

4. 安装luasocket

如果说要远程调试Lua ,也就是说在Lua下使用 socket,需要安装 luasocket。主要是提供网络方面的扩展,提供了包括tcp、udp、http、ftp、smtp等协议的支持,并且支持跨平台。使用它能够很容易的在lua进行网络程序的开发。

linux下,luasocket需要使用luajit,那么则需要下载源码编译。

可以参考官方文档open in new window。首先进入/work的目录,然后从官网的网址上面根据路径下载安装包。

在线下载 wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz

然后编译安装

tar -zxvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4/
make
make install

默认的安装路径是/usr/local/include/luajit-2.0/,这个下一步要用到,否则会出错

接下来安装 luasocket, 下载luasocket-2.0.2则是源码, 可以到Github下载open in new window

wget http://files.luaforge.net/releases/luasocket/luasocket/luasocket-2.0.2/luasocket-2.0.2.tar.gz
tar -zxvf luasocket-2.0.2.tar.gz
cd luasocket-2.0.2/
make LUAINC=-I/usr/local/include/luajit-2.0/
make install
ll /usr/local/lib/lua/5.1/

5. 设置开机启动

配置环境变量:

vi /etc/profile

export NGINX_PATH=/usr/local/openresty/nginx
export PATH=$NGINX_PATH/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脚本:

Description=nginx
After=network.target

[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=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s quit
#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

1603449397840

访问http://192.168.100.130/,效果如下:

1603449630551

参考:

上次编辑于:
贡献者: soulballad