Linux下的Memcache安装

最近有个新项目应用memcache,记录下Memcache安装的过程。
LinuxMemcache服务器端的安装
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.2.6
下载:http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libeventlibevent的最新版本是libevent-1.4.8-stable。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.4.8-stable.tar.gzwget指令直接下载这两个东西.下载回源文件后。
1.
先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure –prefix=/usr/local/ libevent-1.4.8-stable;然后make;然后make install
2.
再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure  --prefix=/usr/local/memcached-1.2.6 –with-libevent=/usr/local/ libevent-1.4.8-stable --enable-64bit --enable-threads;然后make;然后make install
这样就完成了LinuxMemcache服务器端的安装。详细的方法如下:

1.分别把memcachedlibevent下载回来,放到 /tmp 目录下:
# cd /tmp
# wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
# wget http://www.monkey.org/~provos/ libevent-1.4.8-stable.tar.gz
2.先安装libevent
# tar zxvf libevent-1.4.8-stable.tar.gz
# cd libevent-1.4.8-stable
# ./configure --prefix=/usr/local/libevent-1.4.8-stable
# make
# make install
3.测试libevent是否安装成功:
# ls -al /usr/local/libevent-1.4.8-stable/lib | grep libevent
vi /etc/ld.so.conf
加入   /usr/local/libevent-1.4.8-stable/lib运行 ldconfig否则运行memcached的时候,会提示找不到so文件
4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /tmp
# tar zxvf memcached-1.2.6.tar.gz
# cd memcached-1.2.6
#./configure  --prefix=/usr/local/memcached-1.2.6 --with-libevent=/usr/local/libevent-1.4.8-stable --enable-64bit --enable-threads
# make
# make install
#ln -s /usr/local/memcached-1.2.6 /usr/local/memcached
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/memcached/bin/memcached
5.测试是否成功安装memcached
# ls -al /usr/local/memcached/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
安装MemcachePHP扩展
1.
http://pecl.php.net/package/memcache 选择相应想要下载的memcache版本。
2.
安装PHPmemcache扩展#tar vxzf memcache-2.2.4.tgz
#cd memcache-2.2.4
#/usr/local/php/bin/phpize
#./configure  --enable-memcache -with-php-config=/usr/local/php/bin/php-config -with-zlib-dir
#make
#make install
3.上述安装完后会有类似这样的提示:Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/
4.php.ini中的extension_dir = “./”修改为extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”
5.添加一行来载入memcache扩展:extension=memcache.so
memcached的基本设置
1.
启动Memcache的服务器端:
# /usr/local/bin/memcached -d -m 1024 -u nobody -l 192.168.0.200 -p 11211 -c 4096 -P /var/run/memcached/memcached.pid
-d选项是启动一个守护进程,
-m
是分配给Memcache使用的内存数量,单位是MB,我这里是1024MB
-u
是运行Memcache的用户,我这里是nobody
-l
是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200
-p
是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c
选项是最大运行的并发连接数,默认是1024,我这里设置了4096,按照你服务器的负载量来设定,
-P
是设置保存Memcachepid文件,我这里是保存在 /var/run/memcached/memcached.pid
2.如果要结束Memcache进程,执行:# kill `cat /tmp/memcached.pid`也可以启动多个守护进程,不过端口不能重复。
3.重新加载php
也可以写一个运行脚本来处理memcached

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# Source function library.
. /etc/rc.d/init.d/functions
MEMCACHED=/usr/local/memcached/bin/memcached
PFILE=/var/run/memcached/memcached.pid
IP_ADDR=192.168.0.200
PORT=11211
USER=nobody
MAXCONN=4096
CACHESIZE=1024
THREADS=4
OPTIONS="-d -r -M"
if [ ! -f ${MEMCACHED} ]; then
        exit 1
fi
if [ -f /etc/sysconfig/memcached ];then
        . /etc/sysconfig/memcached
fi
# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
        exit 0
fi
RETVAL=0
prog="memcached"
start () {
        echo -n $"Starting $prog: "
        # insure that /var/run/memcached has proper permissions
        if [ ! -d /var/run/memcached ]; then
                mkdir -p /var/run/memcached
                chown $USER /var/run/memcached
        fi
        daemon ${MEMCACHED} -l $IP_ADDR -p $PORT -t $THREADS -u $USER  -m $CACHESIZE -c $MAXCONN -P $PFILE $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
        echo -n $"Stopping $prog: "
        killproc ${MEMCACHED}
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
            rm -f /var/lock/subsys/memcached
            rm -f /var/run/memcached.pid
        fi
}
restart () {
        stop
        start
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status memcached
        ;;
  restart|reload)
        restart
        ;;
  condrestart)
        [ -f /var/lock/subsys/memcached ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
        exit 1
esac
exit $?

 
Memcache环境测试
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始领略Memcache的魅力把!
< ?php
$mem = new Memcache;
$mem->connect(”192.168.0.200″, 11211);
$mem->set(’key’, ‘This is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>
 
标签: 原创 Linux PHP Memcache
评论: 0 | 引用: 0 | 阅读: 7054