Write the Code. Change the World.

4月 04

检查安装 Libevent

Memcached 的安装依赖 Libevent。所以,先检查系统是否安装了 Libevent。还有安装版本是否落后。

rpm -q libevent //检查是否安装了 libevent
# libevent-2.0.21-4.el7.x86_64 

这里已经安装了,不过看了下 libevent 官网,目前最新版本是 2.1.8 先卸载旧的安装个新的。

libevent 官网: http://libevent.org/

安装 memcached 服务,再安装 php memcached扩展。

rpm -e --nodeps libevent-2.0.21-4.el7.x86_64

wget wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz

tar -xzvf libevent-2.1.8-stable.tar.gz 

cd libevent-2.1.8-stable

./configure --prefix=/usr/libevent

make && make install

到此,新版本的 libevent 已经安装完成。

安装 Memcached

官网: http://memcached.org/

当前,最新版本1.5.7 先下载,再解压安装。

cd /usr/local/src/

wget http://www.memcached.org/files/memcached-1.5.7.tar.gz

cd memcached-1.5.7

./configure --prefix=/usr/memcached --with-libevent=/usr/libevent #配置

make && make install #编译安装

cd /usr/memcached/bin #进入程序目录

./memcached -u root -d # 启动

# 下边设置开机自启动

方法一(自测没效果,估计写的不对):
vim /etc/rc.local

# 尾部追加 /usr/memcached/bin/memcached -u root -d

方法二(亲测有效):

cd scripts
cp memcached.sysv /etc/init.d/memcached
vim /etc/init.d/memcached

# 修改
#PORT=11211
#USER=root
#MAXCONN=1024
#CACHESIZE=64
#OPTIONS=""
# ……
# prog="/alidata/service/memcached/bin/memcached"

然后执行下边命令

chmod +x /etc/init.d/memcached
chkconfig memcached on
service memcached start

完整的如下。记得修改成你自己的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

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

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="/alidata/service/memcached/bin/memcached"
prog="memcached"

start () {
    echo -n $"Starting $prog: "
    # insure that /var/run/memcached has proper permissions
    chown $USER /alidata/service/memcached
    daemon /alidata/service/memcached/bin/memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /alidata/service/memcached/memcached.pid $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 /alidata/service/memcached/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 $?

查看进程 ps -ef | grep memcached

停止

memcached进程的pid

pgrep memcached

# 15650

kill -9 15650 # -9表示强制杀死 

在来安装 php memcached 扩展。

安装 php memcached 扩展

先安装 libmemcached 库。
libmemcached 官网: http://libmemcached.org/

cd /usr/local/src/

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz # 当前最新版本

tar -xzvf libmemcached-1.0.18.tar.gz

cd libmemcached-1.0.18

./configure --prefix=/usr/local/libmemcached # 配置,安装目录

make && make install

再 安装php memcached 扩展

cd /usr/local/src/

wget http://pecl.php.net/get/memcached-3.0.4.tgz

tar -xzvf memcached-3.0.4.tgz

cd memcached-3.0.4

find / -name phpize # 找到 phpize 并运行

./configure --with-php-config=/alidata/service/php/bin/php-config \
--with-libmemcached-dir=/usr/local/libmemcached \
--disable-memcached-sasl  # 配置

make && make install

再编译 php.ini,追加配置。

cd /alidata/service/php/etc/php.ini

vim php.ini

# extension=memcached.so //末尾追加,保存

/etc/init.d/php-fpm reload  //重启

php -m  #就可以查看到memcached 扩展了。

发表评论

电子邮件地址不会被公开。 必填项已用*标注