1、安装libevent(下载地址:http://libevent.org/)
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
tar zxvf libevent-2.0.19-stable.tar.gz
cd libevent-2.0.19-stable
./configure
make && make install
2、安装memcache(下载地址:http://memcached.org/)
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar zxvf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure --prefix=/usr/local/memcached
make && make install
3、创建启动文件
vim /etc/init.d/memcached
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: 3 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 128 -l 192.168.2.111 -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
添加执行权限:
chmod 700 /etc/init.d/memcached
启动:
[root@localhost ~]# service memcached start
Starting memcached: [ OK ]
注:第一次启动时报错:
error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解决:ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/(因为我使用的64系统)
查看端口是否保在侦听状态:
[root@localhost ~]# netstat -ntlp | grep mem
tcp 0 0 192.168.2.111:11211 0.0.0.0:* LISTEN 22428/memcached
添加开机启动:
chkconfig --add memcached
查看是否设置成功:
chkconfig --list | grep memcached
4、php安装memcahce modules(下载地址:http://pecl.php.net/package/memcache)
wget http://pecl.php.net/get/memcache-3.0.6.tgz
tar zxvf memcache-3.0.6.tgz
cd memcache-3.0.6
phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
make && make install
5、编辑php.ini
extension=memcache.so
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
memcache.allow_failover = 1
memcache.max_failover_attempts = 20
memcache.chunk_size = 8192
memcache.default_port = 11211
session.save_handler = memcache
session.save_path = "tcp://192.168.2.111:11211"
注:
extension=memcache.so --添加
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"--修改
memcache.allow_failover = 1 --添加
memcache.max_failover_attempts = 20 --添加
memcache.chunk_size = 8192 --添加
memcache.default_port = 11211 --添加
session.save_handler = memcache --修改
session.save_path = "tcp://192.168.2.111:11211" --添加,本测试memcache和apache都在一个服务器上,这个根据实际需要设置地址。
配置完成
重启apache
6、新建一个php测试脚本,供客户端浏览器访问测试
vi /usr/local/apache/htdocs/test.php
<?php
session_start();
if($_GET['act']=='write')
$_SESSION['name']='0009847';
elseif($_GET['act']=='read')
var_dump($_SESSION);
else
echo 'invalid argument';
?>
访问http://192.168.2.111/test.php?act=write再访问http://192.168.2.111/test.php?act=read
结果:array(1) { ["name"]=> string(7) "0009847" }
查看phpinfo:
(完成)