Just Enough Apache HTTP Server integration with Tomcat Web Container

thumbsshameServers

Nov 17, 2013 (3 years and 9 months ago)

71 views

Apache HTTP Server integration with Tomcat Web Container
Just Enough Apache HTTP Server integration with Tomcat Web
Container
What is Apache HTTP Server?
A HTTP Server is a software that uses the Hypertext Transfer Protocol to make web
documents available to the internet. Apache is the robust, commercial-grade reference,
efficient, extensible implementation of the HTTP protocol. It is most popular HTTP
server and it is open source and free.
What is Tomcat Web Container?
Tomcat is a Java based Web Application container that was created to run Servlets and
JavaServer Pages (JSP) in Web applications. It was chosen by Sun as the offical
Reference Implementation.
(Note : Don't know what is Servlet/JSP yet? Do not worry. You can still go through this
tutorial for the integration of Apache and Tomcat. A subsequent tutorial will introduce
the concepts and development of Servlets/JSP)
Architecture
As shown in the diagram below is the architecture we will like to achieve. A user will use
his browser to access the resources server by the Apache HTTP Server. Some resources
will be routed to the Tomcat Container for processing.
For example, if the user request for an URL
http://www.companyname.com/resource/servletname
to Apache, the request will be
forwarded to Tomcat via the Connectors for processing. When the processing is
completed, the results will be sent from Tomcat to Apache and subsequently back to the
browser.
Tomcat
Connectors

Browser
Tomcat Web Container
Servlets
JSPs
Apache HTTP Server
Static
HTML
Why is the integration important?
Clustering

