Centos 下编译安装Memcache

452 查看

安装环境说明:

操作系统:Centos 6.5 -64bit

安装路径:/usr/local/memcached

开始编译memcached

安装编译需要的扩展包

yum install libevent libevent-devel

开始编译memcached

tar -zxvf memcached-1.4.21.tar.gz
cd memcached-1.4.21
./configure --prefix=/usr/local/memcached
make
make install

安装PHP memcache 扩展包

tar -zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config 
make 
make install

# 安装成功后会输出这个地址,这是php扩展模块文件夹
/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/ 

创建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 - for other linux
#. /etc/rc.d/init.d/functions
# Source function library - for suse linux
. /lib/lsb/init-functions


ip="192.168.25.129"             # 绑定侦听的IP地址
port="11211"                    # 服务端口
username="root"                 # 运行程序的用户身份
max_memory=64                   # default: 64M | 最大使用内存
max_connection=1024             # default: 1024 | 最大同时连接数



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"
pidfile=/tmp/memcached.pid
basedir=/usr/local/memcached
cmd=${basedir}/bin/memcached

start () {
  echo -n $"Starting service:$prog "
  chown $username $basedir
  $cmd -d -p $port -u $username  -m $max_memory -c $max_connection -P $pidfile $OPTIONS
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
  return $retval
}

stop() {
    echo -n $"Stopping service: $prog  "
    current_user=$(whoami)
    pidlist=$(ps -ef | grep $current_user | grep memcached | grep -v grep | awk '{print $2}')
    for pid in $pidlist ; do
        kill -9 $pid
        if [ $? -ne 0 ]; then
           return 1
        fi
    done
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/memcached
}

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 $?

脚本配置

# 将此脚本cp到/etc/init.d目录下并改名为memcached,然后增加执行权限
cp memcached /etc/init.d/memcached
chmod a+x /etc/init.d/memcached

配置开启自启动

chkconfig --add memcached              # 增加开启启动
chkconfig --level 2345 memcached on    # 设置启动级别

测试启动,停止,重启memcached

# 服务启动,停止
service memcached start
service memcached stop

# init.d目录启动,停止
/etc/init.d/memcached start
/etc/init.d/memcached stop

配置PHP支持memcached

编辑php.ini文件

vim /usr/local/php/etc/php.ini

# 修改以下内容
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"

# 增加扩展到php
extension = memcache.so

重新启动php-fpm

/etc/init.d/php-fpm restart

在php测试下是否成功

<?php
$memcache = new Memcache ();
$memcache->connect('192.168.25.129', 11211);
$memcache->set('key', 'test');
echo $memcache->get('key');

结果截图( 成功输出了缓存值 ):

总结

到这里我们memcache缓存就编译安装完成了,memcache是很强大的缓存系统,加油吧....如果兄弟们安装遇到问题可以加我QQ:418250505 大家一起讨论,共同进步~!