Using Redis to store PHP session
By default, PHP will store the session information on the file system, no problem in a single machine situation, but when the system load increases, or in the case of high system availability requirements, the machine can not only keep up with the processing power, and when the machine fails, the entire system is in a state of non-availability. Therefore, multi-machine load balancing is very necessary.
But the problem is, assuming the system now has two application server Servera,serverb, user x is now logged in, and the login status is well known to be stored in the application server session, if the user X login request is ServerA response, Then its login state is stored on ServerA, and when the next request of User X is forwarded to ServerB, ServerB will assume that user X is not logged in because he does not store user X's session information.
Of course, you can set the load balancer to the same IP access requests are forwarded to the same server, but this is not perfect, you can imagine a situation where the application server ServerA process is dead, then all users logged on ServerA will not be able to access the service properly.
So only the ability to share storage sessions between multiple application servers can solve this problem, and existing common solutions use Memcache,redis to store the session.
Configure PHP+REDIS1 under Windows. Installing the Redis server
Redis.msi
If you download a non-installed version, you need to start the Redis service from the command line, and the command-line window cannot be closed, or the service will shut down
Redis-server.exe redis.conf
If you choose an MSI installation, you will see the Redis service in the Windows service after installation, which is automatically started by default, the command line goes to the installation directory, and running Redis-cli.exe can operate Redis read and write through the command line.
Keys * //Lists all the keys. Get "phpredis_session:xxxxxxxxxxxxx"//Gets the value of the specified key FLUSHDB//Clears all key-value pairs.
The relevant parameters of the Redis-server can be configured in the redis.conf file.
2. Install the Php_redis extension 2.1. View the PHP compilation script by Phpinfo ().
2.2. Download the Php_redis extension that is consistent with the compilation script
Must be consistent, otherwise you cannot use
Php_redis extension
TS (thread safe), NTS (thread not safe).
2.3. Place the downloaded Php_redis.dll in the PHP extension directory (EXT). 3. Configure php.ini Add extension
Extension=php_redis.dll
4. Restart the Apache service and View Phpinfo () the Redis block indicates success.
5 Setting the session storage mode of PHP.ini
Session.save_handler = Redis session.save_path= "tcp://127.0.0.1:6379";
If more than one Redis server can be set to:
Session.save_path= "Tcp://10.10.0.1:6379?weight=4,tcp://10.10.0.2:6379?weight=6"
6 Testing
Test PHP Code
$redis = new Redis (); $redis->connect ("127.0.0.1", "6379"); The PHP client sets the IP and port //Store a value $redis->set ("Say", "Hello World"); echo $redis->get ("say"); Should output Hello World //Store multiple values $array = Array (' first_key ' = ' first_val ', ' second_key ' = ' second_val ', ' third_key ' = ' third_val '); $array _get = Array (' First_key ', ' second_key ', ' Third_key '); $redis->mset ($array); Var_dump ($redis->mget ($array _get));
Command line View results
Redis-cli.exe keys *
CentOS under Configuration Php+redis
1. Installing the Redis server
Download the Redis-server code and install
Mkdir/tmp/rediscd/tmp/rediswget Http://download.redis.io/releases/redis-2.8.8.tar.gztar xzf REDIS-*CD redis-* Makesudo make install clean # #copy Configure FILEMKDIR/ETC/REDISCP redis.conf/etc/redis/redis.conf
Configuration file Example
#start as a daemon in backgrounddaemonize yes#set port, by default was 6379port 6379#set IP on which daemon would be listeni ng for Connectionsbind 127.0.0.1#where to dump databasedir/var/opt
Check the service installation location:
Whereis Redis-server#redis-server:/usr/local/bin/redis-serve
Setup Redis-server follow system boot
vi/etc/rc.local# Insert the following command before exit 0/usr/local/bin/redis-server/etc/redis/redis.conf# turn on the service redis-server/etc/redis/ redis.conf# Connection Service REDIS-CLI
2. Installing the Php_redis Extension
Download Php-redis extensions and install
Cd/tmpwget Https://github.com/nicolasff/phpredis/zipball/master-O phpredis.zipunzip phpredis.zipcd phpredis-* Phpize./configuremake && make Install
3. Configure PHP.ini
Inserting a Redis configuration
Session.save_handler = Redissession.save_path = "tcp://10.44.64.8:6379" extension=redis.so
Setup is complete.
Using Redis to store PHP session