openresty linux安装教程,Linux下安装配置OpenResty,并测试使用Lua编程 | zifangsky的个人博客...

一 简介

OpenResty,也被称为“ngx_openresty”,是一个以Nginx为核心同时包含很多第三方模块的Web应用服务器。借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序。 OpenResty不是Nginx的分支,它只是一个软件包。主要有章亦春维护。

OpenResty默认集成了Lua开发环境,而且提供了大量组件如Mysql、Redis、Memcached等,使得在Nginx上开发Web应用更方便简单。

二 安装OpenResty

Shell

[root@hbase31 src]# wget https://openresty.org/download/openresty-1.11.2.5.tar.gz

[root@hbase31 src]# tar -zxvf openresty-1.13.6.1.tar.gz

[root@hbase31 openresty-1.13.6.1]# ./configure --prefix=/usr/local/openresty --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/ssl --with-pcre=/usr/local/src/pcre-8.38 --add-module=/usr/local/src/ngx_cache_purge-2.3 --with-http_gzip_static_module --with-luajit

[root@hbase31 openresty-1.13.6.1]# make && make install

1

2

3

4[root@hbase31src]# wget https://openresty.org/download/openresty-1.11.2.5.tar.gz

[root@hbase31src]# tar -zxvf openresty-1.13.6.1.tar.gz

[[email protected]]# ./configure --prefix=/usr/local/openresty --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/ssl --with-pcre=/usr/local/src/pcre-8.38 --add-module=/usr/local/src/ngx_cache_purge-2.3 --with-http_gzip_static_module --with-luajit

[[email protected]]# make && make install

注:关于这里的编译参数可以认为是在Nginx的编译参数的基础上添加了其他组件的参数。如需查看更多参数可以使用以下命令:

Shell

[root@hbase31 openresty-1.13.6.1]# ./configure --help

1[[email protected]]# ./configure --help

配置nginx的启动脚本:

Shell

[root@hbase31 openresty-1.13.6.1]# vim /etc/init.d/nginx

1[[email protected]]# vim /etc/init.d/nginx

添加如下内容:

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.1.3.0 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

# It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/openresty/nginx/conf/nginx.conf

nginxd=/usr/local/openresty/nginx/sbin/nginx

nginx_config=/usr/local/openresty/nginx/conf/nginx.conf

nginx_pid=/usr/local/openresty/nginx/logs/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

echo "nginx already running...."

exit 1

fi

echo -n $"Starting $prog: "

daemon $nginxd -c ${nginx_config}

RETVAL=$?

echo

[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

return $RETVAL

}

# Stop nginx daemons functions.

stop() {

echo -n $"Stopping $prog: "

killproc $nginxd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid

}

reload() {

echo -n $"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`

killproc $nginxd -HUP

RETVAL=$?

echo

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

stop

start

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $prog {start|stop|restart|reload|status|help}"

exit 1

esac

exit $RETVAL

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.1.3.0 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/openresty/nginx/conf/nginx.conf

nginxd=/usr/local/openresty/nginx/sbin/nginx

nginx_config=/usr/local/openresty/nginx/conf/nginx.conf

nginx_pid=/usr/local/openresty/nginx/logs/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

./etc/rc.d/init.d/functions

# Source networking configuration.

./etc/sysconfig/network

# Check that networking is up.

[${NETWORKING}="no"]&&exit0

[-x$nginxd]||exit0

# Start nginx daemons functions.

start(){

if[-e$nginx_pid];then

echo"nginx already running...."

exit1

fi

echo-n$"Starting $prog: "

daemon$nginxd-c${nginx_config}

RETVAL=$?

echo

[$RETVAL=0]&&touch/var/lock/subsys/nginx

return$RETVAL

}

# Stop nginx daemons functions.

stop(){

echo-n$"Stopping $prog: "

killproc$nginxd

RETVAL=$?

echo

[$RETVAL=0]&&rm-f/var/lock/subsys/nginx$nginx_pid

}

reload(){

echo-n$"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`

killproc$nginxd-HUP

RETVAL=$?

echo

}

# See how we were called.

case"$1"in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

stop

start

;;

status)

