在集群中共享session是一个问题。
方案: 1、把session放到共享的设备上去,如nfs。
2、放到 memcache 中,这种方法被很多人推崇。
memcache install:
download http://www.danga.com/memcached
install memcache support modules:
download http://www.monkey.org/~provos/libevent/
./configure
make
make install
install memcache
./configure --prefix=/usr/local/memcached --enable-threads
make
make install
创建一个启动文件 /etc/init.d/memcached
#!/bin/sh # # memcached: MemCached Daemon # # chkconfig: - 90 25 # description: MemCached Daemon # # Source function library. . /etc/rc.d/init.d/functions . /etc/sysconfig/network #[ ${NETWORKING} = "no" ] && exit 0 #[ -r /etc/sysconfig/dund ] || exit 0 #. /etc/sysconfig/dund #[ -z "$DUNDARGS" ] && exit 0
start() { echo -n $"Starting memcached: " daemon $MEMCACHED -u daemon -d -m 1024 -l 192.168.0.100 -p 11211 echo } stop() { echo -n $"Shutting down memcached: " killproc memcached echo } MEMCACHED="/usr/local/memcached/bin/memcached" [ -f $MEMCACHED ] || exit 1 # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop sleep 3 start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac exit 0
|
php memcahce modules install:
download http://pecl.php.net/package/memcache
phpize
./configure --enable-memcache
make
make install
编辑 php.ini 在 [Session] 添加指定你安装的memcache.so的位置。
extension=memcache.so
extension_dir = "/usr/local/php/lib/php/extensions/"
memcache.allow_failover = 1
memcache.max_failover_attempts = 20
memcache.chunk_size = 8192
memcache.default_port = 11211
session.save_handler = memcache
session.save_path = "udp://192.168.0.100:11211,tcp://192.168.0.101:11211"
一个简单的php测试脚本 test.php
//http://www./a.php?act=write //http://www./a.php?act=read session_start(); if($_GET['act']=='write') $_SESSION['name']='0009847'; elseif($_GET['act']=='read') var_dump($_SESSION); else echo 'invalid argument'; ?>
|