linux中编译安装openresty

nginx tengine openresty之间是什么关系?

tengine相当于是nginx的二次开发,做了一些改动,增加了独有的一些功能
openresty是nginx的增强版,扩展了很多模块,特色是引入了lua支持模块,当然还有非常多个其他的模块,nginx核心使用的是原版nginx,并且使用的较新的mainline版本,比如1.9.3.2中包含的nginx版本为nginx-1.9.3 mainline

编译安装openresty

wget https://openresty.org/download/openresty-1.9.7.4.tar.gz
tar zxvf openresty-1.9.7.4.tar.gz
cd openresty-1.9.7.4
#如果已经安装过nginx可以看看原来的编译参数,这里增加上去
#/usr/local/nginx/sbin/nginx -V
./configure --user=www --group=www --prefix=/usr/local --with-luajit --with-http_iconv_module  --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module
make
make install
# install后原来的 /usr/local/nginx/sbin/nginx 会被cp 成  /usr/local/nginx/sbin/nginx.old
/usr/local/nginx/sbin/nginx -v
#检查是否安装成功
/usr/local/nginx/sbin/nginx -t
#测试配置文件是否通过

/etc/init.d/nginx reload

nginx配置nginx.conf
 
/usr/local/openresty/nginx/conf/nginx.conf
# 添加MySQL配置(drizzle)
upstream backend {
    drizzle_server 127.0.0.1:3306 dbname=test user=root password=123456 protocol=mysql;
    drizzle_keepalive max=200 overflow=ignore mode=single;
}

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    location /lua {
        default_type text/plain;
        content_by_lua 'ngx.say("hello, lua")';
    }

    location /lua_redis {
        default_type text/plain;
        content_by_lua_file /usr/local/lua_test/redis_test.lua;
    }

    location /lua_mysql {
            default_type text/plain;
            content_by_lua_file /usr/local/lua_test/mysql_test.lua;
    }

    location @cats-by-name {
        set_unescape_uri $name $arg_name;
        set_quote_sql_str $name;
        drizzle_query 'select * from cats where name=$name';
        drizzle_pass backend;
        rds_json on;
    }

    location @cats-by-id {
        set_quote_sql_str $id $arg_id;
        drizzle_query 'select * from cats where id=$id';
        drizzle_pass backend;
        rds_json on;
    }

    location = /cats {
        access_by_lua '
            if ngx.var.arg_name then
                return ngx.exec("@cats-by-name")
            end

            if ngx.var.arg_id then
                return ngx.exec("@cats-by-id")
            end
        ';

        rds_json_ret 400 "expecting \"name\" or \"id\" query arguments";
    }

    # 通过url匹配出name,并编码防止注入,最后以json格式输出结果
    location ~ '^/mysql/(.*)' {
        set $name $1;
        set_quote_sql_str $quote_name $name;
        set $sql "SELECT * FROM cats WHERE name=$quote_name";
        drizzle_query $sql;
        drizzle_pass backend;
        rds_json on;
    }

    # 查看MySQL服务状态
    location /mysql-status {
        drizzle_status;
    }
}

 
四、lua测试脚本

/usr/local/lua_test/redis_test.lua

local redis = require "resty.redis"
local cache = redis.new()
cache.connect(cache, '127.0.0.1', '6379')
local res = cache:get("foo")
if res==ngx.null then
    ngx.say("This is Null")
    return
end
ngx.say(res)

 

/usr/local/lua_test/mysql_test.lua

local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
    ngx.say("failed to instantiate mysql: ", err)
    return
end

db:set_timeout(1000) -- 1 sec

-- or connect to a unix domain socket file listened
-- by a mysql server:
--     local ok, err, errno, sqlstate =
--           db:connect{
--              path = "/path/to/mysql.sock",
--              database = "ngx_test",
--              user = "ngx_test",
--              password = "ngx_test" }

local ok, err, errno, sqlstate = db:connect{
    host = "127.0.0.1",
    port = 3306,
    database = "test",
    user = "root",
    password = "123456",
    max_packet_size = 1024 * 1024 }

if not ok then
    ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
    return
end

ngx.say("connected to mysql.")

local res, err, errno, sqlstate =
    db:query("drop table if exists cats")
if not res then
    ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
    return
end

res, err, errno, sqlstate =
    db:query("create table cats "
             .. "(id serial primary key, "
             .. "name varchar(5))")
if not res then
    ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
    return
end

ngx.say("table cats created.")

res, err, errno, sqlstate =
    db:query("insert into cats (name) "
             .. "values (\'Bob\'),(\'\'),(null)")
if not res then
    ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
    return
end

ngx.say(res.affected_rows, " rows inserted into table cats ",
        "(last insert id: ", res.insert_id, ")")

-- run a select query, expected about 10 rows in
-- the result set:
res, err, errno, sqlstate =
    db:query("select * from cats order by id asc", 10)
if not res then
    ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
    return
end

local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))

-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(10000, 100)
if not ok then
    ngx.say("failed to set keepalive: ", err)
    return
end

-- or just close the connection right away:
-- local ok, err = db:close()
-- if not ok then
--     ngx.say("failed to close: ", err)
--     return
-- end
';

 

五、验证结果

curl测试
$ curl 'http://127.0.0.1/lua_test'
hello, lua

$ redis-cli set foo 'hello,lua-redis'
OK
$ curl 'http://127.0.0.1/lua_redis'
hello,lua-redis