Use Apache as a front end to connect to multiple instances of Tomcat. Thus, when one
Tomcat fails, the other can still continue serving the users.
Serving of Static pages, CGI, Perl, PHP
Apache supports the serving of the above contents and it does it very well. Thus if the
static pages, CGI, Perl or PHP need to be used with Java together, the integration
deployment will be very useful.
Security
Your Web server can be placed in a demilitarized zone (between one firewall to the
external internet) while the application server (Tomcat in this case) and application data
stay protected behind a second firewall. This can achieve a highly secure environment.
What is required?
Apache HTTP Server
<OPENENTERPRISEXHOME>\v0.1\server\apache-2.0.49
Tomcat
<OPENENTERPRISEXHOME>\v0.1\server\jakarta-tomcat-5.0.19
Tomcat Connectors
<OPENENTERPRISEXHOME>\v0.1\server\apache-2.0.49\modules\mod_jk2.so
Tomcat-Apache plug-in that handles the communication between Tomcat and Apache.
All the above components are re already included in the OpenEnterpriseX distribution.
Simply take note of the directories they are installed in.
What is this tutorial about?
This tutorial aims to illustrate what are the required configurations for the integration. In
fact, when OpenEnterpriseX is installed, the integration has already been carried out by
the installer. Remember the Petstore application can be accessed via the Apache HTTP
server.
In this tutorial, the focus will be on testing out the integration followed by steps to turn
off the integration. With the concept, it will be extremely easy to configure the integration
of Apache with your J2EE application in the future.
Step 1 – Start Tomcat
In this tutorial we are going to assume OpenEnterpriseX is installed to the default
directory
C:\OpenEnterpriseX
. If OpenEnterpriseX is installed in other directory, simply
replace
C:\OpenEnterpriseX
with the installed directory name.
Goto the OpenEnterpriseX directory. Tomcat is stored in the server sub directory. The
script to start Tomcat is stored in the bin directory.
Setup the paths to the Java sdd with the following two commands. This is of course
assuming that the Java SDK is installed in
C:\j2sdk1.4.2_04
directory. Replace this
directory with the directory that the Java SDK is installed.
(Optional : If you have problems take a look at the script in
C:\OpenEnterpriseX\v0.1\oex\scripts\startall.bat. The first two lines contains the full
settings for the following commands. This is setup by the OpenEnterpriseX installer)
Start Tomcat with the following command.
cd C:\OpenEnterpriseX\v0.1\server\jakarta-tomcat-5.0.19\bin
startup
set JAVA_HOME=C:\j2sdk1.4.2_04
set PATH=C:\j2sdk1.4.2_04\bin;%PATH%
A new Dos window will be started to launch Tomcat. The following screenshot is how it
will look like when Tomcat has started completely.
Step 2 – Test out Tomcat in the Browser
Start your Browser (Internet Explorer or Mozilla or others).
Key in the URL as
http://localhost:8080
Tomcat is started on the default port 8080. You can change this port number by going to
C:\OpenEnterpriseX\v0.1\server\jakarta-tomcat-5.0.19\conf\serverl.xml
The following will appear in the browser if everything has been started successfully.
Scroll down the web page until you see the following. We are going to explore the Servlet
and JSP examples provided by Tomcat.
On the following page, look for a section similar to the screenshot below. Execute the
examples and explore the source.
Click
on this
Step 3 – Start Apache HTTP Server
Goto the Apache HTTP Server directory. The executable to start Apache is in the bin
subdirectory.
Start Apache by the following command.
The following is the screenshot for the output.
cd C:\OpenEnterpriseX\v0.1\server\apache-2.0.49\bin
apache
Step 4 – Test out Apache in the Browser
Start your Browser (Internet Explorer or Mozilla or others).
Key in the URL as
http://localhost
Apache is started on the default port 80, which is what the browser will assume. Thus
there is no need to put the port 80 at the end of the URL string. You can change this port
number by going to C:\OpenEnterpriseX\v0.1\server\apache-2.0.49\conf\httpd.conf
Step 5 – Test out the integration
Start your Browser (Internet Explorer or Mozilla or others).
Key in the URL as
http://localhost/servlets-examples/
Notice it also goes to the Servlet examples page started in Tomcat.
Apache HTTP Server integration with Tomcat Web Container
We have already achieve the following architecture. The reason is that during installation
of OpenEnterpriseX, the integration of Apache and Tomcat has already been carried out.
This is done by plugging in the Tomcat Connectors and configuring the components.
The rest of this tutorial will provide insights on how to plug in the connectors and the
configuration setup.
Browser
Tomcat Web Container
Servlets
JSPs
Apache HTTP Server
Static
HTML
Step 6 – Explore the Tomcat Connectors files
The Tomcat Connector can be downloaded separately from the Apache website.
It has already been integrated into Apache in OpenEnterpriseX.
Goto the Apache directory at C:\OpenEnterpriseX\v0.1\server\apache-2.0.49\
The following is a brief description of the files provided by the connectors
doc\mod_jk2 -
documentation files
modules\mod_jk2.so –
actual connector library
conf\mod_jk2.conf.sample -
sample configuration files for integration
conf\workers2.properties.sample -
sample configuration files for integration are in
OpenEnterpriseX provided the following file to setup the integration between Apache and
Tomcat :
conf\conf\workers2.properties
The following are the contents of the file. The 9
th
and 10
th
lines are the setup for Apache
to forward requests that access the URL patterns /servlet-examples/* to Tomcat via port
8009.
Port 8009 is the default Tomcat port used for the integration. It can be configured in
C:\OpenEnterpriseX\v0.1\server\jakarta-tomcat-5.0.19\conf\serverl.xml
[channel.socket:localhost:8009]
port=8009
host=127.0.0.1
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
[uri:/servlets-examples/*]
worker=ajp13:localhost:8009
[uri:/xpetstore-ejb/*]
worker=ajp13:localhost:8009
[uri:/jmx-console/*]
worker=ajp13:localhost:8009
The next two lines is used for the forwarding of requests from Apache to Tomcat if the
Petstore application is accessed. The last two lines is used for the forwarding of requests
from Apache to Tomcat if the Jboss console is accessed.
Thus in the future if there are any additional URL patterns in Tomcat to be integrated,
simply add to the above configuration file.
Locate the following line in the httpd.conf file (C:\OpenEnterpriseX\v0.1\server\apache-
2.0.49\conf\httpd.conf
)
LoadModule jk2_module modules/mod_jk2.so
The above line is used to inform Apache to make use of the Tomcat Connectors. It is
added by the OpenEnterpriseX installer. Comment or delete away this line if integration
between Apache and Tomcat is not required.
(Optional : Move the workers2.properties file to other directories and restart Apache.
Notice Apache can no longer forward requests to Tomcat. Undo what you have done at
the end of this tutorial.)