The integration of Apache and Tomcat

Source: Internet
Author: User
Tags url forwarding

An Apache versus Tomcat connection

Apache supports static pages, and Tomcat supports dynamic, such as Servlets.

For general use of Apache+tomcat, Apache is just a forwarding, and the processing of JSPs is handled by Tomcat.

Apache can support Php\cgi\perl, but to use Java, you need Tomcat to support Apache in the background and transfer Java requests from Apache to Tomcat processing.

Apache is a Web server , Tomcat is an application (Java) server, it is just a servlet (JSP also translated into a servlet) container, can be considered an Apache extension, However, it can be run independently of Apache .

These two can be compared in the following points:
1, both are developed by Apache organizations
2, both have the function of HTTP service
3. Both are free

Different points:
Apache is dedicated to providing HTTP services, and related configuration (such as virtual host, URL forwarding, etc.)
Tomcat is a JSP server developed by Apache in accordance with the Java EE JSP and servlet standards.

The benefits of consolidation are:
If the client is requesting a static page, only the Apache server responds to the request
If the client requests a dynamic page, the Tomcat server responds to the request
Because JSP is a server-side interpretation code, consolidation can reduce the service overhead of Tomcat

A simple way to integrate Apache with Tomcat under Linux II
Transferred from:

1, prepare, download the required documents. This assumes that you have properly installed the JDK.
To download the required files on the Apache official website:
where httpd and jakarta-tomcat-connectors are source packages, Apache-tomcat are binary packages.

2, install Apache.
# tar XZVF httpd-2.2.0.tar.gz
# CD httpd-2.2.0
# make
# make Install

3, install Tomcat.
# CP apache-tomcat-5.5.12.tar.gz/usr/local/
# cd/usr/local
# tar XZVF apache-tomcat-5.5.12.tar.gz
# ln-s apache-tomcat-5.5.12 Tomcat

4. Compile and generate MOD_JK.
# tar XZVF jakarta-tomcat-connectors-1.2.15-src.tar.gz
# CD Jakarta-tomcat-connectors-1.2.15-src/jk/native
# make
# CP./apache-2.0/

5, configuration.
Build two profiles mod_jk.conf and under/usr/local/apache2/conf/.

# VI Mod_jk.conf
Add the following content:
# indicate the location of the working file required for the MOD_JK module to work

# Where to put JK logs

