PHP: how to share multiple server sessions with NFS _ PHP Tutorial

Source: Internet
Author: User
PHP implements multi-server session sharing-NFS sharing. Introduction to NFS sharing for PHP multi-server session sharing, Nio Grand Xia raised the question of session multi-server sharing. for the original article, see PHP multi-server sharing SESSION data. Among them, there is a PHP method for multi-server session sharing NFS sharing.
The Nio hero raised the issue of multi-server session sharing. for the original article, please refer to PHP to implement multi-server sharing SESSION data.

One method is to use NFS to share sessions. if the session volume is large and all session files are in the same subdirectory, this may cause serious load problems and even the website cannot be used. This article provides a detailed explanation of this solution.
First, modify the session. save_path option of php. ini, which is roughly as follows:

Session. save_path = "2;/tmp/php_sess"

The session is stored in the "/tmp/php_sess" directory and divided into two sub-directories. each sub-directory has 16 sub-directories.
Next, assume that the main directory of php is/usr/local/server/php/, create a file/usr/local/server/php/include/php/ext/session/mod_files.sh, the content is as follows:

#! /Bin/sh
# NAME
# Mod_files.sh-Update of the php-source/ext/session/mod_files.sh
#
# SYNOPSIS
# Mod_files.sh basedir depth [numberofsubdirs]
#
# DESCRIPTION
# This script creates the directories tree used by php to store the session files
# (See php. ini-'session. save_path 'option)
#
# Example: if you want php to store the session files in a directory tree
# Of 3 levels of depth containing 32 directories in each directory,
# First, put the setting bellow in the php. ini file:
#
# Session. save_path = "3;/tmp/session"
#
# Now create the basedir directory: 'mkdir/tmp/session'
#
# Then, call this scrip with the following arguments:
#
#./Mod_files.sh./mod_files.sh/tmp/session 3 32

If test "$2" = ""; then
Echo "usage: $0 basedir depth [numberofsubdirs]"
Echo "numberofsubdirs: if unset, defaults to 16. if 32, 32 subdirs, if 64, 64 subdirs ."
Exit 1
Fi

If test "$2" = "0"; then
Exit 0
Fi

Hash_chars = "0 1 2 3 4 5 6 7 8 9 a B c d e f"
If [! -Z $3]; then
If test "$3"-a "$3"-eq "32"; then
Hash_chars = "$ hash_chars g h I j k l m n o p q r s t u v"
If test "$3"-eq "64"; then
Hash_chars = "$ hash_chars w x y z a B c d e f g h I J K L M N O P Q R S T U V W X Y Z -,"
Fi
Fi
Fi

For I in $ hash_chars; do
Newpath = "$1/$ I"
Mkdir $ newpath | exit 1
Sh $0 $ newpath 'expr $2-1' $3
Done

After being set to executable, run the following command to create a hash Directory:

Shell> # cd/usr/local/server/php/include/php/ext/session/
Shell> #./mod_files.sh/tmp/php_sess 2 16

Now, we can set up NFS sharing. Assume that there are three hosts with ip addresses 192.168.0.1 (host name svr1), 192.168.0.2 (host name svr2), and 192.168.0.3 (host name svr3). now let 192.168.0.1 provide NFS sharing service, configure/etc/exports and add the following content:

/Tmp/php_sess/svr * (rw, no_root_squash)

Then restart the nfs service to provide NFS sharing for the other two hosts.
Run the following commands on svr2 and svr3 to Mount NFS:

Shell> # mkdir/tmp/php_sess
Shell> # mount svr1:/tmp/php_sess

Finally, add/modify the above content to php. ini on the two hosts, and then restart apache.

Summary Preface: Nio has raised the issue of sharing sessions with multiple servers. for the original article, see PHP to share session data with multiple servers. There is...

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.