1、安装tomcat
yum install -y java-1.7.0-openjdk tar zxf apache-tomcat-7.0.65.tar.gz -C /usr/local/ cd /usr/local/ ln -s apache-tomcat-7.0.65/ tomcat vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$PATH:$CATALINA_HOME/bin . /etc/profile.d/tomcat.sh vim /etc/init.d/tomcat #!/bin/sh # Tomcat init script for Linux. # # chkconfig: 2345 96 14 # description: The Apache Tomcat servlet/JSP container. JAVA_OPTS='-Xms64m -Xmx128m' JAVA_HOME=/usr CATALINA_HOME=/usr/local/tomcat export JAVA_HOME CATALINA_HOME exec $CATALINA_HOME/bin/catalina.sh $* chmod +x /etc/init.d/tomcat chkconfig --add tomcat service tomcat start
2、安装Apache
yum -y install gcc gcc-c++ openssl-devel pcre-devel tar xf apr-1.4.6.tar.bz2 cd apr-1.4.6 ./configure --prefix=/usr/local/apr --disable-ipv6 make && make install 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 tar xf httpd-2.2.25.tar.bz2 cd httpd-2.2.25 ./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 \ --enable-mpms-shared=all \ --with-mpm=event \ --enable-proxy \ --enable-proxy-http \ --enable-proxy-ajp \ --enable-proxy-balancer \ --enable-lbmethod-heartbeat \ --enable-heartbeat \ --enable-slotmem-shm \ --enable-slotmem-plain \ --enable-watchdog make && make install cp build/rpm/httpd.init /etc/init.d/httpd vim /etc/init.d/httpd httpd=${HTTPD-/usr/local/apache/bin/httpd} pidfile=${PIDFILE-/usr/local/apache/logs/${prog}.pid} lockfile=${LOCKFILE-/var/lock/subsys/${prog}} RETVAL=0 # check for 1.3 configuration check13 () { CONFFILE=/etc/httpd/httpd.conf echo "PATH=/usr/local/apache/bin:$PATH" >> /etc/profile.d/http.sh . /etc/profile.d/http.sh ln -s /usr/local/apache/include/ /usr/include/httpd vim /etc/httpd/httpd.conf LoadModule slotmem_shm_module modules/mod_slotmem_shm.so ServerName localhost:80 chkconfig --add httpd service httpd start
3、配置Apache使用mod_jk模块实现代理及负载均衡
######修改Apache主配置文件,包含一个文件并创建该文件;实现代理功能
将下面指令添加到文件末尾即可
# vim /etc/httpd/httpd.conf Include /etc/httpd/extra/mod_jk.conf
------------------------------------------------------------------------
创建文件添加如下内容
# vim /etc/httpd/extra/mod_jk.conf LoadModule jk_module modules/mod_jk.so JkWorkersFile /etc/httpd/extra/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* TomcatA JkMount /status/ stat1
------------------------------------------------------------------------
# vim /etc/httpd/extra/workers.properties worker.list=TomcatA,stat1 worker.TomcatA.port=8009 worker.TomcatA.host=172.16.14.3 worker.TomcatA.type=ajp13 worker.TomcatA.lbfactor=1 worker.stat1.type = status
----------------------------------------------------------------------
# service httpd restart ######修改后端Tomcat1服务器,添加一个虚拟主机并提供测试页面 [root@Tomcat1 ~]# cd /usr/local/tomcat/conf/ [root@Tomcat1 conf]# cp server.xml server.xml.bak [root@Tomcat1 conf]# vim server.xml ######修改如下内容 <Engine name="Catalina" defaultHost="www.jsprun.com" jvmRoute="TomcatA"> ######在"Engine"中添加如下内容 <Host name="www.jsprun.com" appBase="/jsprun" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="jsprun_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/jsprun" /> </Host> ---------------------------------------------------------------------- ######创建网站存放目录并创建测试页 [root@Tomcat1 ~]# mkdir /jsprun [root@Tomcat1 ~]# vim /jsprun/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA </font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("abc","abc"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> ---------------------------------------------------------------------- ######修改Apache配置文件(mod_jk.conf、workers.properties);实现负载均衡 # vim /etc/httpd/extra/mod_jk.conf LoadModule jk_module modules/mod_jk.so JkWorkersFile /etc/httpd/extra/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* lbcluster JkMount /status/ stat1 ------------------------------------------------------------------------ # vim /etc/httpd/extra/workers.properties worker.list=lbcluster,stat1 worker.TomcatA.port=8009 worker.TomcatA.host=172.16.14.3 worker.TomcatA.type=ajp13 worker.TomcatA.lbfactor=1 worker.TomcatB.port = 8009 worker.TomcatB.host=172.16.14.4 worker.TomcatB.type = ajp13 worker.TomcatB.lbfactor = 1 worker.lbcluster.type = lb worker.lbcluster.sticky_session = 0 #取值为{1|0}1将用户session与后端服务器绑定,0 为不绑定,如果支持session复制或session共享可以设置为0 worker.lbcluster.balance_workers = TomcatA, TomcatB worker.stat1.type = status ---------------------------------------------------------------------- # service httpd reload ######修改后端Tomcat2服务器,添加一个虚拟主机并提供测试页面 [root@Tomcat2 ~]# cd /usr/local/tomcat/conf/ [root@Tomcat2 conf]# cp server.xml server.xml.bak [root@Tomcat2 conf]# vim server.xml ######修改如下内容 <Engine name="Catalina" defaultHost="www.jsprun.com" jvmRoute="TomcatB"> ######在"Engine"中添加如下内容 <Host name="www.jsprun.com" appBase="/jsprun" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="jsprun_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/jsprun" /> </Host> ---------------------------------------------------------------------- ######创建网站存放目录并创建测试页 [root@Tomcat2 ~]# mkdir /jsprun [root@Tomcat2 ~]# vim /jsprun/index.jsp <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB </font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("abc","abc"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> 4、配置Apache基于mod_proxy模块实现代理及负载均衡 ######修改Apache的主配置文件,包含一个文件并创建该文件;实现代理功能 # vim /etc/httpd/httpd.conf #Include /etc/httpd/extra/mod_jk.conf #注释此行 Include /etc/httpd/extra/mod_proxy.conf ------------------------------------------------------------------------ # vim /etc/httpd/extra/mod_proxy.conf ProxyVia On ProxyRequests Off ProxyPreserveHost Off <Proxy *> Order allow,deny Allow from all </Proxy> ProxyPass / ajp://172.16.14.3:8009/ ProxyPassReverse / ajp://172.16.14.3:8009/ <Location / > Order allow,deny Allow from all </Location> ---------------------------------------------------------------------- # service httpd reload ===================================================== ================= 注释: ProxyPass / ajp://172.16.14.3:8009/ #使用的是ajp协议 ProxyPassReverse / ajp://172.16.14.3:8009/ 可以更改为使用http协议;如下 ProxyPass / http://172.16.14.3:8080/ ProxyPassReverse / http://172.16.14.3:8080/ ######这里只演示使用ajp协议,如果是Apache与Tomcat结合建议使用ajp协议 ######修改Apache配置文件(mod_proxy.conf);实现负载均衡 # vim /etc/httpd/extra/mod_proxy.conf ProxyVia Off ProxyRequests Off ProxyPreserveHost Off <Proxy balancer://allen> BalancerMember ajp://172.16.14.3:8009 loadfactor=1 BalancerMember ajp://172.16.14.4:8009 loadfactor=1 ProxySet lbmethod=bytraffic </Proxy> <Location /allen> #设置状态及管理页面 SetHandler balancer-manager Proxypass ! Order allow,deny Allow from all </Location> <Proxy *> Order allow,deny Allow from all </Proxy> ProxyPass / balancer://allen/ stickysession=JSESSIONID ProxyPassReverse / balancer://allen/ <Location / > Order allow,deny Allow from all </Location> ---------------------------------------------------------------------- 注释: BalancerMember ajp://172.16.14.3:8009 loadfactor=1 #使用ajp协议 BalancerMember ajp://172.16.14.4:8009 loadfactor=1 可更改为使用http协议 BalancerMember http://172.16.14.3:8080 loadfactor=1 BalancerMember http://172.16.14.4:8080 loadfactor=1 ######这里只介绍使用ajp协议,如果有兴趣可以更改一下;loadfactor:权重 ---------------------------------------------------------------------- ProxySet lbmethod=bytraffic #设置调度算法 byrequests:即基于权重将统计请求个数进行调度(默认) bytraffic:则执行基于权重的流量计数调度 bybusyness:通过考量每个后端服务器的当前负载进行调度 ---------------------------------------------------------------------- # service httpd reload 5、论坛安装 ######在Tomcat1服务器上安装 # unzip JspRun\!_6.0.0_GBK.zip # mv /web/allen/index.jsp /web/allen/test.jsp # cp -r upload/* /web/allen/ ------------------------------------------------------------------------ 修改论坛数据库连接文件 # vim /web/allen/config.properties dbhost = 172.16.14.5 dbport=3306 dbuser = bbsuser dbpw = bbspass dbname = bbs ------------------------------------------------------------------------ # catalina.sh stop # catalina.sh start ######安装论坛程序 http://172.16.14.1/install.jsp ######将论坛程序拷贝到Tomcat2服务器上一份并访问测试 # mv /web/allen/index.jsp /web/allen/test.jsp #把原来的测试页重命名 ------------------------------------------------------------------------ # scp -r 172.16.14.3:/web/allen/* /web/allen/ # catalina.sh stop # catalina.sh start 6、安装并配置Keepalived ######将Apache1服务器的Httpd配置文件复制到Apache2服务器 [root@Apache1 ~]# cd /etc/httpd/ [root@Apache1 httpd]# scp httpd.conf 172.16.14.2:/etc/httpd/ [root@Apache1 httpd]# cd extra/ [root@Apache1 extra]# scp mod_jk.conf mod_proxy.conf workers.properties 172.16.14.2:/etc/httpd/extra/ ######在Apache1与Apache2服务器上安装Keepalived;这里使用rpm包安装,光 盘映像中有 [root@Apache1 ~]# yum -y install keepalived -------------------------------------------- [root@Apache2 ~]# yum -y install keepalived ######配置Apache1服务器上的Keepalived [root@Apache1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from [email protected] smtp_server 172.16.0.0 smtp_connect_timeout 30 router_id LVS_ALLEN } vrrp_script chk_httpd { script "killall -0 httpd" interval 1 weight -2 } vrrp_instance httpd_1 { state MASTER interface eth0 virtual_router_id 58 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1058 } virtual_ipaddress { 172.16.14.10 } track_script { chk_httpd } } [root@Apache1 ~]# service keepalived start [root@Apache1 ~]# chkconfig keepalived on [root@Apache1 ~]# chkconfig --list keepalived keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@Apache1 ~]# ip addr show eth0 ######配置Apache2服务器上的Keepalived [root@Apache2 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from [email protected] smtp_server 172.16.0.0 smtp_connect_timeout 30 router_id LVS_ALLEN } vrrp_script chk_httpd { script "killall -0 httpd" interval 1 weight -2 } vrrp_instance httpd_1 { state BACKUP interface eth0 virtual_router_id 58 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1058 } virtual_ipaddress { 172.16.14.10 } track_script { chk_httpd } } [root@Apache2 ~]# service keepalived start [root@Apache2 ~]# chkconfig --list keepalived keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off ######模拟前端一台服务器出现故障;查看虚拟IP是否转移 ######停止Apache1服务器上的Httpd服务来模拟故障;并查看IP [root@Apache1 ~]# service httpd stop [root@Apache1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000
link/ether 00:0c:29:2c:1a:24 brd ff:ff:ff:ff:ff:ff
inet 172.16.14.1/16 brd 172.16.255.255 scope global eth0
inet6 fe80::20c:29ff:fe2c:1a24/64 scope link
valid_lft forever preferred_lft forever
=====================================================
===================
######查看Apache2服务器上的IP
[root@Apache2 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000
link/ether 00:0c:29:ec:f6:3f brd ff:ff:ff:ff:ff:ff
inet 172.16.14.2/16 brd 172.16.255.255 scope global eth0
inet 172.16.14.10/32 scope global eth0
inet6 fe80::20c:29ff:feec:f63f/64 scope link
valid_lft forever preferred_lft forever
######从上一步可以看出,虚拟IP地址已成功转移,说明还能正常提供服务,可以
再次访问测试;如果服务器修复好重新上线,虚拟IP还会转移回去;这里就不再测试