# 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 all servlet and JSP requests through the AJP13 protocol to Tomcat for Tomcat to handle
jkmount/servlet/* Worker1
Jkmount/*.jsp Worker1

# VI
Add the following content:
# Defining a worker named Worker1 and of type ajp13

# Set Properties for Worker1

Then configure the httpd.conf to make the following changes:
Modify Listen 80 to Listen
Modify ServerName to ServerName localhost:80

Add index.jsp to DirectoryIndex

My Web page is placed under/var/wwwroot, so I want to modify the DocumentRoot
DocumentRoot "/var/wwwroot"
<directory "/var/wwwroot" >
Options includes FollowSymLinks
AllowOverride None
Order Deny,allow
Allow from all
Xbithack on
<directory "/var/wwwroot/web-inf" >
Order Deny,allow
Deny from all

To add a statement about loading MOD_JK:
LoadModule Jk_module modules/

Finally, edit the Tomcat configuration file Server.xml and add the following in the host section:
<context path= "" docbase= "/var/wwwroot" debug= "0" reloadable= "true" crosscontext= "true"/>
Create a index.jsp under/var/wwwroot, launch Apache and Tomcat, access the http://localhost/with a browser, and you should see the correct page.

Three Windows Apache and Tomcat integration
Transferred from:

Preparatory work:
1. Apache 2.2.4
: Http://

2. Tomcat 6.0.16
: ...

3. JDK
: Http:// ...-windows-i586-p.exe
If only the application, you can also choose to install the JRE, because the JRE is relatively small:)

4 jk-1.2.26-httpd-2.2.4
: Http:// ...
Note that the JK version must be the same as the Apache version. If your version and my different, can go to the official website to download, there are many versions, applicable to various operating systems, download the applicable can, address:

To start the installation:
The installation order of the software can be adjusted appropriately, but the JDK (JRE) must be installed before Tomcat:

1. Installing the JDK (JRE)
The default, custom installation is available here. After the installation is complete, set your system environment variables as follows: My Computer, Properties----Advanced environment variables (System variables, non-user variables are added here)
Java_home = D:\Program files\java\jdk1.6.0_07
CLASSPATH =.; %java_home%\lib\tools.jar;%java_home%\lib\dt.jar;
PATH =%java_home%\bin;%path%
Note that the Java_home path changes to your actual installation path. The other two does not change.

2. Installing Apache
Note here that if you have IIS installed and have 80 ports, you can choose to disable IIS or specify a different port for IIS, or specify a different port for Apache to set according to the actual situation. In the Apache installation process to set up network domain and server name localhost, set your email address (this must be set, or Apache will not start up).
After installation, if Apache is using port 80, enter http://localhost in the browser, if you are using a different port such as 81, then enter http://localhost:81, you will see the success of Apache page. At this point Apache has run, in the system tray can see its icon, double-click Open can control Apache service, we also install Tomcat and JK, so now choose Stop Stop Apache service.

3. Install Tomcat
Unzip the Tomcat to D:\Program Files\tomcat and set the environment variable:
Catalina_base = D:\Program Files\tomcat
Catalina_home = D:\Program Files\tomcat
CLASSPATH =%catalina_home%\lib\servlet-api.jar
Running D:\Program Files\tomcat\bin\startup.bat launches Tomcat, and the Tomcat page indicates a successful installation.

4. Installing JK
Copy the to the D:\Program files\apache2.2\modules.

The configuration of the server is as follows:
1. Configuring Tomcat
Look for the Conf/server.xml file under the Tomcat directory and add the following statement in <context path= "" docbase= "E:\wwwroot" reloadable= "true" crosscontext= "true"/>
Here, if path is not empty, the virtual directory is set, and the value of path is the virtual directory name.

Look for the conf/ file under the Tomcat directory and compare the text below, if there is a difference, modify it with the text below.
workers.tomcat_home=c:\apache-tomcat-6.0.18 #让mod_jk模块知道Tomcat的位置
Workers.java_home=d:\program files\java\jre1.6.0_07 #让mod_jk模块知道jre的位置
worker.ajp13.port=8009 #工作端口, do not modify if not occupied #本机, if the above Apache host is not localhost, make the corresponding modification
Worker.ajp13.lbfactor=1 #代理数, no modification

If this file does not exist, it is created from the above content. Any changes to the Tomcat settings will be restarted to take effect.

2. Configure Apache
Open the httpd.conf under D:\Program files\apache2.2\conf, and at the end, add the following code and save it.
LoadModule Jk_module modules\
Jkworkersfile "D:\Program files\tomcat\conf\"
Jklogfile "D:\Program Files\tomcat\logs\mod_jk2.log"
Jkloglevel Info

<virtualhost localhost>
#如果非虚拟主机, <virtualhost localhost> and final </VirtualHost> comments can be removed
ServerAdmin localhost
DocumentRoot E:/wwwroot
<directory "E:/wwwroot" >
#您的站点项目所在路径, should be the same as the directory settings in Tomcat, it is said that the above two must be set at the same time to take effect, there is no time to try different times what happens
ServerName localhost
DirectoryIndex index.html index.htm index.jsp
Errorlog Logs/shsc-error_log.txt
Customlog Logs/shsc-access_log.txt Common
jkmount/servlet/* ajp13
#让Apache支持对servlet传送, used for Tomcat parsing
Jkmount/*.jsp ajp13
#让Apache支持对jsp传送, used for Tomcat parsing
Jkmount/*.do ajp13
#让Apache支持对. Do transfer for Tomcat parsing

#开头的行为注释, you can delete it. Here the configuration I was to set up the virtual directory, re-specify the Servername,documentroot path to configure the site, in fact, Apache configuration can be very flexible, you can also do not use the virtual directory, and directly to modify the default configuration of the site, Use any text editing tool, search for related items such as servername,documentroot,serveradmin, and then change the configuration of the item according to your actual situation. I recommend that you try a lot of different configurations, which is a fun experience.
After all the modifications are complete, double-click the Apache icon in the system tray and click Start to start the Apache service.
Enter http://localhost/in the address bar separately, and http://localhost:8080/if the results are the same, Apache and Tomcat are integrated successfully

Installation FAQs:
1. One boot Apache appears "Windows cannot start Apache2 on local computer" error
Most of the problem is the port, there may be other software use 80 port, it is best to change the Apache port to 81 port. View 80 port occupancy, suitable for viewing any port
Start – Run –cmd enter Netstat-ano at the command prompt to see all connected PID and find the program corresponding to this PID in Task manager if there is no PID in Task Manager, you can select "View"-"Choose Column" in Task Manager, often We start the application when the system needs to find the port is occupied by other programs, how to know who owns the port we need, many people are more headache, the following is a very simple method, I hope it is useful to everyone.

We need to make sure who's taking our 80 ports.
Execute under Windows command-line window: C:\>netstat-aon|findstr "80″
See, Port is occupied by process number 2044 process, continue to execute the following command:
C:\>tasklist|findstr "2044″
Avp.exe 2044 Console 0 37,680 K
Very clear, AVP Kappa Monitor 80 port, adjust the Cat network monitoring settings.
Of course, if your 80 port is occupied by another program, kill or adjust the port.
Four finishes!

The integration of Apache and Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.