linux-CentOS6.4安装Memcached+memcached扩展+安装memcache扩展+Memcache同步SESSION的几种方法

一、编译环境的准备

  yum install gcc
  yum install gcc-c++ libstdc++-devel
  yum install zlib-devel

二、源码包准备

wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz

三、安装与配置

1、安装libevent

tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent/
make && make install
ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2
2、安装Memcached

tar zxvf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure --prefix=/usr/local/memcached/ --with-libevent=/usr/local/libevent/
make && make install

3、启动Memcached

/usr/local/memcached/bin/memcached -d -m 64 -u root -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid
4、为了方便管理,写个SHELL脚本。
# http://blog.phpha.com
# 以下内容摘自互联网
vi /etc/rc.d/init.d/memcached
#!/bin/sh
#
# memcached:    MemCached Daemon
# chkconfig:    - 90 25
# description:  MemCached Daemon
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0
start()
{
        echo -n $"Starting memcached: "
        daemon $MEMCACHED -u daemon -d -m 64 -l 127.0.0.100 -p 11211 -c 128 -P /tmp/memcached.pid
        echo
}
stop()
{
        echo -n $"Shutting down memcached: "
        killproc memcached
        echo
}
MEMCACHED="/usr/local/memcached/bin/memcached"
[ -f $MEMCACHED ] || exit 1
# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                sleep 3
                start
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
esac
exit 0
5、添加Memcached开机启动

cd /etc/rc.d/init.d/
chmod 777 memcached
chkconfig --add memcached
chkconfig --level 235 memcached on
chkconfig --list | grep memcached
6、Memcached使用

service memcached start
service memcached stop
service memcached restart
四、PHP安装Memcache扩展(memcache与memcached扩展二选一即可,建议安装下面的memcached)
[root~] wget -c http://pecl.php.net/get/memcache-3.0.8.tgz
[root~] tar -zxvf memcache-3.0.8.tgz
[root~] cd memcache-3.0.8
[root~] /usr/local/php/bin/phpize
[root~] ./configure  --with-php-config=/usr/local/php/bin/php-config --enable-memcache --with-zlib-dir
[root~] make && make install
/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so
[root~] vim /usr/local/php/etc/php.ini
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so
session.save_handler = memcache
session.save_path = "tcp://127.0.0.100:11211"
[root~] service php-fpm reload

php5.5+版本的memcache扩展安装方法:

因为php5.5+版本内置了memcache扩展 所以无需下载第三方插件直接 执行如下操作即可
cd /usr/local/php/bin
./pecl install memcache
安装时会提示是否开启session支持,根据需要输入no 或者 yes,安装后路径如下:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so
需要手动配置到php.ini中,增加如下内容,即表示启用memcache扩展:
extension=memcache.so

补充:PHP的Memcache扩展的安装后发现一个问题,SESSION会出现丢失,这可能是安装Memcached服务端的时候,防火墙没有添加11211端口,修改防火墙规则后,一切正常。

五、PHP memcached 扩展的安装(注意这里是memcached非memcache)

wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz
tar -zxvf libmemcached-1.0.16.tar.gz 
cd libmemcached-1.0.16 
./configure --prefix=/usr/local/libmemcached --with-memcached
make && make install 

wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
/usr/local/php/bin/phpize
./configure  --with-php-config=/usr/local/php/bin/php-config
make && make install
vi /usr/local/php/etc/php.ini
加上extension=memcached.so重起一下服务就可以了。

六、PHP项目分布式使用Memcache同步SESSION的几种方法

1、直接修改php.ini配置文件

session.save_handler = memcache
session.save_path = "tcp://127.0.0.100:11211"
2、利用目录下的 .htaccess 文件
php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"
3、项目中修改配置
ini_set("session.save_handler", "memcache");
ini_set("session.save_path", "tcp://127.0.0.100:11211");
上面只是一些简单的说明,一般来说做分布式是有服务器权限的,所以推荐第一种


linux-CentOS6.4安装Memcached+memcached扩展+安装memcache扩展+Memcache同步SESSION的几种方法,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。