apache +tomcat 群集

1.操作系统
RedHat Linux 9.0

2.所需软件
Apache 2.0.54
http://apache.freelamp.com/httpd
下载该文件httpd-2.0.54.tar.gz

Tomcat5.5.9
http://apache.justdn.org/jakarta/tomcat-5/v5.5.9/bin/
下载该文件jakarta-tomcat-5.5.9.tar.gz

Mod_jk 1.2.14
http://apache.justdn.org/jakarta/tomcat-connectors/jk/source/jk-1.2.14/
下载该文件jakarta-tomcat-connectors-1.2.14-src.tar.gz

Jdk 1_5_0_04
http://java.sun.com/j2se/1.5.0/download.jsp
下载该文件jdk-1_5_0_04-linux-i586-rpm.bin
下载时将所需软件包文件保存在/usr/local目录下

3.安装步骤

A 安装JDK
# cd /usr/local/
# chmod +x jdk-1_5_0_04-linux-i586-rpm.bin
# ./jdk-1.5.0_04-linux-i586-rpm.bin
# cd /usr/java/
# ln -s /usr/java/jdk-1_5_0_04 /usr/local/java
# vi /etc/profile
#

添加如下内容

JAVA_HOME=/usr/java/jdk-1_5_0_04
CLASSPATH=/usr/java/jdk-1_5_0_04/lib/dt.jar:/usr/java/jdk-1_5_0_04/lib/tools.jar
PATH=/usr/java/jdk-1_5_0_04/bin:$PATH
export PATH JAVA_HOME CLASSPATH

B 安装Apache

# cd /usr/local/
# tar xvfz httpd-2.0.54.tar.gz
# cd httpd-2.0.54
# ./configure --prefix=/usr/local/apache --enable-module=so 注:--prefix 指定apache安装路径
# make
# make install
# cd /usr/local/apache/conf
# vi ./httpd.conf

将Listen 80 修改为Listen 127.0.0.1:80 (219行)
将ServerName 修改为ServerName LocalHost:80 (291行)

在DirectoryIndex中添加 index.jsp (394行)
# cd /usr/local/apache/bin/
# ./apachectl configtest

显示Syntax ok则表明安装成功

#./apachectl start





启动apache服务,浏览器中访问本机80端口,查看端口是否正常,输入127.0.0.1:80

# ./apachectl stop

关闭服务

备注:prefix定义apache的安装路径

C安装Tomcat

# cd /usr/local/
# tar xvfz jakarta-tomcat-5.5.9.tar.gz
# ln ?s /usr/local/jakarta-tomcat-5.5.9 /usr/local/tomcat
# vi /usr/local/tomcat/bin/catalina.sh

第46行加入此句
JAVA_HOME=/usr/java/jdk-1_5_0_04

启动服务后,访问本机8080端口,查看端口是否正常(输入127.0.0.1:8080)

# /usr/local/tomcat/bin/startup.sh /startup.bat

关闭服务

# /usr/local/tomcat/bin/shutdown.sh /shutdown.bat

D 安装JK

# /usr/local/apache/bin/apachectl start (开启apache)
# cd /usr/local/
# tar xzvf jakarta-tomcat-connectors-1.2.14-src.tar.gz
# cd jakarta-tomcat-connectors-1.2.14-src/jk/native
# chmod 755 buildconf.sh
# ./buildconf.sh
# ./configure --with-apxs=/usr/local/apache/bin/apxs
# make
# make install
# cd /usr/local/jakarta-tomcat-connectors-jk1.2.14-src/jk/native/apache-2.0/
# cp mod_jk.so /usr/local/apache/modules/

E 系统整合

# vi /usr/local/apache/conf/httpd.conf

在文件最末尾加上如下语句

include "conf/mod_jk.conf"

