据说Memcache用到了python的一个Socked处理,所以安装Memcache之前,还需要安装一个安装包,叫做libevent,下载地址为:http://monkey.org/~provos/libevent/。我下载的为libevent-1.4.13-stable.tar.gz,把该文件放入存放软件的常用文件夹,比如/usr/local/software, 并用命令进入该文件夹。
1.解压
tar -xvzf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
2.编译
./configure --prefix=/usr/local/libevent
3.安装
make
make install
接着,我们来安装Memcache,Memcache的下载地址为http://code.google.com/p/memcached/downloads/list,我下载的是memcached-1.4.4.tar.gz。把该文件放入存放软件的常用文件夹,比如/usr/local/software, 并用命令进入该文件夹。
1.解压
tar -xvzf memcached-1.4.4.tar.gz
cd memcached-1.4.4
2.编译
./configure --prefix=/usr/local/memcache --with-libevent=/usr/local/libevent
3.安装
make
make install
4.启动Memcache
/usr/local/memcache/bin/memcached -d -m 30 -u root
参数说明:
-d 启动一个守护进程
-m 30 分配30M的空间
-u root 运行Memcache的用户,当前为root
到这里,有的系统会提示诸如“error while loading shared libraries...”这样的错误,是因为,操作系统加载库文件时候没用从我们刚刚安装的libevent的lib文件夹加载,所以,我们需要修改系统的配置文件。
1.修改ld.so.conf文件
vim /etc/ld.so.conf,在文件末尾加上/usr/local/libevent/lib(刚才安装libevent的路径下的lib文件夹)
2.让配置文件生效
/sbin/ldconfig -v
至此,Memcache安装完毕。
要想让php能访问Memcache,需要安装一个php的Memcache扩展。下载地址为:http://pecl.php.net/package/memcache,我下载的为memcache-1.4.tgz,把该文件放入存放软件的常用文件夹,比如/usr/local/software, 并用命令进入该文件夹。
1.解压
tar -xvzf memcache-1.4.tgz
cd memcache-1.4
2.执行php的扩展模块的编译程序
/usr/local/php/bin/phpize
注意,一定要在memcache-1.4目录下执行,否则会提示错误:Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module。
2.编译
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
3.安装
make
make install
安装完毕后提示Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
4.修改php配置文件
vim /usr/local/php/conf/php.ini
(1)找到包含有“extension_dir”的这行,将其值修改为上面提到的:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
(2) 在下面加一条语句:extension=memcache.so
至此php的memcache扩展安装完毕.
可以写个测试程序test.php
<?php
$mem=new Memcache;
$mem->connect('127.0.0.1',11211);
$mem->set('gaohuiKey','gaohuiValue',NULL,86400);
$value=$mem->get('gaohuiKey');
$echo $value."\n";
?>