re2d2 发表于 2015-4-13 08:37:28

两台LAP和一台NFS+MySQL+DNS实现LAMP负载均衡访问

项目环境:RHEL6.6-i386
源:remi,epel,本地源
各软件版本:
    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.1.2   搭建DNS服务器,NFS服务器,MySQL服务器
主机B IP:192.168.1.3    安装apache和php
主机C IP:192.168.1.4    安装apache和php
域名:mingxiao.info

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

主机A
    1,DNS搭建

    # yum -y install bind bind-utils


配置/etc/named.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// 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 {
      directory       "/var/named";
      recursion yes;

};


zone "." IN {
      type hint;
      file "named.ca";
};

logging {
      channel query_log {
                file "/var/log/named/bind_query.log" versions 3;
                severity dynamic;
                print-category yes;
                print-time yes;
                print-severity yes;
      };
      category queries { query_log; };
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";





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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// named.rfc1912.zones:
//
// Provided by Red Hat caching-nameserver package
//
// ISC BIND named zone configuration for zones recommended by
// RFC 1912 section 4.1 : localhost TLDs and address zones
// and http://www.ietf.org/internet-dra ... -local-zones-02.txt
// (c)2007 R W Franks
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

zone "localhost.localdomain" IN {
      type master;
      file "named.localhost";
      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 "mingxiao.info" IN {
      type master;
      file "mingxiao.info.zone";
};

zone "1.168.192.in-addr.arpa" IN {
      type master;
      file "192.168.1.zone";
};





# cd /var/named
配置mingxiao.info.zone

1
2
3
4
5
6
7
8
9
10
11
$TTL 86400
@ IN SOA ns.mingxiao.info admin.mingxiao.info. (
      2015041211
      2H
      10M
      3D
      1D )
      IN NS ns
ns IN A 192.168.1.2
www IN A 192.168.1.4
www IN A 192.168.1.3




配置192.168.1.zone

1
2
3
4
5
6
7
8
9
10
11
$TTL 86400
@ IN SOA ns.mingxiao.info admin.mingxiao.info. (
      2015041211
      2H
      10M
      3D
      1D )
      IN NS ns.mingxiao.info.
1   IN PTR ns.mingxiao.info.
3   IN PTR www.mingxiao.info.
4   IN PTR www.mingxiao.info.




# chown root:named mingxiao.info.zone 192.168.1.zone
# chmod 640 mingxiao.info.zone 192.168.1.zone

主机A,主机B,主机C中/etc/resolv.conf配置,DNS都指向A主机

1
2
; generated by /sbin/dhclient-script
nameserver 192.168.1.2





#service named start

请确保主机A,主机B,主机C中DNS解析正常,三台主机解析结果如下:
# dig -t A www.mingxiao.info

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -t A www.mingxiao.info
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24914
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;www.mingxiao.info.INA
;; ANSWER SECTION:
www.mingxiao.info.86400INA192.168.1.3
www.mingxiao.info.86400INA192.168.1.4
;; AUTHORITY SECTION:
mingxiao.info.86400INNSns.mingxiao.info.
;; ADDITIONAL SECTION:
ns.mingxiao.info.86400INA192.168.1.2
;; Query time: 0 msec
;; SERVER: 192.168.1.2#53(192.168.1.2)
;; WHEN: Sun Apr 12 23:04:25 2015
;; MSG SIZErcvd: 100





2,MySQL搭建

    这里采用二进制免编译包

此处用的mysql软件包是二进制免编译包。   # cd /usr/local/
   # ln -s/usr/local/mysql-5.6.10-linux-glibc2.5-i686/usr/local/mysql   # cd /usr/local/mysql   # groupadd -r -g 306 mysql         
   # useradd -g 306 -r -u 306 mysql
   # chown -R mysql.mysql /usr/local/mysql/*
新建一个逻辑卷,挂载至/mydata下,后在mydata目录下创建data目录,用于存放mysql数据。
磁盘分区略去,分区后如下:
1
2
3
# fdisk /dev/sdb   
   Device Boot      Start         End      Blocks   IdSystem
   /dev/sdb1            1      2610    20964793+8eLinux LVM




逻辑卷创建:
1
2
3
4
5
# partprobe /dev/sdb1
   # pvcreate /dev/sdb1
   # vgcreate myvg /dev/sdb1
   # lvcreate -n mydata -L 5G myvg
   # mkfs.ext4/dev/myvg/mydata





挂载
1
2
3
# mkdir /mydata
   # echo "/dev/myvg/mydata/mydata ext3 defaults 0 0" >> /etc/fstab
   # mount -a





创建MySQL数据目录
1
2
# mkdir /mydata/data
   # chown -R mysql.mysql /mydata/data





MySQL初始化:
1
2
3
   # cd /usr/local/mysql
   # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
   # chown -R root /usr/local/mysql/*





提供SysV风格服务脚本
1
   # cp support-files/mysql.server /etc/init.d/mysqld





提供MySQL配置文件   # cp my.cnf /etc/my.cnf   # vim /etc/my.cnf
1
2
3
4
basedir = /usr/local/mysql
    datadir = /mydata/data
    port = 3306
    sock=/tmp/mysql.sock




编辑/etc/profile.d/mysql.sh
1
export PATH=$PATH:/usr/local/mysql/bin





编辑/etc/ld.so.conf.d/mysql.conf
1
    /usr/local/mysql/lib




su
man帮助文档,编辑/etc/man.config      
1
    MANPATH /usr/local/mysql/man





头文件
1
ln -sv /usr/local/mysql/include/ /usr/include/mysql





# service mysqls start# chkconfig --add mysqld

1
2
3
4
# mysql
mysql> UPDATE user SET password=PASSWORD('xiaoming') WHERE USER='root';
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'192.168.1.%' IDENTIFIED BY 'xiaoming';
mysql> FLUSH PRIVILEGES;





3,NFS配置

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


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

1
2
    # mkdir /www/xiaoming
    # mount -t nfs 192.168.1.2:/shared/www/xiaoming





安装Apache


1,安装apr

1
2
3
4
5
6
    # cd /usr/local
    # tar xf apr-1.5.1.tar.bz2
    # cd apr-1.5.1
    # ./configure --prefix=/usr/local/apr
    # make
    # make install





2,安装apr-util

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





3,安装httpd
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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





vim /etc/httpd/httpd.conf
      PidFile "/var/run/httpd.pid"
提供SysV风格的启动脚本:/etc/init.d/httpd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#
# 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    # 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配置文件
1
cp php.ini-production /etc/php.ini





/etc/httpd/httpd.conf配置文件   
1
2
3
4
5
6
7
    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





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

1
2
3
4
5
6
7
8
9
10
11

    DocumentRoot "/www/mingxiao"
   
      Options none
      AllowOverride none
      Require all granted
   
    ServerName www.mingxiao.info
    ErrorLog "/var/log/httpd/mingxiao.info_error.log"
    CustomLog "/var/log/httpd/mingxiao.info_access.log" combined






# mkdir /var/log/httpd

# service httpd restart


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

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

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





页: [1]
查看完整版本: 两台LAP和一台NFS+MySQL+DNS实现LAMP负载均衡访问