一、编译环境的准备
yum install gcc
yum install gcc-c++ libstdc++-devel
yum install zlib-devel
二、源码包准备
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
三、安装与配置
1、安装libevent
tar zxvf libevent-1.4.14b-stable.tar.gz cd libevent-1.4.14b-stable ./configure --prefix=/usr/local/libevent/ make && make install ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.22、安装Memcached
tar zxvf memcached-1.4.15.tar.gz cd memcached-1.4.15 ./configure --prefix=/usr/local/memcached/ --with-libevent=/usr/local/libevent/ make && make install
/usr/local/memcached/bin/memcached -d -m 64 -u root -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid4、为了方便管理,写个SHELL脚本。
# http://blog.phpha.com # 以下内容摘自互联网 vi /etc/rc.d/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 64 -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid 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 05、添加Memcached开机启动
cd /etc/rc.d/init.d/ chmod 777 memcached chkconfig --add memcached chkconfig --level 235 memcached on chkconfig --list | grep memcached6、Memcached使用
service memcached start service memcached stop service memcached restart四、PHP安装Memcache扩展(memcache与memcached扩展二选一即可,建议安装下面的memcached)
[root~] wget -c http://pecl.php.net/get/memcache-3.0.8.tgz [root~] tar -zxvf memcache-3.0.8.tgz [root~] cd memcache-3.0.8 [root~] /usr/local/php/bin/phpize [root~] ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache --with-zlib-dir [root~] make && make install /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so [root~] vim /usr/local/php/etc/php.ini extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so session.save_handler = memcache session.save_path = "tcp://127.0.0.100:11211" [root~] service php-fpm reload
php5.5+版本的memcache扩展安装方法:
因为php5.5+版本内置了memcache扩展 所以无需下载第三方插件直接 执行如下操作即可 cd /usr/local/php/bin ./pecl install memcache 安装时会提示是否开启session支持,根据需要输入no 或者 yes,安装后路径如下: /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so 需要手动配置到php.ini中,增加如下内容,即表示启用memcache扩展: extension=memcache.so补充:PHP的Memcache扩展的安装后发现一个问题,SESSION会出现丢失,这可能是安装Memcached服务端的时候,防火墙没有添加11211端口,修改防火墙规则后,一切正常。
五、PHP memcached 扩展的安装(注意这里是memcached非memcache)
wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz tar -zxvf libmemcached-1.0.16.tar.gz cd libmemcached-1.0.16 ./configure --prefix=/usr/local/libmemcached --with-memcached make && make install wget http://pecl.php.net/get/memcached-2.2.0.tgz tar zxvf memcached-2.2.0.tgz cd memcached-2.2.0 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install vi /usr/local/php/etc/php.ini 加上extension=memcached.so重启一下php服务就可以了。如果安装时报错误: checking for libmemcached location... configure: error: memcached support requires libmemcached. Use --with-libmemcached-dir=<DIR> to specify the prefix where libmemcached headers and library are located 请使用如下方法: php的扩展memcached的安装二 tar zxvf memcached-2.2.0.tgz cd memcached-2.2.0 /usr/local/php/bin/phpize ./configure --with-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached make && make install vi /usr/local/php/etc/php.ini 加上extension=memcached.so重启php就可以了(摘取天上星)
1、直接修改php.ini配置文件
session.save_handler = memcache session.save_path = "tcp://127.0.0.100:11211"2、利用目录下的 .htaccess 文件
php_value session.save_handler "memcache" php_value session.save_path "tcp://127.0.0.1:11211"3、项目中修改配置
ini_set("session.save_handler", "memcache"); 使用多个 memcached server 时用逗号","隔开,并且和 Memcache::addServer() 文档中说明的一样, 可以带额外的参数"persistent"、"weight"、"timeout"、"retry_interval" 等等,类似这样的:"tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2" ini_set("session.save_path", "tcp://127.0.0.100:11211");上面只是一些简单的说明,一般来说做分布式是有服务器权限的,所以推荐第一种
Memcached存储session的方法是不加“tcp://“前缀的:
如果安装的PECL是memcached(使用libmemcache库的那个),则配置应为 ini_set("session.save_handler", "memcached"); // 记住是memcached不是memcache ini_set("session.save_path", "127.0.0.1:11211"); // 不要tcp:
启动 memcached参数说明: memcached -d -l 127.0.0.1 -p 11212 -m 128 或 启动Memcache的服务器端: memcached -d -m 100 -u root -l 192.168.36.200 -p 11211 -c 256 -P /tmp/memcached.pid # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid 引用 -d选项是启动一个守护进程, -m是分配给Memcache使用的内存数量,单位是MB,我这里是100MB, -u是运行Memcache的用户,我这里是root, -l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.36.200, -p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,我们这里统一使用11211 -c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定。 -P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid,