CentOS安装apache + tomcat集群(一)

来源:互联网 发布:sql教材推荐 编辑:程序博客网 时间:2024/06/06 14:14

1.下载下列组件:
wget  http://apache.etoak.com/tomcat/tomcat-7/v7.0.27/bin/apache-tomcat-7.0.27.tar.gz
wget  http://labs.renren.com/apache-mirror//apr/apr-1.4.6.tar.gz
wget  http://apache.etoak.com//apr/apr-util-1.4.1.tar.gz
wget  http://labs.renren.com/apache-mirror//httpd/httpd-2.2.22.tar.gz
wget  http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.35-src.tar.gz

 

2.安装apr-1.4.6(最好按这个顺序安装,要不后来安装mod_jk的时候会有点小麻烦,要不停的配置apache,安装过程中出现的缺少linux组件的问题,我也一并记录了一下,安装中按提示把缺少的组件安装上)
[root@localhost app]#tar -zxvf apr-1.4.6.tar.gz
[root@localhost app]#cd apr-1.4.6

[root@localhost apr-1.4.6]#./buildconf
buildconf: checking installation...
buildconf: python version 2.6.5 (ok)
buildconf: autoconf not found.
           You need autoconf version 2.59 or newer installed
           to build APR from SVN.
[root@localhost apr-1.4.6]#yum install autoconf

[root@localhost apr-1.4.6]#./buildconf
buildconf: libtool not found.
           You need libtool version 1.4 or newer installed
           to build APR from SVN.
[root@localhost apr-1.4.6]#yum install libtool

[root@localhost apr-1.4.6]#./configure

[root@localhost apr-1.4.6]# make
-bash: make: command not found
[root@localhost apr-1.4.6]#yum install make

[root@localhost apr-1.4.6]# make
[root@localhost apr-1.4.6]# make install

3.安装apr-util-1.4.1
[root@localhost app]# tar -zxvf apr-util-1.4.1.tar.gz
[root@localhost app]# cd apr-util-1.4.1
[root@localhost apr-util-1.4.1]#./buildconf --with-apr=/app/apr-1.4.6
[root@localhost apr-util-1.4.1]# ./configure --with-apr=/app/apr-1.4.6
[root@localhost apr-util-1.4.1]#make
[root@localhost apr-util-1.4.1]#make install

4.安装httpd-2.2.22.tar.gz
[root@localhost app]# tar -zxvf httpd-2.2.22.tar.gz
[root@localhost app]# cd httpd-2.2.22
[root@localhost httpd-2.2.22]# ./buildconf
[root@localhost httpd-2.2.22]# ./configure
[root@localhost httpd-2.2.22]# make
[root@localhost httpd-2.2.22]# make install

5.安装tomcat-connectors-1.2.35-src.tar.gz
[root@localhost app]tar -zxvf tomcat-connectors-1.2.35-src.tar.gz
[root@localhost app]cd tomcat-connectors-1.2.35-src/native
[root@localhost native]# ./configure --with-apxs=/usr/local/apache2/bin/apxs
[root@localhost native]#make
[root@localhost native]#make install


6.配置Apache httpd服务器
在apache安装目录下conf目录中找到httpd.conf
[root@localhost app]# cd /usr/local/apache2/conf/
在文件最后加上下面一句话就可以了
Include conf/mod_jk.conf


7.在httpd.conf文件同一目录下建立mod_jk.conf文件,内容如下:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so

#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器
JkMount /*.jsp controller

 

8.在httpd.conf同目录下新建 workers.properties文件,内容如下:

worker.list = controller,tomcat1,tomcat2  #server 列表

#========tomcat1========
worker.tomcat1.port=8009         #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1   #server的加权比重,值越高,分得的请求越多

#========tomcat2========
worker.tomcat2.port=9009       #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1   #server的加权比重,值越高,分得的请求越多

#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2   #指定分担请求的tomcat
worker.controller.sticky_session=1


9.解压apache-tomcat-7.0.27.tar.gz,并copy一份,home目录分别命名为tomcat1,tomcat2
[root@localhost app]# tar -zxvf apache-tomcat-7.0.27.tar.gz
[root@localhost app]# mv apache-tomcat-7.0.27 tomcat1
[root@localhost app]# cp -R tomcat1/ tomcat2

10.更改tomcat2的server.xml文件,由于我的两个tomcat是在同一个服务器上,所以要修改下面红颜色的端口,以防端口冲突。
<Server port="9005" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

11.在tomcat1,2的webapps的目录下建test目录,进入test目录,建立index.jsp文件,内容如下:

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%

System.out.println("sessionId--->"+session.getId());
out.println("<br> sessionID " + session.getId()+"<br>");

// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session 列表</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>

12 .安装JDK1.6,我测试用的1.6没有问题。启动httpd,tomcat1,tomcat2服务。访问:http://xxxx.xx.xx.xx/test/index.jsp(如果访问不到,看看是不是centos的防火墙没有关闭),可以看到session ID号。当你刷新页面的时候,查看日志文件,会在两个tomcat上轮流打印出sessionId号,但是session ID号会不停变化,也就是只配好了负载均衡,集群还没有配好,接下来配置集群。