LAMP的负载均衡和DNS的主从负载

前端两台DNS做主从复制及两台apache做负载均衡,实现LAMP的一台NFS+MySQL负载均衡访问,安装discuz软件。

httpd-2.4.10.tar.bz2
openlogic-mysql-5.6.10-linux-ia32-bin-glibc2.5-1.zip
php-5.4.13.tar.bz2
Discuz_X3.2_SC_UTF8.zip

主机A IP:192.168.100.100 搭建DNS服务器
主机B IP:192.168.100.101 搭建DNS服务器
主机C IP:192.168.100.102 安装apache和php
主机D IP:192.168.100.103 安装apache和php
主机E IP:192.168.100.104NFS服务器,MySQL服务器
域名:XUANZI.COM

实施规划:
主机E搭建的NFS服务器共享/shared目录,主机C和主机D将主机A共享的/shared目录挂载至本地的/www/xuanzi目录,当做Apache的DocumenRoot。当通过主机C的IP和主机D的IP以及其域名来访问页面时,页面内容要求一样。
在搭建DNS服务器时,主机名www.xuanzi.com有两个A记录,分别指向主机C的IP和主机D的IP。这样在浏览器输入www.xuanzi.com访问页面时,轮流解析到主机C和主机D的IP,这样便能达到负载均衡的效果。

主机A
1,DNS搭建
# yum -y install bind bind-utils

配置/etc/named.conf 文件


//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

options {
//      listen-on port 53 { 127.0.0.1; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };
        recursion yes;

//      dnssec-enable yes;
//      dnssec-validation yes;

        /* Path to ISC DLV key */
//      bindkeys-file "/etc/named.iscdlv.key";

//      managed-keys-directory "/var/named/dynamic";
};

logging {
        channel default_debug {
                file "data/named.run";
"/etc/named.conf" 43L, 988C                                        26,1          Top

"/etc/named.rfc1912.zones"


        allow-update { none; };
};

zone "localhost" IN {
        type master;
        file "named.localhost";
        allow-update { none; };
};

zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
        type master;
        file "named.loopback";
        allow-update { none; };
};

zone "1.0.0.127.in-addr.arpa" IN {
        type master;
        file "named.loopback";
        allow-update { none; };
};

zone "0.in-addr.arpa" IN {
        type master;
        file "named.empty";
        allow-update { none; };
};
zone "xuanzi.com" IN {
        type master;
        file "xuanzi.com.zone";
};
zone "100.168.192.in-addr.arpa" IN {
"/etc/named.rfc1912.zones" 49L, 1075C                 

   

区域文件

$TTL 86400
$ORIGIN xuanzi.com.
@       IN      SOA     ns1.xuanzi.com. admin.xuanzi.com (
                        2017081402
                        1H
                        5M
                        7D
                        1D )
        IN      NS      ns1
        IN      NS      ns2
        IN      MX 10   mx1
ns1     IN      A       192.168.100.100
ns2     IN      A       192.168.100.101
mx1     IN      A       192.168.100.106
www     IN      A       192.168.100.102
www     IN      A       192.168.100.103

pop3    IN      CNAME   mx1
                                                  

从DNS服务器配置
"/etc/named.root.key"

//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

options {
//      listen-on port 53 { 127.0.0.1; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };
        recursion yes;

//      dnssec-enable yes;
//      dnssec-validation yes;

        /* Path to ISC DLV key */
//      bindkeys-file "/etc/named.iscdlv.key";

//      managed-keys-directory "/var/named/dynamic";
};

logging {
        channel default_debug {
                file "data/named.run";
"/etc/named.conf" 43L, 988C                                        26,1          Top

反向区域文件


$TTL 86400
$ORIGIN 100.168.192.in-addr.arpa.
@       IN      SOA     ns1.xuanzi.com. admin.xuanzi.com (
                        2017081402
                        1H
                        5M
                        7D
                        1D )
        IN      NS      ns1.xuanzi.com.
        IN      NS      ns2.xuanzi.com.
100     IN      PTR     ns1.xuanzi.com.
101     IN      PTR     ns2.xuanzi.com.
102     IN      PTR     www.xuanzi.com.
103     IN      PTR     www.xuanzi.com.
106     IN      PTR     mx1.xuanzi.com.
imap    IN      PTR     imap.xuanzi.com.

配置/etc/named.rfc1912.zones文件


        file "named.localhost";
        allow-update { none; };
};

zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
        type master;
        file "named.loopback";
        allow-update { none; };
};

zone "1.0.0.127.in-addr.arpa" IN {
        type master;
        file "named.loopback";
        allow-update { none; };
};

zone "0.in-addr.arpa" IN {
        type master;
        file "named.empty";
        allow-update { none; };
};
zone "xuanzi.com" IN {
        type slave;
        masters { 192.168.100.100; };
        file "slaves/xuanzi.com.zone";
};
zone "100.168.192.in-addr.arpa" IN {
        type slave;
        masters { 192.168.100.100; };
        file "slaves/192.168.100.zone";
};
"/etc/named.rfc1912.zones" 51L, 1149C                              51,1          Bot

2,MySQL搭建
yum install mysql-server

# mysql
mysql> UPDATE user SET password=PASSWORD('xuanzi') WHERE USER='xuanzi';
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'192.168.100.%' IDENTIFIED BY 'xuanzi';
mysql> FLUSH PRIVILEGES;

3,NFS配置

# mkdir /shared
# groupadd -g 500 xuanzi
# useradd -g 500 -u 500 xuanzi
# vim /etc/exportfs
    /shared 192.168.100.0/24(rw,all_squash,anonuid=500,anongid=500)
# setfacl -m u:xuanzi:rwx /shared

主机C
主机C挂载NFS共享的/shared目录,执行以下命令:

# mkdir /www/xuanzi
# mount -t nfs 192.168.1.2:/shared  /www/xuanzi

安装Apache
安装apr

    # cd /usr/local
    # tar xf apr-1.5.1.tar.bz2
    # cd apr-1.5.1
    # ./configure --prefix=/usr/local/apr
    # make
    # make install

安装apr-util

 # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    # make 
    # make install

安装httpd

# tar xf httpd-2.4.10.tar.bz2     
 # cd httpd-2.4.10   
 # ./configure 
        --prefix=/usr/local/apache 
        --sysconfdir=/etc/httpd 
        --enable-so 
        --enable-rewrite 
        --enable-ssl     
        --enable-cgi 
        --enable-cgid 
        --enable-modules=most
        --enable-mods-shared=most 
        --enable-mpms-shared=all 
        --with-mpm=event 
        --with-apr=/usr/local/apr 
        --with-apr-util=/usr/local/apr-util
出现错误:
configure: error: pcre-config for libpcre not found. PCRE is required anavailabl from 
解决办法
# yum install -y pcre-devel    
# make    
# make install

提供SysV风格的启动脚本:/etc/init.d/httpd

#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#       HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}
# See how we were called.
case "$1" in
  start)