$ curl 'http://127.0.0.1/lua_mysql'
connected to mysql.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]

$ curl 'http://127.0.0.1/cats'
{"errcode":400,"errstr":"expecting \"name\" or \"id\" query arguments"}

$ curl 'http://127.0.0.1/cats?name=bob'
[{"id":1,"name":"Bob"}]

$ curl 'http://127.0.0.1/cats?id=2'
[{"id":2,"name":""}]

$ curl 'http://127.0.0.1/mysql/bob'
[{"id":1,"name":"Bob"}]

$ curl 'http://127.0.0.1/mysql-status'
worker process: 32261

upstream backend
  active connections: 0
  connection pool capacity: 0
  servers: 1
  peers: 1

时间: 2024-08-01 23:39:50

linux中编译安装openresty的相关文章

Linux中编译安装MemcacheQ的步骤详解

队列(Queue)是一种常用的数据结构.在队列这种数据结构中,最先插入的元素将会最先被取出:反之最后插入的元素将会最后被取出,因此队列又称为"先进先出"(FIFO:First In First Out)的线性表. 加入元素的一端叫"队尾",取出元素的一端叫"队头".利用消息队列可以很好地异步处理数据的传送和存储,当遇到频繁且密集地向后端数据库中插入数据时,就可采用消息队列来异步处理这些数据写入. MemcacheQ是一款基于Memcache协议的

linux中编译安装PHP7并安装Redis扩展Swoole扩展

编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机器上已经有编译安装过php5.3以上的版本,从而依赖库都有了 本php7是编译成fpm-php 使用的, 如果是apache那么编译参数应该为 --with-apxs2=/usr/local/apache/bin/apxs 编译安装php7 wget -c http://www.php.net/distributions/php-7.0.0.tar.gz tar zxvf php-7.0.0.tar.gz cd php-7.

linux中编译安装和配置nginx的教程

最近 @大高个 在折腾nginx,一时性起我就在深夜里写下了这篇博文,以记下了我曾经折腾nginx的那些不悔青春,这里以centos为例: 目录约定 首先约定下一些常用的目录,如: # 根目录挂载 /home/     # 安装包目录,存放一些常用的安装包     ./src/         ./nginx-1.11.1.tar         ./node-6.2.2.tar         ./zlib-1.2.8.tar.gz         ...     # 程序目录,存放一些编译后

linux中编译安装Varnish3.03及配置

这是关于Varnish3.03编译安装配置的笔记 安装  代码如下 复制代码 yum -y install gcc gcc-c++ file bison patch unzip mlocate flex wget diffutils automake autoconf kernel-devel gd cpp readline-devel openssl openssl-devel vim-minimal nano libjpeg libjpeg       -devel libpng libpn

linux中编译安装php的参数

php-5.2.14版本  代码如下 复制代码 ./configure –prefix=/usr/local/php-5.2.14 –with-config-file-path=/usr/local/php-5.2.14/etc –with-gd –with-iconv –with-zlib –enable-xml –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curlwrappers

linux平台编译安装PHP7并安装Redis扩展与Swoole扩展实例教程_php技巧

本文实例讲述了linux平台编译安装PHP7并安装Redis扩展与Swoole扩展的方法.分享给大家供大家参考,具体如下: 前面<PHP7安装Redis扩展教程[Linux与Windows平台]>一文告诉读者简单的安装Redis的方法,下面我们来实现在linux中编译安装PHP7并安装Redis扩展与Swoole扩展的方法. 编译安装PHP7并安装Redis扩展Swoole扩展: 在编译php7的机器上已经有编译安装过php5.3以上的版本,从而依赖库都有了 本php7是编译成fpm-php

Linux 有问必答:如何在Ubuntu或者Debian中编译安装ixgbe驱动

Linux 有问必答:如何在Ubuntu或者Debian中编译安装ixgbe驱动 提问: 我想为我的Intel 10G网卡下载安装最新的ixgbe驱动.我该如何在Ubuntu(或者Debian)中安装ixgbe驱动? Intel的10G网卡(比如,82598. 82599. x540)由ixgbe驱动支持.现代的Linux发行版已经带有了ixgbe驱动,通过可加载模块的方式使用.然而,有些情况你希望在你机器上的自己编译安装ixgbe驱动,比如,你想要体验ixbge驱动的最新特性时.同样,内核默认

linux qt4-RedHat企业版6.2中编译安装Qt4.8.5后,进入/tools/qvfb中gmake出现以下错误

问题描述 RedHat企业版6.2中编译安装Qt4.8.5后,进入/tools/qvfb中gmake出现以下错误 有懂的人给看看是怎么回事,急求高手解答.错误如下:.obj/release-shared/qanimationwriter.o: In function QAnimationWriter::QAnimationWriter(QString const&, char const*)': qanimationwriter.cpp:(.text+0x9ec): undefined refe

Linux下编译安装php libevent扩展实例

 这篇文章主要介绍了Linux下编译安装php libevent扩展实例,本文着重讲解了编译过程中一个错误解决方法,需要的朋友可以参考下     原本想尝试一下PHP编写高性能网络服务,需要安装libevent扩展,没想到让人很费了点脑袋 先下载libevent扩展: http://pecl.php.net/package/libevent 解压后,开始编译 代码如下: $ cd libevent-version $ /usr/local/php/bin/phpize $ ./configure