Php + redis implements Intranet session storage for multiple servers and read the example, redissession

Source: Internet
Author: User

Php + redis implements Intranet session storage for multiple servers and read the example, redissession

Large websites may experience unexpected system collapse due to high concurrency. This is really a headache. The first consideration is the Server Load balancer server, of course, the performance of the database is also very important. Today we will talk about how to handle the session problem in the case of Server Load balancer. To be honest, it is also possible not to process the session, however, in actual situations, there may be some problems that make the user experience very painful. For example, the session is lost when the server Load balancer allocates the server for switching back and forth during the shopping order, at this time, the user will be depressed and I will wipe this ghost. As a result, all kinds of worries will arise. Is there any security problems with this broken website. Next we will talk about the solution to this problem. Configure php and redis to store sessions. If I have two (at least) servers for load balancing:

1. On the master server to install php-redis and redis servers, the command line yum or apt-get is not required, the secondary server only needs to install php-redis (the two servers must be in the same Intranet or the Internet access is not secure)

2. Come to the php. ini file on your server opened by cool:

First, check if php extension = redis. so is enabled.

Session. save_handler = redissession. save_path = "tcp: // [Intranet ip address of the redis master server]: 6379 ″

The php-redis of the Primary and Secondary servers needs to be bound with an ip address (the ip address is the same as the redis-server of the primary server)

Redis. confbind [Intranet ip address of the redis master server]

To ensure security, set a password for the redis link:

Open vim/etc/redis. conf

Requirepass communication password (modify here)

OK, configure and restart redis:

sudo service redis-server restart

3. Test Redis connection and verification:

Redis-cli-h [Intranet ip address of the redis master server]

If you have set the redis connection password and try to access the data now, you should get an AUTH error and then execute

> Redis-cliredis Intranet ip: 6397> keys *

Okay. By the time of this step, we have completed 99%. The difference is the restart amount.

service apache2 restartservice php5-fpm restart 

4. test whether the session is written to redis:

<? Php session_start (); $ _ SESSION ['bieanju'] = array ('name' => 'other ances', 'num' => 'HTTP: // www.bieanju.com/'); // connect to redis $ redis = $ this-> connectRedis (); // check session_idecho 'session _ id :'. session_id (). '<br/>'; // session stored in redis (redis uses session_id as the key and stores it as a string) echo 'redis _ session :'. $ redis-> get ('phpredis _ SESSION :'. session_id ()). '<br/>'; echo 'php _ session :'. json_encode ($ _ SESSION ['class']); print_r ($ _ SESSION ['bieanju']);?>

It's right that the session can be read after it is written to redis. So far, the session of Server Load balancer is basically solved. The rest is how to apply the session in the project, which depends on your own business needs.

Finally, let's talk about php as the best language in the world ......

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.