安装memcached需要libevent库的支持,由于memcache配置文件中对libevent的支持方式为–with-libevent=PATH Specify path to libevent installation,此处就用源码安装的方式
服务端配置
下载 libevent
http://libevent.org/ 最新稳定版 libevent-2.0.16-stable.tar.gz
#tar zxvf libevent-2.0.16-stable.tar.gz
#cd libevent-2.0.16-stable
#./configure –prefix=/usr/local/libevent
#make; make install
下载memcahce服务器
http://memcached.org/ 最新稳定版本 memcached-1.4.10.tar.gz
#tar zxvf memcached-1.4.10.tar.gz 解压
#cd memcached-1.4.10
#./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent
#make; make install
客户端配置(php)
下载php的memcache扩展(源码方式安装)
http://pecl.php.net/package/memcache 最新版本 memcache-2.2.6.tgz
# tar zxf memcache-2.2.6.tgz
# cd memcache-2.2.6
# /usr/local/php/bin/phpize (这个是php的安装路径)生成configure文件
#./configure –enable-memcache=/usr/local/memcached –with-php-config=/usr/local/php/bin/php-config –with-apxs2==/usr/local/apache2/bin/apxs
#make; make install
安装完后会有类似这样的提示:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626
修改php.ini
extension_dir = “./”
修改为
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626″
并添加一行
extension = “memcache.so”
重启apache,使memcache有效
#service httpd restart
通过phpinfo 可以查看 php的memcache扩展是否装好
下面测试memcache服务器
<?php
$memcache = new Memcache;
$memcache->connect(‘localhost’, 12000) or die (“Could not connect”);
$version = $memcache->getVersion();
echo “Server’s version: “.$version.”<br/>\n”;
$tmp_object = new stdClass;
$tmp_object->str_attr = ‘test’;
$tmp_object->int_attr = 123;
$memcache->set(‘key’, $tmp_object, false, 10) or die (“Failed to save data at the server”);
echo “Store data in the cache (data will expire in 10 seconds)<br/>\n”;
$get_result = $memcache->get(‘key’);
echo “Data from the cache:<br/>\n”;
var_dump($get_result);
显示结果:
Server’s version: 1.4.10
Store data in the cache (data will expire in 10 seconds)
Data from the cache:
object(stdClass)#3 (2) { ["str_attr"]=> string(4) “test” ["int_attr"]=> int(123) }
memcache搭建完毕