JIYIK CN >

Current Location:Home > Learning > NETWORK >

PHP cluster session sharing

Author:JIYIK Last Updated:2025/03/18 Views:

The concept of cluster is not complicated. It is actually multiple computers working together for the same goal. In Web applications, multiple servers provide services for a site.

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

Nginx load: 192.166.5.111
PHP application 1: 192.168.5.112
PHP application 2: 192.168.5.113

Previously, we needed to install a web server such as Nginx or Apache on the host where the PHP application is located, and then use Nginx as the load in front . 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 situation is different now. The application of Fastcgi technology allows the PHP application layer to no longer need to install a web server. Now PHP5.5 version has supported fpm as an internal module. In this case, the communication between the Nginx load and the PHP application is at the transport layer, and the two use sockets for communication. Of course, this requires the support of the fpm service .

Nginx Setup

First, set up 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 ##This is the domain name
         root /www/onmpw         
         ……
         location ~ \.php$ {
                   root /www/onmpw ##This is the directory where the PHP application is located
                   fastcgi_pass onmpw_phpApps;
                   ……
         }
      }
}

The above is the configuration of Nginx. It only contains the key parts, and the rest is the same as the configuration we usually use when using Nginx+PHP as a web service.

PHP host settings

The settings here are relatively 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 yourself. Save and exit
# /usr/local/php/sbin/php-fpm //Start the service

Host 192.168.5.113

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

At this point, the PHP host settings are complete. Of course, the code needs to be uploaded to both hosts.

Well, after the above settings, a basic PHP cluster has been built. But there is a problem. If you only access static resources or do not interact, there is no problem. If you need to interact, it involves a session sharing issue. By default, PHP stores the session on the local disk. So how do you share the session between the two hosts? Let's 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 and store the sessions of the two PHP hosts in this file system. This will achieve the purpose of sharing sessions.

I personally prefer to store sessions in a 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

PHP does not support Redis operations by default. So here we need to manually install a third-party extension to enable it to support Redis operations. For how to enable PHP to support Redis, we can refer to "Two Ways to Operate Redis with PHP" .

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 to store the session in Redis

Use vim to open php.ini. There are two items that need to be modified: session.save_handler and session.save_path.

session.save_handler = Redis
session.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

After the modification is completed, save and 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 the above steps. After the above steps, all session information is saved in Redis, thus achieving session sharing.

Store session in Redis by rewriting the session mechanism

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

For rewriting session, PHP has provided us with SessionHandlerInterface interface. We just need to implement this interface. For how to rewrite session mechanism, you can refer to the article "PHP rewrite session mechanism" . And I also rewrote the session mechanism myself. The complete code of this class is on github. If you are interested, you can check https://github.com/onmpw/PHPLibrary/tree/master/Code/Session.

Summarize

There are many ways to structure a PHP cluster, but the principles are similar. The key is to find the best solution that suits your project. For example, for the choice of session storage method, you can also choose to use memcache or mysql database. In short, the one that suits you best is the best. I hope this article will be helpful to you.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial