CentOS 安装 Memcached

memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著。

安装约定

源码路径:/usr/local/src
安装路径:/usr/local/memcached

创建www用户和组

# groupadd -g 500 www
# useradd -u 500 -g www -c "www user" -d /var/lib/www -s /sbin/nologin www

下载源代码包

在官网如下地址下载最新版

http://memcached.org/

# cd /usr/local/src/
# wget http://www.memcached.org/files/memcached-1.4.33.tar.gz

安装gcc、make等

# yum -y install gcc gcc-c++ make autoconf automake

安装编译memcached所需的库

# yum -y install libevent-devel

安装memcached

# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15

编译

# ./configure --prefix=/usr/local/memcached
# make
# make install

配置memcached启动脚本

将如下代码复制粘贴到/etc/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

PORT=11211
USER=www
MAXCONN=1024
CACHESIZE=64
OPTIONS=""

if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi

# 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 permissions
if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
chown $USER /var/run/memcached
fi

daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} /usr/bin/memcached
RETVAL=$?
echo
if [ $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} memcached
RETVAL=$?
;;
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

启动memcached

# ln -s /usr/local/memcached/bin/memcached /usr/bin/
# mkdir /var/run/memcached
# chown www:www /var/run/memcached
# chmod +x /etc/init.d/memcached
# service memcached start

设置memcached开机启动

# chkconfig memcached on

下载memcached监控脚本

# wget http://livebookmark.net/memcachephp/memcachephp.zip

将他放在php网站目录下修改好用户账号以及服务器IP和端口号即可通过浏览器访问