How to Connect Tomcat 6 to Apache HTTP Server 2

Tomcat can be run as a standalone server. Tomcat can also be run as an add-on to the Apache HTTP Server (or Microsoft IIS) - as the Java servlet/JSP container. In this combination, Tomcat executes the Java servlets and JSPs, the Apache serves the static HTML pages and performs other server-side functions such as CGI, PHP, SSI, etc. Read "Why should I integrate Apache with Tomcat? (or not)" at Tomcat FAQ (http://wiki.apache.org/tomcat/FAQ/Connectors#Q3).

To configure Tomcat to work with Apache HTTP Server, you should first read the documentation provided in Tomcat thoroughly, and read the "Tomcat Connector" documents @ http://tomcat.apache.org/connectors-doc.

To run Tomcat together with Apache:

  • Apache needs to load a "adapter" module, which uses a certain protocol, such as Apache JServ Protocol (AJP), to communicate with the Tomcat, via another TCP port (port 8009 in the default configuration).
  • When Apache receives an HTTP request, it checks if the request belongs to Tomcat. If so, it lets the adapter takes the request and forwards it to Tomcat, as illustrated below.

There are a few adapter modules available, such as Apache JServ Protocol (AJP) v1.2 "JServ" module (outdated), AJP v1.3 "JK 1.2" module (in use) and "JK 2" module (deprecated). I will only describe the JK1.2 module with Apache 2 here.

The step-by-step procedure is as follow:

Step 0.1: Install Apache HTTP Server - Refer to "Apache HTTP Server - How To". I shall assume that Apache is installed in directory "d:\myproject\apache", and runs on port 7000. I shall denote the apache installed directory as $APACHE_HOME.

Step 0.2: Install Tomcat - Refer to "Tomcat - How To". I shall assume that Tomcat is installed in directory "d:\myproject\tomcat", runs on port 8080. Tomcat's shall contains two web contexts: "/examples" (Tomcat's servlets and JSP examples) and "/ws" (to be created by you). I shall denote Tomcat's installed directory as $CATALINA_HOME (Catalina is the code name for Tomcat 5 and above).

Step 1: Download the Apache-Tomcat Connector Module - An Apache-Tomcat connector - JK1.2 module - which is an adapter module used by Apache to communicate with Tomcat (using AJP v1.3 protocol through TCP port 8009), can be downloaded from Tomcat mother site @ tomcat.apache.org (⇒ Download ⇒ Tomcat Connectors ⇒ JK 1.2 ⇒ JK 1.2 Binary Releases ⇒ win32 ⇒ jk-1.2.xx ⇒ "mod_jk-1.2.xx-httpd-2.2.x.so").

Rename the downloaded module to "mod_jk.so" and move into directory "d:\myproject\apache\modules".

Step 2: Configure Apache - We need to configure the Apache HTTP Server to load and initialize the JK module.

Create a configuration file called "mod_jk.conf" as follows and place it in "d:\myproject\tomcat\conf":

# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so # Where to find workers.properties
# Update this path to match your conf directory location
JkWorkersFile d:/myproject/tomcat/conf/workers.properties # Where to put jk logs
# Update this path to match your logs directory location
JkLogFile d:/myproject/tomcat/logs/mod_jk.log # Set the jk log level [debug/error/info]
JkLogLevel info # Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" # JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T" # Send everything for context /ws to worker ajp13
JkMount /ws ajp13
JkMount /ws/* ajp13 # Send everything for context /examples to worker ajp13
JkMount /examples ajp13
JkMount /examples/* ajp13

For each web context that is to be forwarded from Apache to Tomcat, include two JKMount statements as shown. In the above configuration, Apache forwards all requests to web contexts "/examples" and "/ws" to Tomcat, via a "worker" called "ajp13". (Check the URL of the Tomcat's servlet and JSP examples from the Tomcat's welcome page! It may move!)

Include the above configuration directives into the Apache's configuration by adding the following include statement at the end of "d:\myproject\apache\conf\httpd.conf":

include d:/myproject/tomcat/conf/mod_jk.conf

Note: Unix's forward slash is used as the directory separator instead of backward slash (because Apache was originally built for Unix). The include statement simply appends all the statements from the file "d:\myproject\tomcat\conf\mod_jk.conf" into "httpd.conf". (You can of course add those statements into "httpd.conf" directly.)

Next, observe that the configuration refers to a worker file called "workers.properties", and forward certain requests to a JK worker called "ajp13". Create the "workers.properties" file and place it in "d:\myproject\tomcat\conf" as follows:

# Define 1 real worker named ajp13
worker.list=ajp13 # Set properties for worker named ajp13 to use ajp13 protocol,
# and run on port 8009
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009
worker.ajp13.lbfactor=50
worker.ajp13.cachesize=10
worker.ajp13.cache_timeout=600
worker.ajp13.socket_keepalive=1
worker.ajp13.socket_timeout=300

Note: The JKMount statements forward the requests to a worker called "ajp13", which is defined in this "workers.properties".

Step 3: Configure Tomcat - The default configuration in Tomcat's "conf\server.xml" starts the AJP1.3 service via the following configuration, on TCP port 8009 (remove the comments if these lines are commented out).

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

Step 4: Start the Apache with the JK module

D:\myproject\apache\bin> httpd -k install
D:\myproject\apache\bin> httpd -k start

Check the Apache's log "logs\errors.log" to confirm that JK module was started:

Starting the Apache2.2 service
The Apache2.2 service is running.
Apache/2.2.xx (Win32) mod_jk/1.2.xx configured -- resuming normal operations

Step 5: Start the Tomcat server

D:\myproject\tomcat\bin> startup

Observe that AJP1.3 service is initiated and the ajp13 worker is listening at port 8009.

....
Oct 1, xxxx 9:44:05 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Oct 1, xxxx 9:44:05 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/18 config=null
....

The order of starting up Tomcat and Apache is NOT important. Either apache or tomcat can be restarted at any time.

Step 6: Verify the Installation - Issue the following URLs to access the web contexts "/examples" and "/ws", that are defined in Tomcat (running in port 8080), but accessed via the Apache (running in port 7000).

http://hostname:7000/examples/servlets
http://hostname:7000/examples/jsp
http://hostname:7000/examples
http://hostname:8080/ws
http://hostname:7000/ws
Try Tomcat's servlet examples via Apache
Try Tomcat's JSP examples via Apache
Try Tomcat's examples via Apache
Access ws from Tomcat directly
Access ws via Apache

本文内容转载自:http://www.ntu.edu.sg/home/ehchua/programming/howto/ApachePlusTomcat_HowTo.html

如何配置Tomcat以使用Apache httpd?的更多相关文章

  1. How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover

    http://java.dzone.com/articles/how-configure-tomcatjboss-and In this post we will see how to setup a ...

  2. Apache httpd + tomcat 简单集群

    集群其实很简单,我们就来说一下httpd+tomcat集群都要注意哪些部分: 首先使用的东西有 apache-tomcat-8.0.32      下载地址: http://tomcat.apache ...

  3. Apache Httpd通过mod_jk连接多个Tomcat

    一个tomcat能够配置多个web apps,这是众所周知的.当更改了一个web app,想要又一次启动的时候.因为全部的web apps都是放在同一个tomcat下的,所以别的web apps也在重 ...

  4. Apache配置tomcat集群

     APACHE 2.2.9+TOMCAT6.0配置负载均衡 目标: 使用 apache 和 tomcat 配置一个可以应用的 web 网站,要达到以下要求: 1. Apache 做为 HttpSe ...

  5. Apache Httpd 反向代理配置 (笔记)

    Apache Httpd 配置Http反向代理 打开配置文件 httpd.conf 先启动相关模块(去掉前面的注释#)LoadModule proxy_module modules/mod_proxy ...

  6. 【高可用HA】Apache (4) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_jk

    Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_jk httpd版本: httpd-2.4.17 jk版本: tomcat-connectors-1.2.41 参考 ...

  7. 【高可用HA】Apache (3) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy

    Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy httpd版本: httpd-2.4.17 参考来源: Apache (1) -- Mac下安装Apac ...

  8. centos7 apache httpd安装和配置django项目

    一.安装httpd服务 apache在centos7中是Apache HTTP server.如下对httpd的解释就是Apache HTTP Server.所以想安装apache其实是要安装http ...

  9. 在Fedora8上配置Apache Httpd

    原以为Fedora8我安装的是最简版本,于是去Apache Httpd官网下一个httpd,但是速度很成问题,现在还没有下完. 打开Fedora8的光盘,里面有httpd-2.2.6.3-3.i386 ...

随机推荐

  1. 详解Linux下swig 3.0.12的手动安装过程

    详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...

  2. Go 基础 坑

    1.字符串空为"" 2. 传递的数组是原数组的拷贝,所以是无法通过传递数组的方法去修改原地址的数据的.在GO语言中除了切片(slice).集合(map).通道(channel)和接 ...

  3. ES(Elasticsearch)

    基本概念 Elasticsearch是一个实时分布式搜索和分析引擎 支持: 全文搜索 结构化搜索 分析 可以这样进行描述: 分布式的实时文件存储,每个字段都被索引并可被搜索 分布式的实时分析搜索引擎 ...

  4. python基础--字典

    Python基础--字典 字典的常用函数: dict.clear( )--->无任何返回值 说明: 清除字典内的所有的元素 语法: In [5]: dict.clear? Type: metho ...

  5. Oracle 使用序列实现自增列 及重置序列

    序列是oracle用来生产一组等间隔的数值.序列是递增,而且连续的.oracle主键没有自增类型,所以一般使用序列产生的值作为某张表的主键,实现主键自增.序列的编号不是在插入记录的时候自动生成的,必须 ...

  6. Python随笔--对象

    组合的用法:

  7. Android Stdio 无法打开模拟器

    安装好了各种版本的AVD,有个版本4.1,API版本16,219MB的模拟器是可以打开的,但是基本不能用,只能看到首界面,跳转什么的完全不行. 除此之外其它高版本的模拟器都不能用(API版本>2 ...

  8. 前端车牌识别SDK算法提取

    同行业中,别人标配有的产品我有,别人没有的产品我们也有,如此才能增强竞争力,通过优化创新,前端车牌识别SDK功能,性能上,都是行业NO.1的水平.车牌识别sdk这个用于越来越多人集成了,汽车保有量日益 ...

  9. MySQL MERGE存储引擎

    写这篇文章,主要是因为面试的时候,面试官问我怎样统计所有的分表(假设按天分表)数据,我说了两种方案,第一种是最笨的方法,就是循环查询所有表数据(肯定不能采用):第二种方法是,利用中间件,每天定时把前一 ...

  10. centos7初上手2-安装tomcat服务

    上一篇文章说完安装mysql数据库,这篇文章来学习一下tomcat安装 1.先做准备工作,安装jdk,先看服务器上有没有安装相关java文件 下载好1.8版本的安装包,用xftp传到服务器上(根据个人 ...