PHP cluster session sharing
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.
Related Articles
Detailed explanation of the implementation methods of SSO single sign-on in three
Publish Date:2025/03/18 Views:190 Category:NETWORK
-
Single Sign On (SSO) is not unfamiliar to us. For large systems, using SSO can reduce a lot of trouble for users. Take Baidu for example. Baidu has many subsystems - Baidu Experience, Baidu Knows, Baidu Library, etc. If we need to enter a u
First contact with CGI
Publish Date:2025/03/18 Views:51 Category:NETWORK
-
Since I am a PHP programmer, I often have to build a PHP operating environment. The popular nginx+php environment is very popular, and the mode it adopts is the FastCGI method, so I spent some time to learn about FastCGI. CGI (Common Gatewa
PHP+ajax to achieve cross-domain single sign-on
Publish Date:2025/03/16 Views:145 Category:NETWORK
-
We have previously introduced the principle of cross-domain single sign-on in "Detailed explanation of the implementation methods of three situations of SSO single sign-on" . Here we will introduce how to implement single sign-on using PHP
使用 phpMyAdmin 删除 MySQL 数据库中的所有行
Publish Date:2024/03/25 Views:72 Category:MySQL
-
在本指南中,我们将了解使用 phpMyAdmin 从 MySQL 数据库中删除所有行的最佳方法。
循环 PHP MySQLi 获取数组函数
Publish Date:2024/03/25 Views:125 Category:MySQL
-
本教程将指导你了解 php mysqli_fetch_array() 函数,并介绍如何迭代 mysqli 查询。
使用 PowerShell 远程处理执行命令
Publish Date:2024/03/01 Views:169 Category:编程语言
-
本文将讨论 PowerShell 远程处理的工作原理、几个 PowerShell 远程处理命令,以及我们如何将其投入实际使用。我们还将区分它与许多类似 cmdlet 的优势。
解决 Java 异常 Unable to Instantiate org.apache.hadoop.hive.ql.metadata.Sessi
Publish Date:2023/07/14 Views:255 Category:Java
-
本篇文章介绍如何解决 java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient。Apache Hive 是一款开源数据仓库软件,用于读取、管理和写入存储在 Hadoop 文件中的大
使用 PHP MySQLi 函数获取最后插入的 ID
Publish Date:2023/05/09 Views:102 Category:MySQL
-
本篇文章简要介绍了 PHP mysqli() 函数并演示了如何使用它从 MySQL 数据库中获取最后插入的 ID。它是一个名为 mysqli 的 MySQL 驱动程序扩展版本,
在 PHP 中使用 MongoDB 作为文件存储
Publish Date:2023/04/20 Views:145 Category:MongoDB
-
在为大文件创建可扩展存储方面,MongoDB 及其 GridFS(使用 MongoDB 查询语言 - MQL 编写)是市场上最好的文件存储解决方案之一。 在本教程中,您将学习如何在 PHP 中使用 MongoDB 作为文件存储。