Install PHP Redis for database caching under Centos 6.5

Redis was introduced in previous articles, and the following is a record of installing Redis on CentOS. For later improvement.

 

1. Support environment required for installation

 

The first thing to do before installing Redis is to install the Unix Tcl tool. If you don't install it, you will not be able to test Redis later. In the later execution of make test, the following error message is returned: You need tcl 8.xuyao de5 or newer in order to run the Redis test , the specific process is:

 

yum install -y tcl

 

2. Install redis

 

The process of installing redis is very simple, and there are also specific tutorials on the official website. details as follows:

cd /usr/local/src
wget http://download.redis.io/releases/redis-3.2.0.tar.gz
tar zxvf redis-2.8.19.tar.gz
cd redis-2.8.19
make
make PREFIX=/usr/local/redis install

 Where PREFIX=/usr/local/redis can be omitted, if omitted, redis will be installed in the /usr/local/bin directory by default.

 

3. Test Redis

cd src
make test

 Through the above command, you can perform an increased test on redis.

 

4. Configure redis

 

A. Copy and modify the configuration file

cp ./redis.conf /usr/local/redis/
vim /usr/local/redis/redis.conf

 

I only modified the following two items:

 

daemonize yes #redis will run as a daemon process, the default is no will temporarily use your terminal

timeout 300 #Close the connection after the client is idle for how long. If it is specified as 0, it means to close the function

More configuration content will be released after the subsequent sorting is completed.

 

B. Set up automatic startup

vim /etc/init.d/redis

 

The following content is saved in the file:

#!/bin/sh
#
# redis	  Startup script for Redis Server
#
# chkconfig: - 80 12
# description: Redis is an open source, advanced key-value store.
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/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
    be
    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

 

C. Start or stop the service

service redis start
service redis stop

 

5. Using redis

[root@localhost redis]# cd /usr/local/redis/bin
[root@localhost bin]# ./redis-cli
127.0.0.1:6379> set name wangkun
OK
127.0.0.1:6379> get name
"wangkun"
127.0.0.1:6379>

 

6. Install the redis extension for PHP

cd /usr/local/src/
#Go to https://github.com/phpredis/phpredis/releases to find the latest package
wget -O phpredis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.7.tar.gz
tar -xzvf phpredis.tar.gz
cd phpredis
phpize #If you report Can't find PHP headers in /usr/include/php, install yum install php-devel first
./configure #My php is installed by default. If your php is not installed by default, you need to specify the --with-php-config parameter to indicate the location of your php-config file. You can use find to find it Oh~ For example, mine is like this./configure --with-php-config=/usr/bin/php-config
make && make install

 

 

7. Configure the PHP configuration and open the redis extension

vim /etc/php.ini
Find the location of extension_dir and add the following below: extension=redis.so
service httpd restart #restart

 

8. Access Test

vim haha.php #Fill in the test content
<?php
 $redis = new Redis();
 $redis->connect('127.0.0.1',6379);
 $redis->set('name','wangkun');
 echo $redis->get('name');
?>

 

Access the file, if wangkun is printed, the test is successful!

 

related articles:

1. Install redis under windows

2. Redis novice tutorial

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691722&siteId=291194637