Linux Compiler installation Openresty

Source: Internet
Author: User
Tags curl lua redis unix domain socket

What is the relationship between Nginx Tengine openresty?

Tengine is equivalent to Nginx's two development, making some changes that add unique features
Openresty is an enhanced version of Nginx that extends a number of modules, featuring the LUA support module and, of course, a very large number of other modules, with the Nginx core using the original Nginx, and the newer mainline version used, For example, the Nginx version contained in 1.9.3.2 is nginx-1.9.3 mainline

Compile and install 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可以看看原来的编译参数, add up here.
#/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
# after install the original/usr/local/nginx/sbin/nginx will be CP into/usr/local/nginx/sbin/nginx.old
/usr/local/nginx/sbin/nginx-v
#检查是否安装成功
/usr/local/nginx/sbin/nginx-t
#测试配置文件是否通过

/etc/init.d/nginx Reload

Nginx Configuration nginx.conf

/usr/local/openresty/nginx/conf/nginx.conf
# Add MySQL configuration (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 "expecting \" name\ "or \" id\ "query arguments";
}

# match name with URL, encode to prevent injection, and finally output the result in JSON format
Location ~ ' ^/mysql/(. *) ' {
Set $name $;
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;
}

# View MySQL Service status
Location/mysql-status {
Drizzle_status;
}
}


iv. LUA test Scripts


/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 ten rows in
--The result set:
Res, err, errno, SQLState =
Db:query ("SELECT * FROM cats-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 ten 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
';

V. Results of verification

Curl Test
$ 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": "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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.