实验环境:
系统 :centos6.4
Apache 172.16.4.7
Tomcat1 172.16.4.5
Tomcat2 172.16.4.11
一、在后端服务器上安装 tomcat、jdk
1、安装jdk
#rmp -ivh jdk-7u9-linux-x64.rpm
# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
#. /etc/profile.d/java.sh
# java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
2、安装部署tomcat
# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv apache-tomcat-7.0.42 tomcat
# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
#. /etc/profile.d/tomcat.sh
3、为Tomcat提供SysV脚本
# vim /etc/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
CATALINA_HOME=/usr/local/tomcat
export CATALINA_HOME
# export CATALINA_OPTS="-Xms128m -Xmx256m"
exec $CATALINA_HOME/bin/catalina.sh $*
#chmod +x /etc/init.d/tomcat
# chkconfig --add tomcat
就可以启动服务了
#service tomcat start
二、修改配置文件
1、修改修改配置
# vim /usr/local/tomcat/conf/server.xml
修改默认端口为80
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
找到host添加
<Engine name="Catalina" defaultHost="www.test.com"jvmRoute="TomcatA">
<Host name="www.test.com" appBase="/tocat/app1"
unpackWARs="true" autoDeploy="true">
<Context path="/" docBase="/tomcat/app1" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="test_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
#vim tomcat-users.xml 加用户
-->
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
# mkdir /tomcat/app1 -pv
2、添加测试文件
#vim 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>
#service tomcat stop
#service tomcat start
# jps
7334 Bootstrap
7355 Jps
3、测试一下
4、配置另外一台tomcat服务,和前面配置的tomcatA相同,只需要修改特定的选项
# vim /usr/local/tomcat/conf/server.xml
找到host添加
<Engine name="Catalina" defaultHost="www.test.com"jvmRoute="TomcatA">
其他的和前面配置相同
5、添加测试文件
# mkdir /tomcat/app1 -pv
#vim 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>
测试一下
三、安装配置apache
配置代理单台tomcat服务
1、编译安装 参考博客http://qidian510.blog.51cto.com/7319882/1282663
首先我们要安装apr和apr-util
下载地址http://apr.apache.org/和http://apr.apache.org/
(1) 编译安装apr
# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr
# make && make install
(2) 编译安装apr-util
# tar xf apr-util-1.5.2.tar.bz2
# cd apr-util-1.5.2
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
编译安装apache
# yum install -y pcre-devel openssl-devel
#tar xf httpd-2.4.6.tar.bz2
#cd httpd-2.4.6
--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
2、配置服务
# vim /etc/rc.d/init.d/httpd
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
给其执行权限,添加服务并且设定为自动运行
#chmod +x /etc/rc.d/init.d/httpd
#chkconfig --add httpd
#chkconfig httpd on
# cd /usr/local/apache/conf
# vim httpd.conf
启用模块
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
在最后添加
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost Off
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://172.16.4.5:8009/
ProxyPassReverse / ajp://172.16.4.5:8009/
<Location / >
Require all granted
</Location>
如上apache相关指令说明:
ProxyPreserveHost {On|Off}:如果启用此功能,代理会将用户请求报文中的Host:行发送给后端的服务器,而不再使用ProxyPass指定的服务器地址。如果需要反向代理中支持虚拟主机,则开启此项。
ProxyVia {On|Off|Full|Block}:用于控制在http首部是否使用Via:。默认为Off,即不启用此功能;On表示每个请求和响应报文均添加Via:;Full表示每个Via:行都会添加当前apache服务器的版本号信息;Block表示每个代理请求报文中的Via:都会被移除。
ProxyRequests {On|Off}:是否开启apache正向代理的功能;启用此项时为了代理http协议必须启用mod_proxy_http模块。同时,如果为apache设置了ProxyPass,则必须将ProxyRequests设置为Off。
ProxyPass [path] !|url [key=value key=value ...]]:将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径,path为当前服务器上的某虚拟路径,url为后端服务器上某URL路径。使用此指令时必须将ProxyRequests的值设置为Off
ProxyPassReverse:用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。
注:和ajp结合比http性能好
~
# service httpd configtest
# service httpd start
3、访问测试
配置多台tomcat实现负载均衡
1、修改配置文件
# cd /usr/local/apache/conf
# vim httpd.conf
前面配置相同,在最后添加
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost Off
<proxy balancer://lbcluster1>
BalancerMember ajp://172.16.4.5:8009/
BalancerMember ajp://172.16.4.11:8009/
</proxy>
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Location / >
Require all granted
</Location>
2、配置完成重启服务并测试
#service httpd restart
到这里我们的服务就配置完成了