在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Redis下载地址:wget http://download.redis.io/releases/redis-2.8.6.tar.gz 2、安装依赖包 yum install -y gcc gcc-c++ readline-devel pcre-devel openssl-devel tcl perl
3、安装OpenResty 首先安装LuaJIT wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar zxvf LuaJIT-2.0.2.tar.gz
make
make install
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
安装OpenResty tar zxvf ngx_openresty-1.4.3.6.tar.gz cd ngx_openresty-1.4.3.6 ./configure --with-luajit make make install lua和redis等相关模块就已经安装完成,利用下面命令查看。 /usr/local/openresty/nginx/sbin/nginx -V 设置Nginx为服务和开机启动 vi /etc/rc.d/init.d/nginx #!/bin/bash # Tengine Startup script# processname: nginx # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve # pidfile: /var/run/nginx.pid # config: /usr/local/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 "tengine 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 /usr/local/nginx/logs/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 保存退出 chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限 chkconfig --level 012345 nginx on #设置开机启动 service nginx start 4、安装Redis tar zxvf redis-2.8.6.tar.gz
mv redis-2.8.6 redis cd redis make MALLOC=libc make install 设置Redis的配置文件 vi /usr/local/redis/redis.conf #是否作为守护进程运行 daemonize yes #如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid pidfile redis.pid #绑定主机IP,默认值为127.0.0.1 #bind 127.0.0.1 #Redis默认监听端口 port 6379 #客户端闲置多少秒后,断开连接,默认为300(秒) timeout 300 #日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning loglevel verbose #指定日志输出的文件名,默认值为stdout,也可设为/dev/null屏蔽日志 logfile stdout #可用数据库数,默认值为16,默认数据库为0 databases 16 #保存数据到disk的策略 #当有一条Keys数据被改变是,900秒刷新到disk一次 save 900 1 #当有10条Keys数据被改变时,300秒刷新到disk一次 save 300 10 #当有1w条keys数据被改变时,60秒刷新到disk一次 save 60 10000 #当dump .rdb数据库的时候是否压缩数据对象 rdbcompression yes #本地数据库文件名,默认值为dump.rdb dbfilename dump.rdb #本地数据库存放路径,默认值为 ./ dir ./ 调整内存分配策略 查看一下内存分配策略 cat /proc/sys/vm/overcommit_memory 注意用vi或vim是不能修改里面的值的,需要使用下面的方法 (无需重启服务器就可生效) echo "1" >>/proc/sys/vm/overcommit_memory 该文件指定了内核针对内存分配的策略,其值可以是0、1、2。 启动服务 redis-server /usr/local/redis/redis.conf 通过redis-cli命令来实际操作一下 关闭服务 redis-cli shutdown 如果端口变化可以指定端口 redis-cli -p 6380 shutdown 保存/备份 设置Redis开机自启 vi /etc/rc.d/init.d/redis #!/bin/bash # # redis - this script starts and stops the redis-server daemon # # chkconfig: - 80 12 # description: Redis is a persistent key-value database # processname: redis-server # config: /etc/redis/redis.conf # pidfile: /var/run/redis.pid source /etc/init.d/functions BIN="/usr/local/bin" CONFIG="/usr/local/redis/redis.conf" PIDFILE="/var/run/redis.pid" ### Read configuration [ -r "$SYSCONFIG" ] && source "$SYSCONFIG" RETVAL=0 prog="redis-server" desc="Redis Server" start() { if [ -e $PIDFILE ];then echo "$desc already running...." exit 1 fi echo -n $"Starting $desc: " daemon $BIN/$prog $CONFIG RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog return $RETVAL } stop() { echo -n $"Stop $desc: " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE return $RETVAL } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e /var/lock/subsys/$prog ] && restart RETVAL=$? ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" RETVAL=1 esac exit $RETVAL 保存退出 chmod 777 /etc/rc.d/init.d/redis chkconfig redis on service redis start #stop,restart 5、在Nginx中调用lua脚本简单读取Redis中的数据 vi /usr/local/openresty/nginx/conf/nginx.conf 在server配置节中增加 location /test { default_type text/plain; content_by_lua_file /usr/local/test.lua; } 保存退出,然后创建lua脚本文件 vi /usr/local/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) 保存退出,重启Nginx service nginx restart 6、测试 修改Nginx中html目录下的index.html文件来测试,增加JQuery引用和Ajax请求 <script type="text/javascript" src="jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function() { $.ajax({ type : "GET", url : "http://192.168.0.106/test", success : function(data) { alert(data); } }); }); </script> 访问Nginx地址 http://192.168.0.106/,如果key值为foo没有数据时会弹出 利用redis-cli命令向Redis中增加数据 redis-cli set foo test
再访问地址会弹出 |
请发表评论