Memcache的安装
1.分别下载memcached和libevent
Wget http://memcached.org/files/memcached-1.4.25.tar.gz
wget https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
2.先安装libevent:
# tar zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# ./configure --prefix=/usr
# make
# make install
3.测试libevent是否安装成功
ls -al /usr/lib |grep libevent
4.安装memcached,同时需要安装中指定libevent的安装位置:
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure --with-libevent=/usr
# make
# make install
5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
6.启动memcached
/usr/local/bin/memcached -d -m 10 -u memcache -p 11211 -c 256 -P /tmp/memcached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址
-p是设置Memcache监听的端口,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件
测试:
#Telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is ‘^]‘.
#set test 0 0 10
#test_value
STORED
#get test
VALUE test 0 10
test_value
END
#quit
Connection closed by foreign host.
7.关闭memcached
kill `cat /tmp/memcached.pid`
安装memcache的php扩展
1.下载memcache:
wget http://pecl.php.net/get/memcache-2.2.7.tgz
2.安装PHP的memcache扩展
tar vxzf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize (yum安装的php执行/usr/bin/phpize)
./configure –enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir (yum安装的php执行 ./configure --enable-memcache --with-php-config=/usr/bin/php-config --with-zlib-dir)
make
make install
若没有phpize命令 yum install php-devel
3.上述安装完后会有类似这样的提示:
Installing shared extensions: /usr/lib64/php/modules/
4.修改/etc/php.ini
extension_dir = "/usr/lib64/php/modules/"
extension=memcache.so
5.重新启动apache,启动memcached
service httpd restart
运行下面的php文件,如果有输出hello world!,表示环境搭建成功
<?php
$mem = new Memcache;
$mem->connect(‘127.0.0.1‘,11211);
$mem->set(‘test‘,‘hello world!‘,0,10);
$val = $mem->get(‘test‘);
echo $val;
?>
本文出自 “林羽萧” 博客,请务必保留此出处http://10308953.blog.51cto.com/10298953/1746087
原文地址:http://10308953.blog.51cto.com/10298953/1746087