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扩展
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'));
- ?>
Linux下安装Memcached软件
安装memcache源代码
在官网下载, 官网地址: http://memcached.org/
软件地址: http://www.memcached.org/files/memcached-1.4.24.tar.gz
1. 首先安装依赖包libevent
yum -y install libevent*
2.主包已经安装, 别忘记安装libevent-devel*, 不然./configure过不去
tar zxf /lamp/memcached-1.4.24.tar.gz #解压memcache
cd /lamp/memcached-1.4.24 #进入memcached目录
./configure --prefix=/usr/local/memcache #配置
make && make install #编译与安装
useradd memcache #添加memcache用户
3.因为系统不能用root运行memcache软件
/usr/local/memcache/bin/memcached -umemcache & #后台运行
netstat -tunpl | grep :11211 #查看端口
telnet 192.168.10.1 11211 #连接测试
status #memcache命令: 查看当前状态
4.写入自启动
vi /etc/rc.local
/usr/local/memcache/bin/memcached -umemcache &
5.如何杀掉后台进程
pkill memcached
也有一个php写的memcache监控工具可以下载
http://livebookmark.net/journal/2008/08/28/memcachephp-flushes-servers/
这篇文章是如何设置php session到memcache
http://koda.iteye.com/blog/466667
最后可以将phpmemcacheadmin安装一下, 方便查看
https://github.com/hgschmie/phpmemcacheadmin