I.安装Memcahce
1. 安装依赖包libevent
Memcache需要安装libevent,所以安装前可能需要执行
yum install libevent-devel
2.安装memcache
从http://memcached.org/下载最源码新版1.4.13
tar -xzvf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure --prefix=/usr/local/memcache
make && make install
3.配置memcached自启动
复制源码目录下scripts/memcached.sysv到/etc/init.d/memcached,需要修改/etc/init.d/memcached如下行:
chown $USER /usr/local/memcache/bin/memcached
daemon /usr/local/memcache/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
(就是修改memcached所在路径)
然后执行如下命令即可:
chmod 755 memcached
chkconfig --add memcached
启动试试看
service memcached start
II.安装PHP Memcahce扩展
引用
pecl.php.net有两个memcache扩展:
memcache memcached extension
memcached PHP extension for interfacing with memcached via libmemcached library
memcached 的版本比较新,而且使用的是 libmemcached 库。libmemcached 被认为做过更好的优化,应该比 php only 版本的 memcache 有着更高的性能。所以这里安装的是memcached(假设php5.4已经安装在在/usr/local/php).
1. 安装依赖库
https://launchpad.net/libmemcached/1.0/1.0.4/+download/libmemcached-1.0.4.tar.gz
tar -xzvf libmemcached-1.0.4.tar.gz
cd libmemcached-1.0.4
./configure
make
make install
2.安装memcached
http://pecl.php.net/get/memcached-2.0.1.tgz下载,安装指令:
tar vxzf memcached-2.0.1.tgz
cd memcache-2.0.1
phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
make
make install
装完后,最后显示Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/,在该目录下生成memcached.so,将其复制到php.ini的extension_dir指向的目录。修改php.ini增加行extension = memcached.so重启apache,再到页面上打开phpinfo信息页面,看看memcached扩展是否安装成功了。
写个测试看看
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$m->set('username', 'Allen');
var_dump($m->get('username'));
?>
也有一个php写的memcache监控工具可以下载
http://livebookmark.net/journal/2008/08/28/memcachephp-flushes-servers/
这篇文章是如何设置php session到memcache
http://koda.iteye.com/blog/466667