利用lvs-nat实现两台web服务器负载均衡的简单案例
写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正。如有不明白的地方,愿可一起探讨。
案例拓扑图
配置主机1
安装ipvsadm
# yum -y install ipvsadm # ipvsadm -A -t 10.170.2.80:80 -s rr # ipvsadm -a -t 10.170.2.80:80 -r 192.168.3.101 -m # ipvsadm -a -t 10.170.2.80:80 -r 192.168.3.102 -m # echo 1 > /proc/sys/net/ipv4/ip_forward
配置主语2和3
编译安装apr-1.5.0
# tar xf apr-1.5.0.tar.bz2 # cd apr-1.5.0 # ./configure --prefix=/usr/local/apr # make && make install
编译安装apr-util-1.5.3
# tar xf apr-util-1.5.3.tar.bz2 # cd apr-util-1.5.3 # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ # make && make install
编译安装httpd-2.4.9
# tar xf httpd-2.4.9.tar.bz2 # cd httpd-2.4.9 # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event # make && make install
修改httpd主配置文件,设置Pid文件的路径
# vim /etc/httpd24/httpd.conf
在ServerRoot "/usr/local/apache"行下面添加一行:
PidFile "/var/run/httpd.pid"
提供SysV服务脚本/etc/rc.d/init.d/httpd24,这个文件需要新建
# vim /etc/rc.d/init.d/httpd24
#!/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/httpd24 # chkconfig --add httpd24 # chkconfig httpd24 on
启动http24服务并测试
# service httpd24 start
在主机2和主机3上添加不同的页面
主机2
# vim /usr/local/apache/htdocs/test.html <h1>Hello,WEB1</h1>
主机3
# vim /usr/local/apache/htdocs/test.html <h1>Hello,WEB2</h1>
在浏览器键入10.170.2.80/test.html,并刷新可以得到不同的结果
编译安装php-5.4.26
# tar xf php-5.4.26.tar.bz2 # cd php-5.4.26 # ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --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 # make && make install
为php提供配置文件
# cp php.ini-production /etc/php.ini
编辑httpd配置文件,以支持php
# vim /etc/httpd24/httpd.conf
在AddType application/x-gzip .gz .tgz行下面添加下面两行: AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps 将DirectoryIndex index.html这一行改为: DirectoryIndex index.php index.html
给主机2和主机3添加测试页
主机2
# vim /usr/local/apache/htdocs/index.php <h1>Hello,WEB1</h1> <?php phpinfo(); ?>
主机3
# vim /usr/local/apache/htdocs/index.php <h1>Hello,WEB2</h1> <?php phpinfo(); ?>
重启主机2和主机3的httpd24服务
# service httpd24 restart
在浏览器中键入10.170.2.80,刷新可以得到不同的结果
配置主机4
编译安装mariadb-5.5.36
# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local # cd /usr/local/ # ln -sv mariadb-5.5.36-linux-x86_64/ mysql # mkdir -pv /mysql/data # groupadd -r mysql # useradd -g mysql -s /sbin/nologin -M -d /mysql/data -r mysql # chown -R mysql:mysql /mysql # chown -R mysql:mysql /mysql/data
为数据库提供配置文件:
# cd mysql # mkdir /etc/mysql # chown -R root.mysql ./* # cp support-files/my-large.cnf /etc/mysql/my.cnf 修改文件/etc/mysql/my.cnf文件内容,在thread_concurrency = 8行下添加一行: datadir = /mysql/data
为数据库提供SysV启动脚本,并设置为开机启动:
# cp support-files/mysql.server /etc/init.d/mysqld # chkconfig --add mysqld # chkconfig mysqld on
初始化数据库并启动数据库:
# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh # source /etc/profile.d/mysql.sh # echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf # ldconfig # ln -sv /usr/local/mysql/include/ /usr/include/mysql # scripts/mysql_install_db --user=mysql --datadir=/mysql/data/ # /etc/init.d/mysqld start
创建数据库并授权:
MariaDB [(none)]> CREATE DATABASE discuz; MariaDB [(none)]> GRANT ALL ON discuz.* TO dscuser@‘192.168.3.%‘ IDENTIFIED BY ‘dscpass‘; MariaDB [(none)]> FLUSH PRIVILEGES;
启动nfs共享服务
# useradd -u 600 discuz # vim /etc/exports /var/www/html 192.168.3.0/24(rw,no_root_squash) # /etc/init.d/rpcbind start # /etc/init.d/nfs start # /etc/init.d/nfslock start
将Discuz_X2.5_SC_GBK解压到/var/www/html下并修改权限
# unzip Discuz_X2.5_SC_GBK.zip . # cd upload # chmod -R go+w config # chmod -R go+w data # chmod -R go+w uc_*
搭建测试环境并进行测试
在主机2和主机3上进行相关配置
# useradd -u 600 discuz # mount -f nfs 192.168.3.36:/var/www/html /usr/local/apache/htdocs
在浏览器中键入10.170.2.80/upload,可以得到安装discuz论坛页面
点击“我同意”-->点击“下一步”-->点击“下一步”-->根据数据库授权信息,填写“安装数据库内容”,然后点击“下一步”-->数据库安装成功后,在显示的页面上点击“暂不开通”即可得到论坛页面
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。