CentOS PHP与memcache快速搭建(一)

1、由于CentOS系统默认源没有memcache安装包,因此需要导入第三方的源。执行如下两条命令:

[root@EACNCTCSHHSHH00B001 data]# wget ftp://fr2.rpmfind.net/linux/epel/5/ppc/epel-release-5-4.noarch.rpm

[root@EACNCTCSHHSHH00B001 data]# rpm -ivh epel-release-5-4.noarch.rpm

2、yum安装Memcache服务器与php扩展

[root@EACNCTCSHHSHH00B001 data]# yum install memcached php-pecl-memcache

此时应该能正常安装这两个包,而不出现无法找到的情况。

3、安装成功后,检测php是否正常加载了memcache模块:

[root@EACNCTCSHHSHH00B001 data]# php -m|grep memcache

memcache

返回了“memcache”表示已经安装。

4、设置memcached服务开机自动启动

[root@EACNCTCSHHSHH00B001 data]# chkconfig memcached on

5、启动memcached服务并重启Apache

[root@EACNCTCSHHSHH00B001 data]# /etc/init.d/memcached start

启动 memcached:[确定]

[root@EACNCTCSHHSHH00B001 data]# /etc/init.d/httpd restart

停止 httpd:[确定]

启动 httpd:[确定]

6、测试php支持memcache是否正常

在apache的网站根目录建立 memcache.php 文件

[root@EACNCTCSHHSHH00B001 www]# vi memcache.php

内容如下:

1
2
3
4
5
6
7
8
<?php
$memcache = new Memcache();
$memcache->connect('127.0.0.1', 11211);
$memcache->set('key', 'Memcache test successful!', 0, 60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>

如果一切正常,访问此页面,应该正常返回“Memcache test successful”,至此,Memcached与php扩展memcache安装成功。

Memcached的默认端口为11211,因此在php中使用此端口即可。下面顺便给出个清除memcache所有缓存内容的方法:

执行:

[root@EACNCTCSHHSHH00B001 data]# nc localhost 11211

然后输入:

flush_all

quit