PHP cluster session sharing

Source: Internet
Author: User
PHP cluster session sharing copyright statement: This article is reprinted from Ziyi

Address: http://www.onmpw.com/tm/xwzj/network_144.html

If you want to reprint the information, please contact Yi Xiaoyi, a member of the cloud College team, at: ruanqy # tingyun.com

The concept of a cluster is not complex. In fact, multiple computers work together for the same purpose. In Web applications, multiple servers provide services for one site.

The first step to build a PHP cluster is to set up server load balancer. First, we need three hosts:

Nginx load: 192.166.5.111PHP Application 1: 192.168.5.20.php application 2: 192.168.5.113

Previously, on the host where the PHP application is located, we had to install Nginx, apache, and other web servers and then use Nginx as the load. The communication between the Nginx load and the php application is at the application layer, and the Nginx load is actually equivalent to a proxy. However, the current situation is different. The application of Fastcgi technology allows the php application layer to no longer install web servers. Currently, fpm is supported as an internal module in PHP5.5. In this case, the communication between the Nginx load and the php application is at the transport layer, and the two use socket for communication. Of course, this requires support from the fpm service.

Nginx settings

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

Http {...... Upstream onmpw_phpApps {server 192.168.18.88: 9000; server 192.168.18.191: 9000 ;}...... Server {listen 80; server_name load.onmpw.com # Here is the domain name root/www/onmpw ...... Location ~ \. Php $ {root/www/onmpw # Here is the Directory of the PHP application fastcgi_pass onmpw_phpApps ;...... }}}

The preceding settings are for Nginx. It only contains the key part, and the rest are set the same as when we use Nginx + PHP as the web service.

PHP host settings

The settings here are relatively simple.

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 // you can set the port here. Save and exit #/usr/local/php/sbin/php-fpm // enable the service

Host 192.168.5.113

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

Now the PHP host settings are complete. Of course, the code needs to upload one copy on each of the two hosts.

Well, after the above settings, a basic PHP cluster has been built. However, there is a problem. in this case, it is okay if you only access static resources or do not perform interactions. If interaction is required, session sharing is involved. By default, PHP stores sessions on a local disk. Then, how can we share sessions between the two hosts? next we will solve this problem.

Session sharing between PHP hosts

I have seen a solution on the internet before. Since PHP stores sessions in files, we can build a Distributed File System (NFS) on the Nginx load host ), store the sessions of the two PHP hosts in this file system. To share the session.

I personally prefer to store sessions in the database. So here I will introduce how to store sessions in redis. So we need to add a Redis server.

Redis server: 192.168.5.114

By default, PHP does not support Redis operations. Therefore, we need to manually install third-party extensions to support Redis operations. For details about how to enable PHP to support Redis, refer to "two methods for PHP to operate Redis".

Here I think our PHP already supports Redis. The next step is to store the session in Redis. There are two ways: one is to directly modify the PHP configuration file php. ini; the other is to rewrite the session mechanism.

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

Use vim to open php. ini. you need to modify the following two items: session. save_handler and session. save_path.

Session. save_handler = Redissession. save_path = "tcp: // 192.168.5.114: 6379" // Redis does not require password verification session. save_path = "tcp: // 192.168.5.114: 6379? Auth = password "// Redis requires password verification

Modification complete. save and exit. Restart the php-fpm service.

# kill -INT `cat /usr/local/php/var/run/php-fpm.pid`# /usr/local/php/sbin/php-fpm

Follow these steps for both PHP hosts. After the above steps, all session information is saved to Redis. This achieves session sharing.

Store the session to Redis by rewriting the session mechanism.

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

For session rewriting, php has provided the SessionHandlerInterface interface for us. We only need to implement this interface. For details about how to rewrite the session mechanism, refer to the article "rewrite the session mechanism in PHP. I have also rewritten the session mechanism myself. the complete code of this class is on github. if you are interested, click here to view it.

Summary

There are many PHP cluster architecture methods, but their principles are similar. The key is to find the best solution that best suits your project. For example, you can also choose to use memcache or MySQL database for session storage. In short, the most suitable for you is the best. I hope this article will help you.

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