本文共 13599 字,大约阅读时间需要 45 分钟。
1、安装tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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
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 | 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主配置文件,包含一个文件并创建该文件;实现代理功能
将下面指令添加到文件末尾即可
1 2 | # vim /etc/httpd/httpd.conf Include /etc/httpd/extra/mod_jk .conf |
------------------------------------------------------------------------
创建文件添加如下内容
1 2 3 4 5 6 7 | # 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 |
------------------------------------------------------------------------
1 2 3 4 5 6 7 | # 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 |
----------------------------------------------------------------------
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | # 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 admin@allen.com 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 admin@allen.com 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还会转移回去;这里就不再测试