首页 > 代码库 > 集群三 php+memcached缓存服务器

集群三 php+memcached缓存服务器

nginx+php 安装
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel gd gd-devel curl curl-devel e2fsprogs e2fsprogs-devel  krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre pcre-devel ImageMagick ImageMagick-devel git

mkdir /hequan && cd /hequan
wget http://cn2.php.net/distributions/php-5.6.22.tar.bz2
wget http://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
wget http://nchc.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget http://pecl.php.net/get/memcache-2.2.7.tgz
wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
wget http://pecl.php.net/get/imagick-3.1.2.tgz
git clone https://github.com/cfsego/ngx_log_if.git
php安装简单版
./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql --with-mysqli --with-config-file-scan-dir=/etc/php.d --enable-fpm --with-gd --with-iconv --with-zlib --enable-xml --enable-inline-optimization --with-openssl --enable-pcntl --enable-zip --with-curl --with-bz2 --with-jpeg-dir --with-png-dir --enable-mbstring

tar -zvxf libiconv-1.14.tar.gz
cd libiconv-1.14/srclib
sed -i -e ‘/gets is a security/d‘ ./stdio.in.h
cd ../
./configure --prefix=/application/
make
make install
cd /hequan
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd /hequan
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
cd /hequan
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
export LD_LIBRARY_PATH=/usr/local/lib: LD_LIBRARY_PATH
./configure
make
make install
cd /hequan/
tar -jxvf php-5.6.22.tar.bz2
cd php-5.6.22
./configure --prefix=/usr/local/php --with-config-file-path=/application/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
make ZEND_EXTRA_LIBS=‘-liconv‘
make install
cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod u+x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
groupadd www
useradd -s /sbin/nologin -g www -M www
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php.ini /etc/php.ini
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
 /usr/local/php/sbin/php-fpm
iptables -I INPUT -s 172.16.10.0/24 -p tcp --dport 9000 -j ACCEPT
zlib====》提供数据压缩用的函式库
pcre====》rewrite的支持库
openssl====》https的支持库
libiconv====》字符编码转换库
libxml====》XML库
libpng====》图片库
freetype====》字库
libmcrypt====》加密算法
mhash====》加密库
curl====》URL传输
gd====》绘图


yum -y install  nfs 
vim /etc/exports
/data/web/      192.168.50.58/24(rw,sync)
/data/web/      192.168.50.59/24(rw,sync)
/data/web/      192.168.50.60/24(r,sync)
/data/web/      192.168.50.61/24(r,sync)
systemctl rstart nfs.service
systemctl enable nfs.service  
yum install nfs-utils  -y
showmount -e  192.168.50.62
mkdir -p /data/web/
mount -t nfs 192.168.50.62:/data/web/  /data/web
 echo "<h1>DynamicServer is Health.</h1> "  > /data/web/www/health.php
 echo "<h1>StaticServer  is Health.</h1> "   >  /data/web/www/health.html
Memcached 安装

下载
cd /tmp
wget http://downloads.sourceforge.net/levent/libevent-2.0.22-stable.tar.gz
wget http://www.memcached.org/files/memcached-1.4.33.tar.gz

tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure  -prefix=/usr/local/
make && make install
ls -al /usr/local/lib | grep libevent-   


cd /tmp
tar zxvf memcached-1.4.33.tar.gzsgh5+9
cd memcached-1.4.33
./configure  --with-libevent=/usr/local/ --enable-64bit --enable-threads
make && make install
ls -al /usr/local/bin/memcached  0sac

groupadd memcached
useradd -g memcached memcached

cd /var/run
mkdir memcached
chown -R memcached.memcached memcached

cp /tmp/memcached-1.4.33/scripts/memcached.sysv /etc/init.d/memcached
chown memcached.memcached /etc/init.d/memcached
chmod 755 /etc/init.d/memcached

vim /etc/init.d/memcached
PORT=11211

USER=memcached

MAXCONN=1024

CACHESIZE=128

OPTIONS=””

chown $USER /usr/local/bin/memcached

/usr/local/bin/memcached –d –p $PORT –u $USER –m $CACHESIZE –c $MAXCONN –P /var/run/memcached/memcached.pid $OPTIONS


启动
chkconfig --add memcached
chkconfig --level 235 memcached on
chkconfig --list memcached

