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/