1,memcached需要libevent,所以要先安装它
下载地址:http://download.chinaunix.net/download.php?id=45065&ResourceID=5804
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
make
sudo make install
2,安装memcached
wget http://memcached.org/files/memcached-1.5.5.tar.gz
tar xf memcached-1.5.5.tar.gz
cd memcached-1.5.5/
./configure --with-libevent=/usr/local/libevent
make && sudo make install
3,安装php的memcache扩展
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7/
/usr/local/php54/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php54/bin/php-config --with-zlib-dir
【如果没有zlib,先安装zlib】
wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make && sudo make install
接着再安装memcache
make && sudo make install
4,在php.ini中添加扩展
extension=memcache.so
5,启动memcached服务端
/usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
6,编写测试文件
<?php $mem = new Memcache; $mem->connect( "127.0.0.1", 12000 ); $mem->set( "hi", "hello,ghostwu", 0, 120 ); echo $mem->get( "hi" ) . PHP_EOL; ?>