|
Centos6.6+apache2.2+php5.3+mysql5.1下memcache的搭建
一、准备包
下载memcache所需包
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
(memecached服务端依赖)
wget http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz (memecached服务端)
wget http://pecl.php.net/get/memcache-3.0.5.tgz (php的memecached客户端)
可以到http://pecl.php.net/package/memcache查看更新
安装libevent和memcached见之前的文章。
安装memcache的memcache扩展(即php的客户端)
tar vxzf memcache-3.0.5.tgz
cd memcache-3.0.5
/usr/bin/phpize #生成configure编译文件,phpize是php模块的执行文件
如果这里出现
Can’t find PHP headers in /usr/include/php The php-devel package is required for use of this command
那么执行 yum install php-devel
如果出现Can't find config.m4 或者Cant't find autoconf
# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
# tar -zvxf m4-1.4.9.tar.gz
# cd m4-1.4.9/
# ./configure && make && make install
# cd ../
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
# tar -zvxf autoconf-2.62.tar.gz
# cd autoconf-2.62/
# ./configure && make && make install
// 然后执行以下命令进行安装
# /usr/local/php/bin/phpize
#./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent –with-php-config=/usr/local/php/bin/php-config
#make && make install
这一步如果使用yum 安装的php 则可以:
./configure -with-php-config=/usr/bin/php-config
也可指定php-config目录自定义安装(推荐)
./configure –enable-memcache –with-php-config=/usr/bin/php-config –with-zlib-dir
make && make install
如果make不成功提示zlib不存在的话运行sudo yum install zlib-devel即可
make install
安装完成后,提示
1.Installingssharedextensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
则安装.so文件成功。
2. Yum 安装的php 提示如下
Installing shared extensions: ”/usr/lib64/php/modules”
配置php.ini文件
修改extension_dir:
1.extension_dir = ”/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/”
2. yum 安装配置文件如下
3. extension_dir = ”/usr/lib64/php/modules”
4.添加一行以便加载memcache扩展:extension=memcache.so
extension=memcache.so
验证PHP中是否成功加载memcache扩展
Php -m | grep memcache
Memcache
模块安装成功
启动memcached服务端程序
1. memcached -d -m 10 -u root -l localhost -p 11211 -c 256 -P /tmp/memcached.pid
2. 如果运行到一步报错提示Libaraies 没有 或者不能共享
执行ldconfig (加载动态链接库)
执行上面操作之前 最好执行这一步ldconfig –C | grep libevent 看看有没有
结束memcache
kill ‘cat /tmp/memcached.pid’
查看memcache的运行状态
echo stats | nc 192.168.1.253 11211
实时查看memcache的运行状态
Watch “echo stats | nc 192.168.1.253 11211”
快速清空
Echo “flush_all” | nc 192.168.1.253 11211
需要有nc命令才能查看,没有请安装
测试是否成功,将下面写入一个php文件
<?php
$memcache = new Memcache();
$memcache->connect('<span style="font-family: Arial, Helvetica, sans-serif;">127.0.0.1</span><span style="font-family: Arial, Helvetica, sans-serif;">', 11211);</span>
$memcache->set(‘key’, ’Memcache test successful!’, 0, 60);
$result = $memcache->get(‘key’);
unset($memcache);
echo $result;
// 访问出现Memcache test successful! memcache就成功了</span>
// 将下面代码写入php,连续访问2次,看看效果有什么不同,这就是memcache的作用
<?php
$start = microtime(true);
$mem = new Memcache;
$mem->connect(“127.0.0.1″, 11211);
$key = ”key”;
$val = $mem->get($key);
if($val === false){
$val = some_calculate();
$mem->set($key, $val, 0, 60);
}
echo $val.”\n”;
echo microtime(true)-$start;
function some_calculate(){
//模拟复杂计算
echo ”calculate\n”;
sleep(2);
return ”result”;
}
?>
版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|
|