源码安装memcached和php memcache扩展
注:以下安装步骤以centos6.5系统为例。
一、安装前准备
1、下载源码包
http://www.memcached.org/
目前最新版本是http://www.memcached.org/files/memcached-1.4.24.tar.gz
2、安装libevent
由于memcached依赖于libevent库,因此需要安装libevent
#yum install libevent libevent-devel
二、安装memcached
1、创建memcached用户
#useradd -d /var/run/memcached/ -s /sbin/nologin -c "Memcached daemon" memcached
2、解压源码包
#tar zxvf memcached-1.4.24.tar.gz
3、编译安装
#cd memcached-1.4.24
#./configure --prefix=/usr/local/memcached
#make && make install
4、memcached常用参数说明
-m memcache最大可用内存数(单位:M)
-u 用户
-l 监听IP
-p 端口号
三、添加service脚本
#vi /etc/rc.d/init.d/memcached
内容如下
#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid
# Standard LSB functions
#. /lib/lsb/init-functions
# Source function library.
. /etc/init.d/functions
IP=127.0.0.1
PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
# Check that networking is up.
. /etc/sysconfig/network
if [ "$NETWORKING" = "no" ]
then
exit 0
fi
RETVAL=0
prog="memcached"
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}
start () {
echo -n $"Starting $prog: "# Ensure that /var/run/memcached has proper permissionsif [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then chown $USER /var/run/memcachedfidaemon --pidfile ${pidfile} /usr/local/memcached/bin/memcached -d -l $IP -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONSRETVAL=$?echo[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
echo -n $"Stopping $prog: "killproc -p ${pidfile} /usr/local/memcached/binRETVAL=$?echoif [ $RETVAL -eq 0 ] ; then rm -f ${lockfile} ${pidfile}fi
}
restart () {
stop start
}
# See how we were called.
case "$1" in
start)
start;;
stop)
stop;;
status)
status -p ${pidfile} memcachedRETVAL=$?;;
restart|reload|force-reload)
restart;;
condrestart|try-restart)
[ -f ${lockfile} ] && restart || :;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force- reload|condrestart|try-restart}"RETVAL=2 ;;
esac
exit $RETVAL
四、测试
1、启动memcached服务
#/etc/rc.d/init.d/memcached start
默认memcached服务监听在127.0.0.1:11211
2、测试
#telnet 127.0.0.1 11211
输入 stats,正常显示如下,说明安装成功
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
STAT pid 12246
STAT uptime 2444
STAT time 1442302696
STAT version 1.4.24
STAT libevent 2.0.22-stable
STAT pointer_size 64
STAT rusage_user 0.089986
STAT rusage_system 0.027995
STAT curr_connections 5
STAT total_connections 7
STAT connection_structures 6
五、安装memcached-tool工具
memcached-tool是memcached监控工具,是一个perl脚本,可以直接运行。命令用法如下。
Usage: memcached-tool
memcached-tool 10.0.0.5:11211 display # shows slabs memcached-tool 10.0.0.5:11211 # same. (default is display) memcached-tool 10.0.0.5:11211 stats # shows general stats memcached-tool 10.0.0.5:11211 dump # dumps keys and values
六、安装php memcache扩展
1、下载源码包
memcache-3.0.8.tgz
2、安装memcache扩展
#tar zxvf memcache-3.0.8.tgz
#cd memcache-3.0.8
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make && make install
3、修改配置文件php.ini
添加 extension=memcache.so
4、测试
重启nginx和php-fpm,查看phpinfo()显示如下有信息,说明安装成功。
Registered save handlers files user memcache