Memcached需要libevent支持,所以需要先检查libevent是否已经安装。
下面是Memcached的安装过程:
# tar zvxf memcached-1.4.9.tar.gz
# cd memcached-1.4.9
# ./configure --prefix=/usr/local/memcached
# make
# make install
启动Memcached:
# /usr/local/memcached/bin/memcached -d -m 128 -l 127.0.0.1 -p 11211 -u root
参数128指定了内存使用量
接下来是为PHP安装memcache扩展:
# gzip -d memcache-2.2.6.tgz
# tar xvf memcache-2.2.6.tar
# cd memcache-2.2.6
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make
# make install
最后将如下配置信息添加到php.ini文件中:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension=memcache.so
备注:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/这个路径可能会有不同,以您机器中的为准。
Memcached已在Linux中安装完毕,且被部署到/usr/local/memcached这个路径下,接下来我们来开始为其安装用于启动和停止的服务管理脚本。
首先,通过vi在/etc/rc.d/init.d路径下新建一个名为memcached的脚本文件,命令如下:
vi /etc/rc.d/init.d/memcached
然后,向其中写入Shell脚本如下:
#!/bin/sh
#
# chkconfig: 2345 90 50
# description: Memcached Service Daemon
#
# processname: Memcached
#
# 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
MEMCACHED="/usr/local/memcached/bin/memcached"
start()
{
echo -n $"Starting Memcached: "
daemon $MEMCACHED -u daemon -d -m 32 -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
echo
}
stop()
{
echo -n $"Shutting down Memcached: "
killproc memcached
echo
}
[ -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
保存退出vi后,执行下面的命令来安装这个脚本
chmod 777 /etc/rc.d/init.d/memcached
chkconfig --add memcached
chkconfig --level 235 memcached on
然后可以通过下面的命令来检查是否安装成功
chkconfig --list | grep memcached
service memcached start
service memcached stop
service memcached restart