Redis installation, configuration, use, and redis php extension installation tutorial

Source: Internet
Author: User
Tags install redis

Redis is a memory database that supports more value types than memcache. Sina Weibo uses redis for caching.

Install redis source code
Copy codeThe Code is as follows:
Wget http://download.redis.io/redis-stable.tar.gz
Tar-zxvf redis-stable.tar.gz
Cd redis-stable
Make
Make test
Make install
1. the following error may be reported during make:
Copy codeThe Code is as follows:
Zmalloc. o: In function 'zmalloc _ used_memory ':
/Root/apsaradb for redis-stable/src/zmalloc. c: 223: undefined reference to '_ sync_add_and_fetch_4'
Collect2: ld returned 1 exit status
Make [1]: *** [redis-server] Error 1
Make [1]: Leaving directory '/root/redis-stable/src'
Make: *** [all] Error 2
Solution:
Edit the OPT in src/. make-settings and change it to OPT =-O2-march = i686.

2. make test:
Copy codeThe Code is as follows:
You need tcl 8.5 or newer in order to run the Redis test
Make: *** [test] Error 1
Solution: Install tcl
Copy codeThe Code is as follows:
Wget http://downloads.sourceforge.net/tcl/tcl8.6.0-src.tar.gz

Cd tcl8.6.0/

Cd unix &&
./Configure -- prefix =/usr \
-- Mandir =/usr/share/man \
-- Without-tzdata \
$ ([$ (Uname-m) = x86_64] & echo -- enable-64bit )&&
Make &&

Sed-e "s @ ^ \ (TCL_SRC_DIR = '\). * @ \ 1/usr/include '@"\
-E "/TCL_ B/s @ = '\ (-L \)\?. * Unix @ = '\ 1/usr/lib @"\
-I tclConfig. sh

Make install &&
Make install-private-headers &&
Ln-v-sf tclsh8.6/usr/bin/tclsh &&
Chmod-v 755/usr/lib/libtcl8.6.so

Redis command Introduction

Redis consists of four executable files: redis-benchmark, redis-cli, redis-server, and redis-stat. conf constitutes the final available package of the entire redis. Their functions are as follows:

Redis-server: daemon Startup Program of the Redis server
Redis-cli: Redis command line operation tool. Of course, you can also use telnet to operate based on its plain text protocol.
Redis-benchmark: Redis performance testing tool to test the read/write performance of Redis in your system and your configuration
Redis-stat: Redis status detection tool that can detect Redis's current status parameters and latency
Now you can start redis. redis has only one startup parameter, that is, its configuration file path.

Start redis

Copy redis. conf in the source package to/etc.
Copy codeThe Code is as follows: # cd redis-stable
# Cp redis. conf/etc/redis. conf

Edit/etc/redis. conf, change daemaon no to daemaon yes, and start the process as a daemon process.
Copy codeThe Code is as follows:
# Redis-server/etc/redis. conf

Disable redis

Copy codeThe Code is as follows:
# Redis-cli shutdown // close all
Disable redis on a port
# Redis-cli-p 6397 shutdown // shut down redis on port 6397
Note: cache data will be automatically dumped to the hard disk after it is disabled. For the hard disk address, see dbfilename dump. rdb in redis. conf.

Redis Configuration

Note: by default, the daemonize parameter of the previously copied redis. conf file is no, so redis will not run in the background. to test it, we need to re-open a terminal. If it is changed to yes, redis is run in the background. In addition, the pid file, log file, and data file address are specified in the configuration file. If you need to modify the address first, the default log information is directed to stdout.

The main configuration parameters of redis. conf are as follows:
Copy codeThe Code is as follows:
Daemonize: whether to run in daemon mode
Pidfile: pid File Location
Port: the port number of the listener.
Timeout: Request timeout
Loglevel: log information level
Logfile: Location of the log file
Databases: number of databases Enabled
Save **: the frequency at which snapshots are saved. The first "*" indicates the duration and the third "indicates the number of write operations performed. Snapshots are automatically saved when a certain number of write operations are performed within a certain period of time. You can set multiple conditions.
Rdbcompression: whether to use Compression
Dbfilename: Data snapshot file name (only file name, excluding directory)
Dir: directory for storing data snapshots (this is the Directory)
Appendonly: whether to enable appendonlylog. If it is enabled, a log is recorded for each write operation, which improves data risk resistance but affects efficiency.
Appendfsync: How to synchronize appendonlylog to the disk (three options are force-call fsync for each write, enable fsync once per second, and do not call fsync to wait for the system to synchronize itself)
Now you can open a terminal for testing. The default listening port in the configuration file is 6379.

Automatic startup of redis upon startup

Before using this script for management, you must configure the following kernel parameters. Otherwise, an error will be reported when the Redis script restarts or stops redis, data cannot be automatically synchronized to the disk before the service is stopped:
Copy codeThe Code is as follows:
# Vi/etc/sysctl. conf

Vm. overcommit_memory = 1

Then the application takes effect:
Copy codeThe Code is as follows:
# Sysctl-p

Create a redis STARTUP script:
Copy codeThe Code is as follows:
# Vim/etc/init. d/redis

#! /Bin/bash
#
# Init file for redis
#
# Chkconfig:-80 12
# Description: redis daemon
#
# Processname: redis
# Config:/etc/redis. conf
# Pidfile:/var/run/redis. pid
Source/etc/init. d/functions
# BIN = "/usr/local/bin"
BIN = "/usr/local/bin"
CONFIG = "/etc/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
Then add the service and start it automatically:
Copy codeThe Code is as follows:
# Chmod 755/etc/init. d/redis
# Chkconfig -- add redis
# Chkconfig -- level 345 redis on
# Chkconfig -- list redis

Redis php extension Installation
Copy codeThe Code is as follows:
Wget-O https://github.com/nicolasff/phpredis/zipball/master
Unzip php-redis.zip
Cd nicolasff-phpredis-2d0f29b/
/Usr/local/php/bin/phpize
./Configure -- with-php-config =/usr/local/php/bin/php-config
Make & make install

After the installation is complete, redis. so is installed
Copy codeThe Code is as follows:/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

Vi/usr/local/php/lib/php. ini

Add
Copy codeThe Code is as follows: extension = redis. so

Restart php-fpm.

You may encounter configure when adding the -- with-php-config parameter.
Copy codeThe Code is as follows:
Configure: error: Cannot find php-config. Please use -- with-php-config = PATH

./Configure -- with-php-config =/usr/local/php/bin/php-config

Related Article

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.