LAMP的負(fù)載均衡和DNS的主從負(fù)載

前端兩臺DNS做主從復(fù)制及兩臺apache做負(fù)載均衡,實現(xiàn)LAMP的一臺NFS+MySQL負(fù)載均衡訪問,安裝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服務(wù)器
主機B IP:192.168.100.101 搭建DNS服務(wù)器
主機C IP:192.168.100.102 安裝apache和php
主機D IP:192.168.100.103 安裝apache和php
主機E IP:192.168.100.104NFS服務(wù)器,MySQL服務(wù)器
域名:XUANZI.COM

實施規(guī)劃:
主機E搭建的NFS服務(wù)器共享/shared目錄,主機C和主機D將主機A共享的/shared目錄掛載至本地的/www/xuanzi目錄,當(dāng)做Apache的DocumenRoot。當(dāng)通過主機C的IP和主機D的IP以及其域名來訪問頁面時,頁面內(nèi)容要求一樣。
在搭建DNS服務(wù)器時,主機名www.xuanzi.com有兩個A記錄,分別指向主機C的IP和主機D的IP。這樣在瀏覽器輸入www.xuanzi.com訪問頁面時,輪流解析到主機C和主機D的IP,這樣便能達(dá)到負(fù)載均衡的效果。

主機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                 

   

區(qū)域文件

$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服務(wù)器配置
"/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

反向區(qū)域文件


$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目錄,執(zhí)行以下命令:

# 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
出現(xiàn)錯誤:
configure: error: pcre-config for libpcre not found. PCRE is required anavailabl from 
解決辦法
# yum install -y pcre-devel    
# make    
# make install

提供SysV風(fēng)格的啟動腳本:/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
    出現(xiàn)錯誤:
    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配置

<VirtualHost *:80>
    DocumentRoot "/www/xuanzi"
    <Directory "/www/xuanzi">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
    ServerName www.xuanzi.com
    ErrorLog "/var/log/httpd/xuanzi.com_error.log"
    CustomLog "/var/log/httpd/xuanzi.com_access.log" combined
</VirtualHost>

主機D
主機D和主機C配置相同,參考以上文檔

*主機E **
Discuz論壇測試
# unzip Discuz_X3.2_SC_UTF8.zip
# cd upload/
# cp -R ./
/shared/

瀏覽器輸入www.xuanzi.com即可配置安裝Discuz了,要注意的是安裝Discuz論壇時填入mysql服務(wù)器IP地址時,要填入192.168.1.104不能填入localhost。并且輸入主機C的IP和輸入主機D的IP訪問的內(nèi)容是相同的,比如主機C上發(fā)帖,用主機D的IP訪問可以看到。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容