start
;;
  stop)
stop
;;
  status)
        status -p ${pidfile} $httpd
RETVAL=$?
;;
  restart)
stop
start
;;
  condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
  reload)
        reload
;;
  graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
  *)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL

chmod +x /etc/init.d/httpd

chkconfig --add httpd

编辑/etc/profile.d/httpd.sh   

1

PATH=$PATH:
/usr/local/apache/bin

# source /etc/profile.d/httpd.sh
#  service httpd start

安装php

# cd php-5.4.13
    # ./configure 
        --prefix=/usr/local/php 
        --with-mysql=mysqlnd
        --with-mysqli=mysqlnd
        --with-pdo-mysql=mysqlnd
        --with-openssl
        --enable-mbstring 
        --with-freetype-dir 
        --with-jpeg-dir 
        --with-png-dir 
        --with-zlib 
        --with-libxml-dir=/usr 
        --enable-xml 
        --enable-sockets 
        --with-apxs2=/usr/local/apache/bin/apxs 
        --with-mcrypt --with-config-file-path=/etc 
        --with-config-file-scan-dir=/etc/php.d 
        --with-bz2 --enable-maintainer-zts
    出现错误:
    configure: error: mcrypt.h not found. Please reinstall libmcrypt.
    解决办法:
        #yum -y install libmcrypt-devel mhash-devel
    # make
    # make install

提供php配置文件
cp php.ini-production /etc/php.ini

/etc/httpd/httpd.conf配置文件

AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    DirectoryIndex index.php index.html
    注释掉
    #/DocumentRoot "/usr/local/apache/htdocs"
    取消注释
    Include /etc/httpd/extra/httpd-vhosts.conf

主机C和主机D /etc/httpd/extra/httpd-vhosts.conf配置


    DocumentRoot "/www/xuanzi"
    
        Options none
        AllowOverride none
        Require all granted
    
    ServerName www.xuanzi.com
    ErrorLog "/var/log/httpd/xuanzi.com_error.log"
    CustomLog "/var/log/httpd/xuanzi.com_access.log" combined

主机D
主机D和主机C配置相同,参考以上文档

*主机E **
Discuz论坛测试
# unzip Discuz_X3.2_SC_UTF8.zip
# cd upload/
# cp -R ./
/shared/

浏览器输入www.xuanzi.com即可配置安装Discuz了,要注意的是安装Discuz论坛时填入mysql服务器IP地址时,要填入192.168.1.104不能填入localhost。并且输入主机C的IP和输入主机D的IP访问的内容是相同的,比如主机C上发帖,用主机D的IP访问可以看到。

你可能感兴趣的:(LAMP的负载均衡和DNS的主从负载)