1、安装libevent(下载地址:http://libevent.org/)

 
  1. wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz

  2. tar zxvf libevent-2.0.19-stable.tar.gz

  3. cd libevent-2.0.19-stable

  4. ./configure

  5. make && make install

2、安装memcache(下载地址:http://memcached.org/)

 
  1. wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz

  2. tar zxvf memcached-1.4.13.tar.gz

  3. cd memcached-1.4.13

  4. ./configure --prefix=/usr/local/memcached

  5. make && make install

3、创建启动文件

vim /etc/init.d/memcached

 
  1. #!/bin/sh

  2. #

  3. # memcached: MemCached Daemon

  4. #

  5. # chkconfig: 3 90 25

  6. # description: MemCached Daemon

  7. #

  8. # Source function library.

  9. . /etc/rc.d/init.d/functions

  10. . /etc/sysconfig/network

  11. #[ ${NETWORKING} = "no" ] && exit 0

  12. #[ -r /etc/sysconfig/dund ] || exit 0

  13. #. /etc/sysconfig/dund

  14. #[ -z "$DUNDARGS" ] && exit 0

  15. start()

  16. {

  17. echo -n $"Starting memcached: "

  18. daemon $MEMCACHED -u daemon -d -m 128 -l 192.168.2.111 -p 11211

  19. echo

  20. }

  21. stop()

  22. {

  23. echo -n $"Shutting down memcached: "

  24. killproc memcached

  25. echo  

  26. }

  27. MEMCACHED="/usr/local/memcached/bin/memcached"

  28. [ -f $MEMCACHED ] || exit 1

  29. # See how we were called.

  30. case"$1"in

  31. start)

  32. start

  33. ;;

  34. stop)

  35. stop

  36. ;;

  37. restart)

  38. stop

  39. sleep 3

  40. start

  41. ;;

  42. *)

  43. echo $"Usage: $0 {start|stop|restart}"

  44. exit 1

  45. esac  

  46. exit 0

添加执行权限:

chmod 700 /etc/init.d/memcached

启动:

 
  1. [root@localhost ~]# service memcached start

  2. 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系统)

查看端口是否保在侦听状态:

 
  1. [root@localhost ~]# netstat -ntlp | grep mem

  2. 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)

 
  1. wget http://pecl.php.net/get/memcache-3.0.6.tgz

  2. tar zxvf memcache-3.0.6.tgz  

  3. cd memcache-3.0.6

  4. phpize

  5. ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config

  6. make && make install

5、编辑php.ini

 
  1. extension=memcache.so

  2. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

  3. memcache.allow_failover = 1

  4. memcache.max_failover_attempts = 20

  5. memcache.chunk_size = 8192

  6. memcache.default_port = 11211

  7. session.save_handler = memcache

  8. session.save_path = "tcp://192.168.2.111:11211"

 
  1. 注:

  2. extension=memcache.so --添加

  3. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"--修改

  4. memcache.allow_failover = 1 --添加

  5. memcache.max_failover_attempts = 20 --添加

  6. memcache.chunk_size = 8192 --添加

  7. memcache.default_port = 11211 --添加

  8. session.save_handler = memcache --修改

  9. session.save_path = "tcp://192.168.2.111:11211" --添加,本测试memcache和apache都在一个服务器上,这个根据实际需要设置地址。

配置完成

重启apache

6、新建一个php测试脚本,供客户端浏览器访问测试

vi /usr/local/apache/htdocs/test.php

 
  1. <?php

  2. session_start();

  3. if($_GET['act']=='write')

  4. $_SESSION['name']='0009847';

  5. elseif($_GET['act']=='read')

  6. var_dump($_SESSION);

  7. else

  8. echo 'invalid argument';

  9. ?>

访问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:

(完成)