CentOS 安装Memcached和PHP memcache扩展

一:安装Memcached(服务端)
1.安装依赖包libevent
yum install libevent-devel
2.安装memcached​
wget http://www.memcached.org/files/memcached-1.4.35.tar.gz
tar -xzvf memcached-1.4.35.tar.gz
cd memcached-1.4.23
./configure --prefix=/usr/local/memcache
make && make install
3.启动memcached​
vi /etc/init.d/memcached
贴入以下代码
#!/bin/sh   
#
# memcached: MemCached Daemon
#
# chkconfig: - 90 25
# description: MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0
start()
{
echo -n $"Starting memcached: "
daemon $MEMCACHED -u daemon -d -m 1024 -l 127.0.0.1 -p 11211
echo
}
stop()
{
echo -n $"Shutting down memcached: "
killproc memcached
echo
}
#MEMCACHED memcache安装路径
MEMCACHED="/usr/local/bin/memcached"
[ -f $MEMCACHED ] || exit 1
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0


注意memcache路径。
cd /etc/init.d/
chmod 755 memcached
chkconfig --add memcached //添加memcached Service
chkconfig memcached on //设置开机启动
service memcached start //start stop restart 启动memcached
二.安装PHP Memcahce扩展
//注意phpize,php-config路径
wget 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/***指示的目录路径,在该目录下找到生成的memcached.so,将其复制到php.ini的extension_dir指向的目录。修改php.ini增加行extension = memcached.so重启apache,再到页面上打开phpinfo信息页面,看看memcached扩展是否安装成功了。
三:测试
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set('key','hello world',0,12);
echo $mem->get('key');
?>

0 个评论

要回复文章请先登录注册