1. linux下apache+tomcat集群详细配置
环境:
操作系统均为:CentOS 5.1
Apache2.X服务器一台:IP地址192.168.232.4;安装路径/usr/local/apache;
Tomcat6服务器一台:IP地址192.168.232.5;安装路径/usr/local/tomcat;
Tomcat6服务器一台:IP地址192.168.232.6;安装路径/usr/local/tomcat;
配置:
Apache安装:
#./configure --prefix=/usr/local/apache --enable-moles=so --enable-mods-shared=all --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-rewrite
注释:激活tomcat集群需要的 enable-proxy,enable-proxy-http,enable-proxy-connect,enable-proxy-ajp和enable-proxy-balancer,其中proxy-ajp和proxy-balancer必须依赖proxy,如果是自定义的编译除了以上几个必须的模块外,mod_status也要编译进去,切记。enable-proxy-ftp可以不编译。
#make;make install
制作Apache启动项:
#cp support/apachectl /etc/rc.d/init.d/httpd
#vi /etc/rc.d/init.d/httpd
添加以下内容:(包括#号)
# Startup script for the Apache Web Server
# chkconfig: 2345 85 15
# description: Apache is a World Wide Web server .It is used to server
# HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache/log/httpd.pid
# config: /usr/local/apache/conf/httpd.conf
增加服务项
#chkconfig --add httpd
#chmod 755 /etc/rc.d/init.d/httpd
#chkconfig --level 345 httpd on
JDK安装:
#chmod a+x jdk-6u4-linux-i586-rpm.bin
#./jdk-6u4-linux-i586-rpm.bin
java环境变量设置:
#vi /etc/profile
在文件最后添加以下内容:
JAVA_HOME=/usr/java/jdk1.6.0_04
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CLASSPATH PATH CATALINA_HOME
执行如下命令使环境变量生效:
source /etc/profile
测试配置是否成功:
java –version
Tomcat安装:
#wget [url]http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.tar.gz[/url]
#tar zxvf apache-tomcat-6.0.16.tar.gz
#mv apache-tomcat-6.0.16 /usr/local/tomcat
Tomcat随机启动:
#vi /etc/rc.local
添加以下内容:
/usr/local/tomcat/bin/startup.sh
tomcat6配置文件server.xml:
把
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
改成
<!-- You should set jvmRoute to support load-balancing via AJP ie :
-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcatX">
<!--
<Engine name="Catalina" defaultHost="localhost">
-->
说明:
第一台tomcat就把jvmRoute="tomcat1"
第二台tomcat就把jvmRoute="tomcat2"
把
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
去掉注释变为
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
***群集详细配置***
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session."/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
配置应用的web.xml:
在每个webapps应用中,修改配置文件web.xml文件 添加元素<distributable/>
在web.xml文件中<web-app>元素下增加以下内容:
<!--此应用将与群集服务器复制Session-->
<distributable/>
具体修改如下:
修改前:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="[url]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
version="2.5">
</web-app>
修改后:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="[url]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
version="2.5">
<!--此应用将与群集服务器复制Session-->
<distributable/>
</web-app>
配置apache的ajp负载均衡功能:
确保将以下Mole的注释去掉
LoadMole proxy_mole moles/mod_proxy.so
LoadMole proxy_connect_mole moles/mod_proxy_connect.so
LoadMole proxy_ftp_mole moles/mod_proxy_ftp.so
LoadMole proxy_http_mole moles/mod_proxy_http.so
LoadMole proxy_ajp_mole moles/mod_proxy_ajp.so
LoadMole proxy_balancer_mole moles/mod_proxy_balancer.so
LoadMole status_mole moles/mod_status.so
增加以下内容:
# Proxypass Config
Include conf/extra/httpd-modproxy.conf
建立文件httpd-modproxy.conf输入内容:
<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from all
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
ProxyRequests Off
ProxyPass / balancer://tomcatcluster stickysession=jsessionid nofailover=On
<Proxy balancer://tomcatcluster>
BalancerMember [url]http://192.168.232.5:8080[/url] loadfactor=1
BalancerMember [url]http://192.168.232.6:8080[/url] loadfactor=2
</Proxy>
注释:
ProxyRequests Off 表示启用反向代理,必须开启;
ProxyPass为代理转发的Url,即将所有访问/的请求转发到群集balancer://tomcatcluster,这里为/即将所有访问/的请求转发到群集balancer://tomcatcluster的/test目录;
BalancerMember为群集的成员,即群集服务器1或2,负载均衡服务器会根据均衡规则来将请求转发给BalancerMember;
调试负载均衡集群系统:
访问apache服务器的web服务:[url]http://192.168.232.4/balancer-manager[/url]
如果显示负载均衡有关信息则说明成功了,接着可以访问[url]http://192.168.232.4/[/url]即访问到了tomcat的应用
***必须先启动Tomcat服务再启动Apache服务!***
参考文档:
[url]http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html[/url]
[url]http://tomcat.apache.org/tomcat-6.0-doc/balancer-howto.html[/url]
[url]http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/mod/mod_proxy.html[/url]
[url]http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/mod/mod_proxy_balancer.html[/url]
2. 如何在linux下配置tomcat
一.下载安装对应的jdk,并配置Java环境。
官网下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u26-download-400750.html
下载将jdk加压后放到/usr/local目录下:
[root@master ~]#chmod 755 jdk-6u5-linux-x64.bin
[root@master ~]# ./jdk-6u5-linux-x64.bin
[root@master ~]#mv jdk1.6.0_05 /usr/local
建立/usr/local/下的jdk软连接方便以后版本升级 :
[root@master ~]# ln -s /usr/local/jdk1.6.0_05/ /usr/local/jdk
配置环境变量:
在 /etc/profile 中加入以下内容:
JAVA_HOME=/usr/local/jdk1.6.0_05
JAVA_BIN=/usr/local/jdk1.6.0_05/bin
PATH=$PATH:$JAVA_BIN
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
保存退出
[root@master ~]source /etc/profile
查看java环境变量是否生效
[root@master ~]# java -version
Java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 10.0-b19, mixed mode)
测试成功
二.下载安装tomcat(http://tomcat.apache.org/)
[root@master ~]# unzip apache-tomcat-6.0.30.zip
[root@master ~]# mv apache-tomcat-6.0.30/ /usr/local/
[root@master ~]cd /usr/local/
[root@master local]# ln -s /usr/local/apache-tomcat-6.0.30/ /usr/local/tomcat
[root@master local]# cd tomcat/bin/
[root@master bin]#ls
[root@master bin]#vim catalina.sh
添加以下内容:
CATALINA_HOME=/usr/local/apache-tomcat-6.0.30/
[root@master local]#chmod +x *.sh
三.启动tomcat服务器
[root@master tomcat]# /usr/local/tomcat /bin/catalina.sh start
Using CATALINA_BASE: /usr/local/apache-tomcat-6.0.30/
Using CATALINA_HOME: /usr/local/apache-tomcat-6.0.30/
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-6.0.30//temp
Using JRE_HOME: /usr/local/jdk1.6.0_05
Using CLASSPATH: /usr/local/apache-tomcat-6.0.30//bin/bootstrap.jar
[root@master logs]# cd /usr/local/tomcat/logs/
[root@master logs]# tail -f catalina.out
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Jul 4, 2011 11:06:57 AM org.apache.coyote.http11.Http11Protocol pause
INFO: Pausing Coyote HTTP/1.1 on http-8080
Jul 4, 2011 11:06:58 AM org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
Jul 4, 2011 11:06:58 AM org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8080
四.在浏览器中输入
http://localhost:8080/(如果不是本机,则输入对应的ip地址)
测试出现tomcat页面则测试成功
ps:需要说明的是tomcat的默认测试页面是放在webapps下面,这个其实是在server.xml文件中配置的,如下所示:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
[root@SOR_SYS apache-tomcat-6.0.30]# ls
LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf lib logs temp webapps work
1)webapps文件夹主要用于web应用程序部署,比如你可以把你的应用程序包,如war文件拷到该目录下,容器会自动部署。
2)conf文件夹下主要是放置tomcat的服务器的相关配置文件
五。添加应用,再次进行测试
在webapps目录下放测试包(注意:webaapps目录下面主要是放应用包的.war在tomcat重启访问后会自动解压)
[root@master webapps]# pwd
/usr/local/tomcat/webapps
[root@master webapps]# tar -zxvf moni2.tar.gz
[root@master webapps]# ls
docs examples host-manager manager moni2 moni2.tar.gz ROOT
[root@master webapps]# /usr/local/tomcat/bin/catalina.sh start(重启tomcat)
Using CATALINA_BASE: /usr/local/apache-tomcat-6.0.30/
Using CATALINA_HOME: /usr/local/apache-tomcat-6.0.30/
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-6.0.30//temp
Using JRE_HOME: /usr/local/jdk1.6.0_05
Using CLASSPATH: /usr/local/apache-tomcat-6.0.30//bin/bootstrap.jar
在浏览器中输入如下地址:
http://192.168.55.229:8080/moni2/