PHP Cluster session sharing

Source: Internet
Author: User
Tags redis server
Copyright Notice: This article reproduced self-trace memory

Original address: http://www.onmpw.com/tm/xwzj/network_144.html

For reprint please contact the Cloud College team member Nanjiao, email: ruanqy#tingyun.com

The concept of clustering is not much more complex, in fact, more than one computer to work together for the same goal. In a Web application, multiple servers provide a service for a site.

The first step in building a PHP cluster is to set up load balancing. First we need three hosts:

Nginx Load: 192.166.5.111PHP application 1:192.168.5.112php Application 2:192.168.5.113

Previously, on the host where the PHP application resides, we need to install such Web servers as Nginx or Apache, and then use Nginx as the payload. The communication between the nginx load and the PHP application is in the application layer, and the nginx load is actually the equivalent of an agent. However, the situation is different now. The application of FASTCGI technology allows the Web server to be installed in the PHP application layer. The PHP5.5 version now supports FPM as an internal module. In this case, the communication between the Nginx payload and the PHP application is in the transport layer, and the socket is used for communication between the two. Of course, this requires the support of the FPM service.

Nginx settings

First set the Nginx (192.168.5.111), edit the nginx.conf configuration file

http{...         Upstream onmpw_phpapps{            server 192.168.18.88:9000;            Server 192.168.18.191:9000;        }       ... server{         listen        ;         server_name   load.onmpw.com   # #这里是域名         root           /www/onmpw         ... Location ~ \.php$ {                   root         /www/onmpw   # #这里是PHP应用所在目录                   fastcgi_pass   Onmpw_phpapps;                   ...}}      

These are the settings for Nginx. It only contains the key parts, and the rest of the peace is often the same as when we use nginx+php as a Web service.

PHP Host Settings

The settings here are quite simple.

First edit the php-fpm.conf file, modify the listening IP and port, and then start the FPM service

Host 192.168.5.112

Listen = 192.168.5.112:9000   //The port here can be set by itself. Save Exit #/USR/LOCAL/PHP/SBIN/PHP-FPM   //Open service

Host 192.168.5.113

Listen = 192.168.5.113:9000#/usr/local/php/sbin/php-fpm

This is where the host settings for PHP are completed. Of course, the code needs to be uploaded on both hosts.

Well, after the above setup, a basic PHP cluster has been built. However, there is a problem, which is no problem if you are simply accessing static resources or not interacting. If you need to interact, it involves a session sharing issue. By default, PHP has a session on the local disk. So how do we share the session between the two hosts, and then we'll solve the problem.

Session sharing between PHP hosts

Previously saw a solution on the Internet. Since PHP is storing the session in a file, we can build a distributed File System (NFS) on the Nginx load host, so that the sessions of the two PHP hosts are stored in this file system. To achieve the purpose of sharing the session.

I personally prefer to store the session in a database. So what I'm describing here is storing the session in Redis. So we need to add a Redis server

Redis Server: 192.168.5.114

PHP is not supported by default for REDIS operations. So here we need to manually install a third-party extension to support operations on Redis. For how to make PHP support Redis, we can refer to the two ways of working with Redis in PHP.

In this case, I think our PHP has supported Redis. The next step is to store the session in Redis in two ways: one is to modify the PHP configuration file php.ini directly, and the other is to rewrite the session mechanism.

Modify the PHP configuration file php.ini to store the session in Redis

Using Vim to open php.ini, there are two items that need to be modified: Session.save_handler and Session.save_path.

Session.save_handler = Redissession.save_path = "tcp://192.168.5.114:6379"//redis does not require password authentication session.save_path = "tcp:// 192.168.5.114:6379?auth=password "//redis requires password authentication

Modify complete, save exit. Then restart the PHP-FPM service

# kill-int ' cat/usr/local/php/var/run/php-fpm.pid ' #/USR/LOCAL/PHP/SBIN/PHP-FPM

Both PHP hosts follow these steps. After the above steps, all the information for the session is saved to Redis. Thus, the session sharing is realized.

To store the session in Redis by overriding the session mechanism

Generally, in many cases we do not have permission to modify the php.ini file. At this time we can modify the session information storage by rewriting the session mechanism.

For the rewrite session,php has provided us with the Sessionhandlerinterface interface. We just have to implement this interface. about how to rewrite the session mechanism, you can refer to the "PHP rewrite session mechanism" this article. And I have also rewritten the session mechanism, the complete code of the class on GitHub, everyone interested and can click here to view.

Summarize

There are many ways to structure a PHP cluster, but the principle is similar. The key is to find the best solution for your project. For example, you can choose to use Memcache or MySQL database for the selection of the session storage mode. In short, the most suitable for their own is the best. I hope this article is helpful to everyone.

To read more technical articles, visit the Cloud Technology blog and visit the official cloud website to learn more about the magic of application performance optimization.

  • 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.