1. download Openresty and 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. Install dependent packages
Yum install-y gcc gcc-c++ readline-devel pcre-devel openssl-devel tcl Perl
3, installation openresty
Install Luajit First
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gztar zxvf luajit-2.0.2.tar.gzmakemake installexport luajit_lib=/ Usr/local/libexport luajit_inc=/usr/local/include/luajit-2.0
Installing Openresty
Tar zxvf ngx_openresty-1.4.3.6.tar.gzcd ngx_openresty-1.4.3.6./configure--with-luajitmakemake Install
the relevant modules such as LUA and Redis are already installed and can be viewed using the commands below .
/usr/local/openresty/nginx/sbin/nginx-v
set Nginx as service and boot up
Vi/etc/rc.d/init.d/nginx
#!/bin/bash# tengine Startup script# processname:nginx# chkconfig:-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.confnginxd=/usr/local/openresty/ nginx/sbin/nginxnginx_config=/usr/local/openresty/nginx/conf/nginx.confnginx_pid=/usr/local/openresty/nginx/ Logs/nginx.pidretval=0prog= "Nginx" # Source function library. /etc/rc.d/init.d/functions# Source Networking configuration: /etc/sysconfig/network# Check that networking are up. [${networking} = "No"] && exit 0[-x $nginxd] | | Exit 0# Start Nginx daemons Functions.start () {If [-e $nginx _pid];thenecho "Tengine already running ...." Exit 1fiecho-n $ "Starting $prog:" Daemon $nginxd-c ${nginx_config}retval=$?echo[$RETVAL = 0] && touch/var/lock/subsys/nginxre Turn $RETVAL}# Stop nginx daemons functions.stop () {echo-n $ "stopping $prog:" Killproc $nginxdRETVAL =$?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-hupretval =$?echo}# See how we were Called.case "$" instart) start; stop) stop;; reload) reload;; restart) Stopstart;; Status) status $progRETVAL =$?;; *) echo $ "Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL
Save Exit
chmod 775/etc/rc.d/init.d/nginx #赋予文件执行权限chkconfig --level 012345 nginx on #设置开机启动service nginx start
4. Installing Redis
Tar zxvf redis-2.8.6.tar.gz
MV redis-2.8.6 Redis
CD Redis
Make MALLOC=LIBC
Make install
Set up a Redis configuration file
Vi/usr/local/redis/redis.conf
#是否作为守护进程运行daemonize yes# is run as a subsequent process, you need to specify a PID, which defaults to/var/run/redis.pidpidfile redis.pid# bound host IP, and the default value is 127.0.0.1#bind 127.0.0.1#redis Default listening port 6379# client idle for how many seconds, disconnect, default is 300 (seconds) Timeout 300# logging level, there are 4 optional values, Debug,verbose (default), notice, Warningloglevel verbose# Specifies the file name of the log output, the default value is stdout, can also be set to/dev/null masking log logfile stdout# the number of available databases, the default is 16, the default database is 0databases 16# Policy to save data to disk # When a keys data is changed, 900 seconds to disk save 900 # When 10 keys data is changed, 300 seconds to disk save 300 10# when there are 1w keys data is changed, 60 seconds Refresh to disk once save 60 10000# when dump. RDB database is compressed data object rdbcompression yes# Local database file name, default value is Dump.rdbdbfilename dump.rdb# The local database holds the path, the default value is./dir.
Adjust the memory allocation policy
Take a look at the memory allocation policy Vi/proc/sys/vm/overcommit_memory note with VI or VIM can not modify the value inside, you need to use the following method echo "1" >>/proc/sys/vm/overcommit_ Memory
This file specifies the kernel's policy for memory allocation, which can be 0, 1, 2.
0, indicates that the kernel will check for sufficient available memory to be used by the process, and if sufficient memory is available, the memory request is allowed; otherwise, the memory request fails and the error is returned to the application process.
1, which means that the kernel allows all physical memory to be allocated regardless of the current memory state.
2, which indicates that the kernel allows allocating memory that exceeds the sum of all physical memory and swap space.
Redis in the dump data, will fork out a sub-process, in theory the child process occupies the same memory and the parent is the same, such as the parent occupies 8G of memory, this time also to allocate 8G of memory to child, if the memory is not affordable, Will often cause the Redis server down or IO load is too high, inefficient. So the more optimized memory allocation policy here should be set to 1 (indicating that the kernel allows all physical memory to be allocated regardless of the current memory state)
Start the service
Redis-server/usr/local/redis/redis.conf
Use the REDIS-CLI command to actually do it.
Close Service
REDIS-CLI shutdown If port change can specify port redis-cli-p 6380 shutdown
Save/backup
Data backups can be implemented by backing up the file on a regular basis.
Because Redis is written to disk asynchronously, if you want to have the in-memory data written to the hard disk immediately, you can execute the following command:
Redis-cli Save or Redis-cli-p 6380 Save (Specify port)
Note that the above deployment requires certain permissions, such as copying and setting kernel parameters.
Memory data is also written to the hard disk when the Redis-benchmark command is executed.
Set up Redis boot
Vi/etc/rc.d/init.d/redis
#!/bin/bash## redis-this script starts and stops the Redis-server daemon## chkconfig:-12# Description:redis is A persistent key-value database# processname:redis-server# config:/etc/redis/redis.conf# pidfile:/var/run/redi S.pidsource/etc/init.d/functionsbin= "/usr/local/bin" config= "/usr/local/redis/redis.conf" PIDFILE= "/var/run/ Redis.pid "# # Read configuration[-R" $SYSCONFIG "] && source" $SYSCONFIG "retval=0prog=" 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 ""in Start" start; stop) stop;; restart) restart;; Condrestart) [-e/var/lock/subsys/$prog] && restart retval=$? ;; Status) status $prog retval=$? ;; *) echo $ "Usage: $ start|stop|restart|condrestart|status}" Retval=1esacexit $RETVAL
Save exit
chmod 777/etc/rc.d/init.d/redischkconfig redis onservice redis start #stop, restart
5. Invoke Lua script in Nginx to simply read data from Redis
Vi/usr/local/openresty/nginx/conf/nginx.conf
In the Server configuration section, add
location/test { default_type text/plain; Content_by_lua_file/usr/local/test.lua; }
Save exit, and then create a LUA script file
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") Returnendngx.say (RES)
Save exit, restart Nginx
Service Nginx Restart
6. Testing
Modify the index.html file in the Nginx HTML directory to test, add jquery references and AJAX requests
<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>
Access Nginx address http://192.168.0.106/, if the key value is foo No data will pop up
Adding data to Redis with the REDIS-CLI command
REDIS-CLI set Foo test
The re-access address will pop up
Category: Linux
CentOS6.4 install Openresty and Redis and easily read Redis data using LUA in Nginx