由于项目上线后需要考虑分布式,SESSION准备放在Memcache来同步,下面是一些配置及随手的记录。

1、安装libevent

[root@PHPHa ~]# wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
[root@PHPHa ~]# tar zxvf libevent-1.4.14b-stable.tar.gz
[root@PHPHa ~]# cd libevent-1.4.14b-stable
[root@PHPHa ~]# ./configure --prefix=/usr/local/libevent/
[root@PHPHa ~]# make && make install
[root@PHPHa ~]# ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2

2、安装Memcached

[root@PHPHa ~]# wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
[root@PHPHa ~]# tar zxvf memcached-1.4.15.tar.gz
[root@PHPHa ~]# cd memcached-1.4.15
[root@PHPHa ~]# ./configure --prefix=/usr/local/memcached/ --with-libevent=/usr/local/libevent/
[root@PHPHa ~]# make && make install

3、启动Memcached

[root@PHPHa ~]# /usr/local/memcached/bin/memcached -d -m 64 -u root -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid

4、为了方便管理,写个SHELL脚本吧。

# http://blog.phpha.com
# 以下内容摘自互联网
[root@PHPHa ~]# vi /etc/rc.d/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 64 -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid
        echo
}
stop()
{
        echo -n $"Shutting down memcached: "
        killproc memcached
        echo
}
MEMCACHED="/usr/local/memcached/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

5、添加Memcached开机启动

[root@PHPHa ~]# cd /etc/rc.d/init.d/
[root@PHPHa ~]# chmod 777 memcached
[root@PHPHa ~]# chkconfig --add memcached
[root@PHPHa ~]# chkconfig --level 235 memcached on
[root@PHPHa ~]# chkconfig --list | grep memcached

6、Memcached使用
经过上面的步骤,可以如下进行开启、停止、重启等操作。

[root@PHPHa ~]# service memcached start
[root@PHPHa ~]# service memcached stop
[root@PHPHa ~]# service memcached restart

标签:Memcached / CentOS