status$prog

RETVAL=$?

;;

*)

echo$"Usage: $prog {start|stop|restart|reload|status|help}"

exit1

esac

exit$RETVAL

添加可执行权限:

Shell

[root@hbase31 openresty-1.13.6.1]# chmod a+x /etc/init.d/nginx

1[[email protected]]# chmod a+x /etc/init.d/nginx

启动nginx:

Shell

[root@hbase31 openresty-1.13.6.1]# service nginx start

1[[email protected]]# service nginx start

三 在Nginx中使用Lua脚本

Shell

[root@hbase31 vhost]# cd /usr/local/openresty/nginx/conf

[root@hbase31 conf]# mkdir lua vhost

1

2[root@hbase31vhost]# cd /usr/local/openresty/nginx/conf

[root@hbase31conf]# mkdir lua vhost

(1)测试在Nginx中使用Lua脚本:

Shell

[root@hbase31 nginx]# vim /usr/local/openresty/nginx/conf/vhost/lua.conf

1[root@hbase31nginx]# vim /usr/local/openresty/nginx/conf/vhost/lua.conf

其内容如下:

server {

server_name localhost;

listen 3000;

index index.html index.htm index.jsp;

location / {

root /usr/local/openresty/nginx/html;

}

location /lua {

default_type text/plain;

content_by_lua 'ngx.say("hello,lua!")';

}

limit_conn perip 1000;

access_log logs/access_rua.log;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17server{

server_namelocalhost;

listen3000;

indexindex.htmlindex.htmindex.jsp;

location/{

root/usr/local/openresty/nginx/html;

}

location/lua{

default_typetext/plain;

content_by_lua'ngx.say("hello,lua!")';

}

limit_connperip1000;

access_loglogs/access_rua.log;

}

测试是否可以访问:

Shell

[root@hbase31 nginx]# service nginx reload

1[root@hbase31nginx]# service nginx reload

然后访问:http://192.168.1.31:3000/lua

如果输出以下内容则证明在Nginx中可以执行Lua脚本:

hello,lua!

1hello,lua!

(2)在Nginx中使用Lua脚本访问Redis:

i)连接Redis集群,然后添加测试参数:

192.168.1.30:7000> set '123' '456'

1192.168.1.30:7000>set'123''456'

ii)添加连接Redis的Lua脚本:

Shell

[root@hbase31 nginx]# vim /usr/local/openresty/nginx/conf/lua/redis.lua

1[root@hbase31nginx]# vim /usr/local/openresty/nginx/conf/lua/redis.lua

其内容如下:

Lua

local redis = require "resty.redis"

local conn = redis.new()

conn.connect(conn, '192.168.1.30', '7000')

local res = conn:get("123")

if res==ngx.null then

ngx.say("redis集群中不存在KEY——'123'")

return

end

ngx.say(res)

1

2

3

4

5

6

7

8

9localredis=require"resty.redis"

localconn=redis.new()

conn.connect(conn,'192.168.1.30','7000')

localres=conn:get("123")

ifres==ngx.nullthen

ngx.say("redis集群中不存在KEY——'123'")

return

end

ngx.say(res)

iii)在上面的lua.conf配置文件中添加以下location:

location /lua_redis {

default_type text/plain;

content_by_lua_file /usr/local/openresty/nginx/conf/lua/redis.lua;

}

1

2

3

4location/lua_redis{

default_typetext/plain;

content_by_lua_file/usr/local/openresty/nginx/conf/lua/redis.lua;

}

iv)测试是否可以访问:

Shell

[root@hbase31 nginx]# service nginx reload

1[root@hbase31nginx]# service nginx reload

然后访问:http://192.168.1.31:3000/lua_redis

如果输出以下内容则证明可以访问redis:

456

1456

你可能感兴趣的:(openresty,linux安装教程)