An example of Redis's installation, use, and expansion of Redis in PHP and implementation of the Redis operation in PHP

Source: Internet
Author: User
Tags redis server

1, download the source package redis-2.8.21.tar.gz, and upload it to the specified directory /urs/src, and then unzip it:

[Email protected] src]# TAR-XVF redis-2.8.21.tar.gz

Enter the extracted directory and execute the following command to specify the installation directory as /urs/local/redis:

[Email protected] src]# CD redis-2.8.21

[[email protected] redis-2.8.21]# make Prefix=/usr/local/redis Install

After you have successfully installed Redis, you can See a bin directory in/usr/local/redis that includes the following files:

[Email protected] ~]# cd/usr/local/redis/bin/

[[email protected] bin]# ls

Redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server

2. Add the redis boot script to the service:

In the /etc/rc.d/init.d directory, create a file called redis(description:/etc/rc.d/init.d/ The script in the directory will be executed when the system starts, and then add the following to the file:

[Email protected] ~]# 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"

Config= "/etc/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 "$" 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=1

Esac

Exit $RETVAL

Copy the profile redis.conf from the extracted redis directory to /etc/redis/redis.conf ( Note the The path in the Redis startup script is the same )

[Email protected] ~]# Mkdir/etc/redis

[Email protected] ~]# cp/usr/src/redis-2.8.21/redis.conf/etc/redis/redis.conf

Then add redis to the enrollment service:

[Email protected] ~]# chkconfig--add Redis

[[email protected] ~]# chkconfig Redis on

[Email protected] ~]# chkconfig--list Redis

Redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off

To restart redis:

[Email protected] ~]# service Redis restart

Stop Redis Server: [OK]

Starting Redis Server: [OK]

To see if the startup was successful:

[Email protected] ~]# Ps-ef | grep Redis

Root 2984 1 0 13:40? 00:00:00/usr/local/bin/redis-server 0.0.0.0:6379

Root 2989 2444 0 13:40 pts/1 00:00:00 grep redis

Modify /etc/redis/redis.conf, set up the redis process as the daemon, and specify a password :

[Email protected] ~]# vim/etc/redis/6379.conf

Daemonize Yes//daemonize: Whether to run in the next station daemon mode

Requirepass 20082009// Set Password to 20082009

After Setup, you will need to restart Redis for the settings to take effect:

[Email protected] ~]# service Redis restart

Modify the environment variable file to add the following:

Vim/etc/profile

#set Redis Path

Export Redis_home=/usr/local/redis

Export Path=${redis_home}/bin:${path}

make it effective immediately by Source/etc/profile

3, call redis-cli command for simple operation (note whether to start password Authentication):

[Email protected] ~]# REDIS-CLI

127.0.0.1:6379> Ping

(Error) Noauth Authentication required.

127.0.0.1:6379> auth 20082009 // need to enter a password

Ok

127.0.0.1:6379> Ping

PONG

127.0.0.1:6379> Set name LeBron James

(Error) ERR syntax error

127.0.0.1:6379> set name "LeBron James" /// string with spaces need to be added ""

Ok

127.0.0.1:6379> Get Name

"LeBron James"

127.0.0.1:6379> Set name Lebronjames

Ok

127.0.0.1:6379> Get Name

"Lebronjames"

127.0.0.1:6379>

4. expand redisin PHP:

Download a php extension phpredis Source package phpredis-2.2.4.tar.gz, upload it to the server at the specified location, Unzip it and then go to the extracted directory:

TAR-XVF phpredis-2.2.4.tar.gz

CD phpredis-2.2.4

generating Configure configuration files with phpize

[Email protected] phpredis-2.2.4]#/usr/local/php/bin/phpize

Then execute the following command:

[Email protected] phpredis-2.2.4]#/configure--with-php-config=/usr/local/php/bin/php-config

[[email protected] phpredis-2.2.4]# make && make install

To configure PHP support Phpredis, add the following in the php.ini file:

Vim/usr/local/php/lib/php.ini

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so

Then restart the nginx,php-fpm and redisseparately:

Service Nginx Restart

Service PHP-FPM Restart

Service Redis Restart

View phpinfo (), shown below, indicating that php extended Redis was successful:


A simple example of the PHP operation redis :

<?php   $redis = new Redis ();   $redis->connect (' 127.0.0.1 ', ' 6379 ') or Die ("Could not-to-connect to Redis"); or the specific host IP address line   $redis->set ("Test", "PHP handle Redis");   Var_dump ($redis->get (' Test '));   echo "<br/>";   $redis->del (' test ');   Delete Assignment   var_dump ($redis->get (' Test '));  >


Output Result:


From the results can be seen, Redis redis.conf The password is configured in the file redis redis.conf REQUIREPASS&NBSP;20082009&NBSP;&NBSP; #requirepass  20082009 redis : service redis restart

Execute the php file again, showing the following results, indicating that php operation Redis succeeded:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An example of Redis's installation, use, and expansion of Redis in PHP and implementation of the Redis operation in PHP

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.