源碼安裝memcached和php memcache擴展

源碼安裝memcached和php memcache擴展

注:如下安裝步驟以centos6.5系統爲例。php

1、安裝前準備

一、下載源碼包

http://www.memcached.org/nginx

目前最新版本是http://www.memcached.org/files/memcached-1.4.24.tar.gzcentos

二、安裝libevent

因爲memcached依賴於libevent庫,所以須要安裝libevent
#yum install libevent libevent-develmemcached

2、安裝memcached

一、建立memcached用戶

#useradd -d /var/run/memcached/ -s /sbin/nologin -c "Memcached daemon" memcachedphp-fpm

二、解壓源碼包

#tar zxvf memcached-1.4.24.tar.gz工具

三、編譯安裝

#cd memcached-1.4.24
#./configure --prefix=/usr/local/memcached
#make && make install測試

四、memcached經常使用參數說明

-m memcache最大可用內存數(單位:M)
-u 用戶
-l 監聽IP
-p 端口號rest

3、添加service腳本

#vi /etc/rc.d/init.d/memcachedcode

內容以下

#! /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.pidip

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

IP=127.0.0.1
PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=64
OPTIONS=""

# 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}  /usr/local/memcached/bin/memcached -d -l $IP  -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/local/memcached/bin
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

4、測試

一、啓動memcached服務

#/etc/rc.d/init.d/memcached start
默認memcached服務監聽在127.0.0.1:11211

二、測試

#telnet 127.0.0.1 11211

輸入 stats,正常顯示以下,說明安裝成功

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
STAT pid 12246
STAT uptime 2444
STAT time 1442302696
STAT version 1.4.24
STAT libevent 2.0.22-stable
STAT pointer_size 64
STAT rusage_user 0.089986
STAT rusage_system 0.027995
STAT curr_connections 5
STAT total_connections 7

STAT connection_structures 6

5、安裝memcached-tool工具

memcached-tool是memcached監控工具,是一個perl腳本,能夠直接運行。命令用法以下。

Usage: memcached-tool <host[:port]> [mode]

memcached-tool 10.0.0.5:11211 display    # shows slabs
   memcached-tool 10.0.0.5:11211            # same.  (default is display)
   memcached-tool 10.0.0.5:11211 stats      # shows general stats
   memcached-tool 10.0.0.5:11211 dump       # dumps keys and values

6、安裝php memcache擴展

一、下載源碼包

memcache-3.0.8.tgz

二、安裝memcache擴展

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

三、修改配置文件php.ini

添加 extension=memcache.so

四、測試

重啓nginx和php-fpm,查看phpinfo()顯示以下有信息,說明安裝成功。

Registered save handlers files user memcache

memcache support enabled

相關文章
相關標籤/搜索