案例描述与实现。由于公司知名度不断提升,网站的访问量也不断提升开放的对产品和经营的论坛访问量也越来越大,一台WEB服务器已经不能满足需求,需要增加一台web服务器,将两台web服务器做成一个Discuz论坛的小集群,使用简单的DNS轮询功能来实现双web的负载均衡,在web1上搭建DNS服务器,NFS服务器,并部署php与mysql,以及Discuz论谈,web2上只需搭建LAMP环境即可,主要提供mysql数据存储服务。利用web1的NFS共享来实现数据间的同步,此时就可以实现访问其中一台web服务器时都可以看到相同的页面。
案例拓扑结构图
先配置web1.
配置dns实现解析与轮询功能
- # vim /var/named/zzu.com.db
- $TTL 600
- @ IN SOA ns.zzu.com. admin.zzu.com (
- 2012071501
- 30M
- 15M
- 1W
- 1D)
- IN NS 192.168.0.100
- ns IN A 192.168.0.101
- ns IN A 192.168.0.100
- www IN A 192.168.0.100
- www IN A 192.168.0.101
- ~
- # vim /var/named/192.168.0.db
- $TTL 600
- @ IN SOA ns.zzu.com admin.zzu.com (
- 2012071501
- 30M
- 15M
- 1W
- 1D)
- IN ns ns.zzu.com.
- 100 IN PTR ns.zzu.com.
- 102 IN PTR ns.zzu.com.
- 100 IN PTR www.zzu.com.
- 101 IN PTR www.zzu.com.
- [root@localhost named]# vim /etc/resolv.conf #dns指向本服务器
- [root@localhost named]# dig -t A www.zzu.com
- ; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5 <<>> -t A www.zzu.com
- ;; global options: printcmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31504
- ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 0
- ;; QUESTION SECTION:
- ;www.zzu.com. IN A
- ;; ANSWER SECTION:
- www.zzu.com. 600 IN A 192.168.0.101 # 注意,下边就轮询到了0.100
- www.zzu.com. 600 IN A 192.168.0.100
- ;; AUTHORITY SECTION:
- zzu.com. 600 IN NS 192.168.0.100.zzu.com.
- ;; Query time: 5 msec
- ;; SERVER: 192.168.0.100#53(192.168.0.100)
- ;; WHEN: Fri Jul 13 05:37:58 2012
- ;; MSG SIZE rcvd: 89
- [root@localhost named]# dig -t A www.zzu.com
- ; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5 <<>> -t A www.zzu.com
- ;; global options: printcmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22071
- ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 0
- ;; QUESTION SECTION:
- ;www.zzu.com. IN A
- ;; ANSWER SECTION:
- www.zzu.com. 600 IN A 192.168.0.100
- www.zzu.com. 600 IN A 192.168.0.101
- ;; AUTHORITY SECTION:
- zzu.com. 600 IN NS 192.168.0.100.zzu.com.
- ;; Query time: 5 msec
- ;; SERVER: 192.168.0.100#53(192.168.0.100)
- ;; WHEN: Fri Jul 13 05:38:02 2012
- ;; MSG SIZE rcvd: 89
dns配置完毕 编译安装配置apache 编译过程依赖pcre-devel 编译安装前务必要确保该软件包安装上。
- [root@localhost httpd-2.4.2]# vim /etc/httpd/httpd.conf
- 编译安装apr
- # tar xf apr-1.4.6.tar.bz2
- # cd apr-1.4.6
- # ./configure --prefix=/usr/local/apr
- # make &&make install
- 编译安装apr-util
- # tar xf apr-util-1.4.1.tar.bz2
- # cd apr-util-1.4.1
- #./configure --prefix=/usr/local/apr-util --with apr=/usr/local/apr
- # make &&make install
- 编译安装httpd
- # tar xf httpd-2.4.2.tar.bz2
- # cd httpd-2.4.2
- # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi -enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
- # vim /etc/rc.d/init.d/httpd #为httpd服务提供脚本
- #!/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/rc.d/init.d/httpd
- # chkconfig --add httpd
- # chkconfig httpd on
- # service httpd start
- 编译安装mysql,下步为提供php动态页面,需要用到mysql 所以这里这需要安装就可以了不需要启动。
- [root@localhost ~]# tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local
- [root@localhost ~]# cd /usr/local
- [root@localhost local]# ln -s mysql-5.5.24-linux2.6-i686/ mysql
- [root@localhost local]# cd mysql
- [root@localhost mysql]# chown -R mysql:mysql .
- [root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
- Installing MySQL system tables...
- OK
- Filling help tables...
- OK
- To start mysqld at boot time you have to copy
- support-files/mysql.server to the right place for your system
- PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
- To do so, start the server, then issue the following commands:
- ./bin/mysqladmin -u root password 'new-password'
- ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
- Alternatively you can run:
- ./bin/mysql_secure_installation
- which will also give you the option of removing the test
- databases and anonymous user created by default. This is
- strongly recommended for production servers.
- See the manual for more instructions.
- You can start the MySQL daemon with:
- cd . ; ./bin/mysqld_safe &
- You can test the MySQL daemon with mysql-test-run.pl
- cd ./mysql-test ; perl mysql-test-run.pl
- Please report any problems with the ./bin/mysqlbug script!
- [root@localhost mysql]# chown -R root .
- [root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
- [root@localhost mysql]# vim /etc/ld.so.conf
- [root@localhost mysql]# ldconfig
- 到此为止mysql就配置完毕,此时不需要启动服务。
编译安装php
- # tar xf php-5.3.14.tar.bz2
- # cd php-5.3.14
- # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
- # make && make intall
- 为php提供配置文件:
- # cp php.ini-production /etc/php.ini
- 3、 编辑apache配置文件httpd.conf,以apache支持php
- # vim /etc/httpd/httpd.conf
- 1、添加如下二行
- AddType application/x-httpd-php .php
- AddType application/x-httpd-php-source .phps
- 2、定位至DirectoryIndex index.html
- 修改为:
- DirectoryIndex index.php index.html
- 而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。
配置Discuz
- 为Discuz运行创建mysql用户
- # mysql -u root
- mysql> CREATE DATABASE discuz; //创建数据库discuz
- mysql> GRANT ALL ON discuz.* TO [email protected] IDENTIFIED BY "discuz"; //创建mysql用户discuz并给于对discuz数据库所有权,并设置密码为discuz
- mysql> FLUSH PRIILEGES; //刷新授权表
- mysql> quit //退出
- 2、安装Discuz
- # unzip Discuz_7.2_FULL_SC_GBK.zip /root
- # mv /root/upload/* /var/www/Dicuz
详细配置请参考我的推荐博文<快速建站LAMP>,这里就不详细介绍
搭建NFS服务器,这个很简单,只需安装nfs-utils即可,配置也很简单
- # vim /etc/exports
- /var/www/Discuz 192.168.0.101(rw)
- 执行exportfs -a 即可实现共享
web2的配置,在web2只需搭建LNMP环境,但重要的是提供Mysql服务,因为本案例实现的是分离Mysql实现的负载均衡,来自web1,和web2的数据都存储在web2的mysql中,web1中的数据只是提供编译安装php是需要用到mysql。搭建mysql课参考web1中mysql的编译安装。
- [root@localhost ~]# mount -t nfs 192.168.0.100:/var/www/Discuz /Discuz
- 可以编辑/etc/fstab 设置开机自动挂载。
此时一个基于dns轮询的双web服务器集群就搭建完毕。用户访问的时候,不管轮询到哪台服务器都可以访问到相同的数据。此处各个阶段的测试图片都没有给大家展示,希望大家在每个阶段都做好测试工作,随时发现错误并改正,这样才有利于下面的工作顺利进行,更能提高效率。