安装环境说明:
操作系统:Centos 6.7 -64bit
软件版本:memcached-1.4.33、memcache-2.2.7、libevent-2.0.22
安装路径:/usr/local/libevent
/usr/local/memcached
开始编译libevent
方式一:
yum install libevent-devel
方式二:编译安装
tar -zxxf libevent-2.0.22-stable.tar.gz
./configure --prefix=/usr/local/libevent
make && make install
echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
ldconfig
开始编译memcached
tar -zxvf memcached-1.4.33.tar.gz
cd memcached-1.4.33
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install
创建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.10.100" # 绑定侦听的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
安装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-20131226
配置PHP支持memcached
vim /etc/php.ini
#编辑php.ini文件增加以下内容
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226"
extension = memcache.so
#重新启动php-fpm
/etc/init.d/php-fpm restart
在php测试下是否成功
<?php
echo "www.jcwtime.cn<br/>\n";
$mem = new Memcache;
$mem->connect("192.168.10.101", 11211) or die("Could not connect");
$version = $mem->getVersion();
echo "Server's version: ".$version."<br/>\n";
$mem->set('key', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";
$get_result = $mem->get('key');
echo "$get_result is from memcached server.";
?>