centos7.6安装openresty

安装详情在官网http://openresty.org/cn/installation.html

一、预编译包的方式安装

1、安装前的准备

#add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/

#update the yum index:
sudo yum check-update

2、下载安装
安装软件包sudo yum install -y openresty
如果想安装命令行工具 sudo yum install -y openresty-resty
程序会被安装到/usr/local/openresty目录
我们可以查看所有 openresty 仓库里头的软件包sudo yum --disablerepo="*" --enablerepo="openresty" list available
openresty -v可以查到版本是openresty/1.19.3.1
二、源码方式安装

1、安装前的准备
Fedora 和 RedHat 用户,推荐您使用yum安装以下的开发库(我用的centos7.6mini)
yum install pcre-devel openssl-devel gcc curl

2、下载OpenRest源码包
从地址http://openresty.org/cn/download.html下载,我下载了openresty-1.19.3.1.tar.gz
3、解压
我放到了/root目录下面,所以先cd /root
再解压tar -xzvf openresty-1.19.3.1.tar.gz
4、进入解压后的目录执行./configure
cd openresty-1.19.3.1
执行./configure
可能会报错/usr/bin/env: perl: No such file or directory,安装perl就是了yum install perl
默认, --prefix=/usr/local/openresty 程序会被安装到/usr/local/openresty目录。
配置文件(./configure script)运行出错可以到 build/nginx-1.19.3.1/objs/autoconf.err 找到。
5、编译和安装
make
sudo make install

三、配置
cd /usr/local/openresty/
输入ll可查看到各种文件
cd nginx/conf
vi nginx.conf并按i进入编辑状态
在第一行添加user root root;表示会在root目录下找lua脚本
把server_name的localhost改成我虚拟机中centos的IP地址192.168.3.160
放开pid logs/nginx.pid这行的注释
esc退出编辑状态,输入:wq保存并退出文件

四、启动
使用nginx -c的参数指定nginx.conf文件的位置
在sbin/ 路径下执行 ./nginx -c /usr/local/openresty/nginx/conf/nginx.conf
进入cd /usr/local/openresty/nginx/sbin
启动程序./nginx
输入ps -ef | grep "nginx"可看到进程了

五、访问
在命令行输入curl http://192.168.3.160可得到默认的返回的html文本
开放80端口firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙firewall-cmd --reload
查看开放的端口列表iptables-save
在win10中打开浏览器输入http://192.168.3.160访问centos就可以看到Welcome to OpenResty!

六、测试执行Lua脚本
新建目录mkdir /root/lua_script
进入目录cd /root/lua_script
新建文件vi test.lua
输入以下lua脚本

ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local mysql = require("resty.mysql")

local db = mysql:new()
db:set_timeout(1000)  
local props = {
    
      
    host = "192.168.3.160",  
    port = 3306,  
    database = "UserDb",  
    user = "root",  
    password = "root"  
}  

local res = db:connect(props)
local select_sql = "select * from UserInfo"  
res = db:query(select_sql)  
db:close()  

ngx.say(cjson.encode(res))

修改nginx配置文件vi /usr/local/openresty/nginx/conf/nginx.conf
输入i进入编辑状态
添加以下代码

location /test {
    
    
    content_by_lua_file /root/lua_script/test.lua;
}

esc结束编辑,输入:wq保存并退出
进入cd /usr/local/openresty/nginx/sbin
重启./nginx -s reload
在win10中打开浏览器输入http://192.168.3.160/test访问centos就可以看到查到用户表的数据了。

七、opm包管理器的使用
opm是官方的OpenResty软件包管理器,类似于NodeJS的npm。从1.11.2.2开始,OpenResty版本已经包含并默认安装opm。但是如果使用的是官方的OpenResty 预制linux软件包,则应该安装 openresty-opm软件包,因为 openresty二进制软件包本身不包含opm。
opm依赖 perl,安装perl
yum install perl
yum -y install perl-Digest-MD5
https://github.com/openresty/opm下载源码,然后只需将bin里面的文件opm复制到 /usr/local/openresty/bin里面,
curl https://raw.githubusercontent.com/openresty/opm/master/bin/opm > /usr/local/openresty/bin/opm

chmod +x /usr/local/openresty/bin/opm
export PATH=/usr/local/openresty/bin:$PATH
如果您使用的是默认情况下不包含的旧版本的OpenResty opm,则还应该创建以下目录:
cd /usr/local/openresty
mkdir -p site/lualib site/manifest site/pod
输入opmopm --help就可看到相关使用说明了

我们可以在网页上搜索想要的包https://opm.openresty.org/docs
也可以在命令行搜索,例如搜索lua-resty-redis-connector
和 npm 正好相反, npm install 默认安装本地包, 加上 -g 才是安装全局包, 而 opm 则默认安装全局包
搜索包opm search resty.redis.connector
下载包opm get ledgetech/lua-resty-redis-connector=0.10
然后在/usr/local/openresty/site/lualib/resty/redis里就可看到connector.lua和sentinel.lua

修改/root/lua_script/test.lua代码,并加入操作redis的代码(我这里用的redis哨兵sentinel集群)
vi /root/lua_script/test.lua
然后输入i进入编辑模式
添了如下代码,其中m表示master角色,0表示第一个redis库

local redis, err = require("resty.redis.connector").new({
    
    
    url = "sentinel://abc123456@mymaster:m/0",
    sentinels = {
    
    
        {
    
     host = "192.168.3.113", port = 26379 },
        {
    
     host = "192.168.3.149", port = 26379 },
        {
    
     host = "192.168.3.160", port = 26379 },
    }
}):connect()

redis:set("mydata",cjson.encode(res))
redis:close()

ngx.say("{\"msg\":\"success\"}")

esc结束编辑,输入:wq保存并退出
进入cd /usr/local/openresty/nginx/sbin
重启./nginx -s reload
在win10中打开浏览器输入http://192.168.3.160/test访问centos就可以看到msg:success了,并且使用redis桌面工具也可看到数据成功存到redis里了。

猜你喜欢

转载自blog.csdn.net/junshangshui/article/details/114106135