PHP NFS的实现代码

网络 网络管理
下面我们主要讲解一下PHP NFS的实现。那么稳重我们对session文件进行了修改和设置,具体代码已经总结好,希望对大家有所帮助。

在以往的学习中,我们知道NFS可以在很多系统以及软件,硬件平台中实现。那么今天我们就来讲解一下PHP NFS实现的具体操作和代码内容。其中,有一种方法就是利用NFS来共享session,如果session量比较大并且所有的session文件都在同一个子目录下的话,那么可能会由此带来很严重的负载问题,甚至导致网站无法使用。本文就是对这个方案做一下详细的解说。

首先,修改 php.ini的 session.save_path 选项,大致如下:

  1. session.save_path = "2;/tmp/php_sess" 

在PHP NFS设置中,意为把session存放在 "/tmp/php_sess" 目录下,并且分成 2 级子目录,每级子目录又分别有 16 个子目录。接下来,假设php的主目录为 /usr/local/server/php/,则新建一个文件 /usr/local/server/php/include/php/ext/session/mod_files.sh,其内容如下:

  1. #! /bin/sh  
  2. # NAME  
  3. #      mod_files.sh  - Update of the php-source/ext/session/mod_files.sh  
  4. #  
  5. # SYNOPSIS  
  6. #      mod_files.sh basedir depth [numberofsubdirs]  
  7. #  
  8. # DESCRIPTION  
  9. #      this script creates the directories tree used by php to store the session files  
  10. #      (see php.ini - 'session.save_path' option)  
  11. #  
  12. #      Example: if you want php to store the session files in a directory tree  
  13. #      of 3 levels of depth containing 32 directories in each directory,  
  14. #      first, put the setting bellow in the php.ini file:  
  15. #  
  16. #      session.save_path = "3;/tmp/session" 
  17. #  
  18. #      Now create the basedir directory: 'mkdir /tmp/session'  
  19. #  
  20. #      Then, call this scrip with the following arguments:  
  21. #  
  22. #      ./mod_files.sh ./mod_files.sh /tmp/session 3 32  
  23.  
  24. if test "$2" = ""; then  
  25.        echo "usage: $0 basedir depth [numberofsubdirs]"  
  26.        echo "numberofsubdirs: if unset, defaults to 16. if 32, 32 subdirs, if 64, 64 subdirs."  
  27.        exit 1  
  28. fi  
  29.  
  30. if test "$2" = "0"; then  
  31.        exit 0  
  32. fi  
  33.  
  34. hash_chars="0 1 2 3 4 5 6 7 8 9 a b c d e f" 
  35. if [ ! -z $3 ] ; then  
  36. if test "$3" -a "$3" -eq "32"; then  
  37.   hash_chars="$hash_chars g h i j k l m n o p q r s t u v" 
  38.   if test "$3" -eq "64"; then  
  39.    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 - ," 
  40.   fi  
  41. fi  
  42. fi  
  43.  
  44. for i in $hash_chars; do  
  45.        newpath="$1/$i" 
  46.        mkdir $newpath || exit 1  
  47.        sh $0 $newpath `expr $2 - 1` $3  
  48. done 

PHP NFS的目录修改后,设置为可执行之后,运行以下命令来创建哈希目录:

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

现在,就开始设置 NFS 共享了。假定有3台主机,ip分别为192.168.0.1(主机名svr1)、192.168.0.2(主机名svr2)、192.168.0.3(主机名svr3),现在让192.168.0.1来提供NFS共享服务,配置 /etc/exports,加入如下内容:

  1. /tmp/php_sess/ svr*(rw,no_root_squash) 

然后重启 nfs 服务,即可对另外两台主机提供NFS共享了。在 svr2、svr3 上执行以下命令来挂在NFS:

  1. shell>#mkdir /tmp/php_sess  
  2. shell>#mount svr1:/tmp/php_sess /tmp/php_sess 

用NFS来存储session的缺点是,session过期后不能自动清除,必须自己设定回收机制,我们可以利用crontab来定期回收,用用以下shell命令即可:

  1. find /tmp/php_sess -mmin +30 | xargs rm -fr 

意思是,删除30分钟以前PHP NFS的session文件,具体的时间请大家自己重新设置吧。***,在这两个主机上对 php.ini 增加/修改上面提到的内容,然后重启apache即可。

责任编辑:佟健 来源: IT实验室
相关推荐

2016-09-06 21:09:35

Phpgd库图片水印

2010-08-04 14:45:18

NFS挂载脚本

2010-07-17 00:53:50

CMD Telnet

2015-04-17 10:31:11

PHP下载美女图片实现代码

2011-08-29 11:25:29

清空service bSQL Server

2010-03-03 09:30:40

Python实现网页爬

2010-07-28 15:13:42

VMwareNFS

2009-12-02 19:08:19

PHP跳转代码

2009-11-16 16:17:45

PHP数组排序

2010-06-04 14:24:12

Linux 查看网络流

2023-11-09 09:28:09

Java代码

2010-07-30 13:51:23

NFS配置

2010-08-03 17:23:41

NFS服务

2021-04-15 21:21:59

代码热Python函数

2010-02-06 09:46:46

C++单向链表

2010-09-13 14:17:42

CSS纵向导航菜单

2009-12-18 16:12:11

Ruby加密

2020-04-02 15:39:51

代码编译器前端

2023-12-04 07:31:41

Golangwebsocket

2010-06-24 17:57:45

chkconfig h
点赞
收藏

51CTO技术栈公众号