service memcached start
ps -ef | grep memcached
service memcached stop
magent安装
cd /usr/local   
mkdir magent   
cd magent
tar zxvf magent-0.5.tar.gz
vim ketama.h          #在开头添加这三行
#ifndef SSIZE_MAX   
#define SSIZE_MAX 32767   
#endif
ln -s /usr/lib64/libm.so /usr/lib64/libm.a
/sbin/ldconfig
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile   
vi Makefile
CFLAGS = -lrt -Wall -O2 -g  #修改这行
make

keepalived设置

主memcache:    开机不启动keepalved  memcached 
vim  /etc/keepalived/keepalived.conf
! Configuration File for keepalived   
global_defs {
 router_id  mem1
}
vrrp_script chk_magent {
  script "/opt/magent.sh"
  interval 2
}
vrrp_script chk_memcached {
   script "killall -0 memcached"
   interval 1
   weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id  101
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.50.199/24
    }
    track_script {
        chk_magent
            chk_memcached
        }
   notify_master  "/etc/keepalived/changemail.py master"
   notify_backup   "/etc/keepalived/changemail.py backup"
   notify_fault    "/etc/keepalived/changemail.py fault"
}
vim  /opt/magent.sh
#!/bin/bash
k=`ps -ef |grep keepalived |grep -v grep |wc -l`
if [ $k -gt 0 ];then
    /usr/local/magent/magent  -u root -n 51200 -l 192.168.50.199 -p 12000 -s 192.168.50.63:11211 -b 192.168.50.64:11211
else
pkill -9 magent
fi

备用memcache:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived   
global_defs {
 router_id  mem2
}
vrrp_script chk_magent {
        script "/opt/magent.sh"
        interval 2
}
vrrp_script chk_memcached {
   script "killall -0 memcached"
   interval 1
   weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id  101
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.50.199/24
    }
    track_script {
       chk_magent
          chk_memcached
        }
   notify_master  "/etc/keepalived/changemail.py master"
   notify_backup   "/etc/keepalived/changemail.py backup"
   notify_fault    "/etc/keepalived/changemail.py fault"
}
vim /opt/magent.sh
#!/bin/bash
k=`ip a |grep 192.168.50.199 |wc -l`
if [ $k -gt 0 ];then
        /usr/local/magent/magent -u root -n 51200 -l 192.168.50.199 -p 12000 -s 192.168.50.53:11211 -b 192.168.50.64:11211
else
pkill -9 magent
fi

注意:如果将故障的memcached节点修复后,缓存是不会再通过到已修复的节点上的,如果是magent指定的主节点故障,那么主节点的缓存数据会丢失,修复后不能立刻重启memcached服务,如果重启,客户端会去查询主节点的数据。并发高的网站会拖死数据库。因此,建议一般业务低峰期的时候再启动memcached主节点服务。然后通过magent再指定主缓存节点和备缓存节点。

1.若memcached读写的数据多,一定将关掉防火墙或将防火墙设置为不使用跟踪链接表,否则会导致/var/log/message报错“kernel: nf_conntrack: table full, dropping packet”,详情请参考本博客中的博文http://blog.csdn.net/yanggd1987/article/details/45886913。
2.使用service iptables stop关掉,并且不能用iptables -L -vnx查看,因此使用此命令会将防火墙开启,虽然规则是清空的,但仍会记录连接跟踪表。

https://my.oschina.net/kisops/blog/150995
安装php扩展
cd /tmp
tar zxvf /software/memcache-3.0.8.tgz -C /tmp
cd memcache-3.0.8
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make
make install
vim /usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"
extension=memcache.so
[Memcache]
memcache.allow_failover = 1
memcache.max_failover_attempts=20
memcache.chunk_size =8192
发布discuz
unzip /software/Discuz_X3.2_SC_UTF8.zip -d /discuz/
chmod -R 777 /discuz/
#开启memcache支持
vim /discuz/upload/config/config_global.php
$_config[‘memory‘][‘memcache‘][‘server‘] = ‘192.168.50.199‘;  #magent代理服务器地址
$_config[‘memory‘][‘memcache‘][‘port‘] = 12000;
$_config[‘memory‘][‘memcache‘][‘pconnect‘] = 1;
$_config[‘memory‘][‘memcache‘][‘timeout‘] = 1;

yum install -y telnet
telnet 192.168.50.199  12000
stats
STAT cmd_get 199
STAT cmd_set 37
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 167
STAT get_misses 32


本文出自 “何全” 博客,请务必保留此出处http://hequan.blog.51cto.com/5701886/1886309

集群三 php+memcached缓存服务器