---
title: "apache and tomcat confingration"
author: "human_zx"
date: "2010-01-05T14:51:03Z"
tags: ["Apache", "Cache", "HTML", "JSP", "Tomcat"]
categories: ["企业架构"]
slug: "apache-and-tomcat-confingration"
draft: false
lastmod: "2010-01-05T14:51:03Z"
id: 563024
url: "https://rargers.iteye.com/blog/563024"
---

这几天没事大事要做，就想做做apache和tomcat的整合，网上找了下，发现大多数都是一个样子，对于tomcat的负载均衡的地方陪的不对。下面我就做个全的配置，免的别人在找了。

   首先下载apachel,tomcat ,jk.

   我下载的是tomcat-6.0.20免安装版，apache\_2.0.63，mod\_jk-1.2.28-httpd-2.0.52.so，安装apache，解压tomcat，我是测试的是两个tomcat，所以命名为tomcat1，tomcat2。

   apache配置：

   把 mod\_jk-1.2.28-httpd-2.0.52.so拷到apache的安装目录下的modules中。

   编辑conf里的httpd.conf文件，在最后添加：

 

   #加载mod\_jk Module

```
LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.0.52.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,"loadbalancer"为在workers.propertise里指定的负载分配控制器
JkMount /*.jsp loadbalancer
JkMount /servlet/* loadbalancer
```

  

在conf目录下建 workers.properties属性文件，内容为：

 

```
#
# workers.properties 
#
 
# list the workers by name

worker.list=tomcat1, tomcat2, loadbalancer

# ------------------------
# First tomcat server
# ------------------------
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13

# 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=localhost
worker.tomcat2.type=ajp13

# 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

#
# END workers.properties
#
```

 

   apache配置完毕。

   配置tomcat：

  修改tomcat1 conf里的server.xml文件

 

```
<Engine name="Catalina" defaultHost="localhost">
```

 

 改为

 

```
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
```

 

 

  同时把tomcat2 的配置文件的这个地方改为：

 

```
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
```

 

 如果tomcat是两个电脑的话，这样改就可以了，如果tomcat1和2在一个电脑上的，那么tomcat2里面的端口都要改下了

 

```
<Server port="8005" shutdown="SHUTDOWN">

<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />


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

 

 

改为：

 

```
<Server port="9005" shutdown="SHUTDOWN">

<Connector port="9080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />


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

 

 

这些端口号可以自己命名，只有不冲突就可以

建个文件夹test，新建 test.jsp

 

```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% 

   System.out.println("===========================");

%>
</body>
</html>
```

 

 把test文件夹放到tomcat1和2下，启动tomcat1和2.启动重启apache

测试[http://localhost/test/test.jsp](http://localhost/test/test.jsp)。如果控制太打印出来 “===========================”的话则说明负载均衡配置成功

 

先到这 ，有时间在学习～