在conf 目录下 建立文件 mod_jk.conf :
#Load mod_jk
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so
#指定 workers.properties文件路径
# Configure mod_jk
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
#指出那些请求交给tomcat 处理,"loadbalancer" 为workers.properties里指定的负载均衡器
JkMount /*.jsp loadbalancer
JkMount /servlet/* loadbalancer
JkMount /application/* loadbalancer


#vi /usr/local/apache/conf/workers.properties
添加下列语句

#
# workers.properties
#
# In Unix, we use forward slashes:
ps=/
# list the workers by name
worker.list=tomcat1, tomcat2, loadbalancer
# ------------------------
# First tomcat server
# ------------------------
worker.tomcat1.port=8009 			#ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=127.0.0.1		 #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1   #server的加权比重,值越高,分得的请求越多

# Specify the size of the open connection cache.
#worker.tomcat1.cachesize
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat1.lbfactor=100

# ------------------------
# Second tomcat server
# ------------------------

worker.tomcat2.port=9009
worker.tomcat2.host=127.0.0.1
worker.tomcat2.type=ajp13
worker.tomcat1.lbfactor = 1   

# Specify the size of the open connection cache.
#worker.tomcat2.cachesize
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat2.lbfactor=100

# ------------------------
# Load Balancer worker
# ------------------------
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
# once in a while. Until then all work is redirected to peer
# worker.

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1, tomcat2  #指定分担请求的tomcat

#
# END workers.properties
#

#vi /usr/local/tomcat/conf/server.xml

<Connector port="8009"
protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler"
redirectPort="8443" protocol="AJP/1.3" />  第101行

在119行左右加入如下语句(去注释)

<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1"></Engine>

若第二台tomcat,将jvmRoute的修改为tomcat2系统整合完毕
另外<Connector> 端口要更改別的::



启动apache和tomcat服务
# /usr/local/apache/bin/apachectl start
# /usr/local/tomcat/bin/catalina.sh start

将如下内容添加至/etc/rc.d/rc.local文件末尾以便系统启动后开启apache,tomcat服务
#vi /etc/rc.d/rc.local
添加
/usr/local/tomcat/bin/catalina.sh start
/usr/local/apache/bin/apachectl start

5.编写一个测试jsp
建立一个目录test.里面新建一个test.jsp,内容为
<%
   System.out.println("===========================");
%>
把test放到tomcat1,tomcat2的webapps下
6.启动apache,tomcat1,tomcat2,进行测试
通过 http://localhost/test/test.jsp 访问,查看tomcat1的窗口,可以看到打印了一行"=========="
再刷新一次,tomcat2也打印了一条,再刷新,可以看到请求会被tomcat1,tomcat2轮流处理,实现了负载均衡





  <%@ 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>");%>
<%
  out.println("<br> ID " + session.getId()+"<br>");
  
  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">
    Name:<input type=text size=20 name="dataName">
     <br>
    VALUE:<input type=text size=20 name="dataValue">
     <br>
    <input type=submit>
   </form>
</body>
</html>




To run session replication in your Tomcat 6.0 container, the following steps should be completed:

        * All your session attributes must implement java.io.Serializable
        * Uncomment the Cluster element in server.xml
        * If you have defined custom cluster valves, make sure you have the ReplicationValve defined as well under the Cluster element in server.xml
        * If your Tomcat instances are running on the same machine, make sure the tcpListenPort attribute is unique for each instance, in most cases Tomcat is smart enough to resolve this on it's own by autodetecting available ports in the range 4000-4100
        * Make sure your web.xml has the <distributable/> element or set at your <Context distributable="true" />
        * If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine <Engine name="Catalina" jvmRoute="node01" > and that the jvmRoute attribute value matches your worker name in workers.properties
        * Make sure that all nodes have the same time and sync with NTP service!
        * Make sure that your loadbalancer is configured for sticky session mode.

    Load balancing can be achieved through many techniques, as seen in the Load Balancing chapter.

    Note: Remember that your session state is tracked by a cookie, so your URL must look the same from the out side otherwise, a new session will be created.

    Note: Clustering support currently requires the JDK version 1.5 or later.

    The Cluster module uses the Tomcat JULI logging framework, so you can configure logging through the regular logging.properties file. To track messages, you can enable logging on the key:org.apache.catalina.tribes.MESSAGES





看了很多资料得到以下解释
–enable-module=so
so模块用来提供DSO支持的apachehe核心模块,
–enable-shared=max
max是指所有已配置模块都编译成DSO模块(除了so以外的,so不能被编译成DSO的)
使用动态加载方式会带来5%的性能下降,但和带来的好处相比更本不算什么:比如模块升级方便,系统升级风险降低,安装过程标准化。
--enable-module=most
用most可以将一些不常用的,不在缺省常用模块中的模块编译进来

我的问题是如果指定–enable-shared=max 是不是就不用加–enable-module=so这一句了呀?

因为我看很多网上教程的通用的编译选项能满足以后任意模块的安装是这样写的:
./configure --prefix=/home/apache/ --enable-shared=max --enable-module=most
它们都没有加–enable-module=so这一句呀!!!!!为什么呀!!!

APACHE是配置文件默认是只要你安装了一个动态加载模块。系统会默认将SO放进去。不需要你特意说明的。系统会自动检查的。

你可能感兴趣的:(apache,jdk,tomcat,linux,jsp)