This time we are not going to talk about integrating two frameworks of the
same language or platform. We'll take you through a different path altogether.
As we have been saying earlier, more often than not you might find yourself
working with different technologies. Reasons may vary from adding a module to
your Web application or simply deploying an application that has been built by
teams working with two different technologies as per their skill set.
So this time , we look into different techniques involved in case you are
deploying a Web application with both PHP and JSP pages. Integration between
different Java based frameworks has become easier as frameworks now provide
built-in support for this. Such kind of an integration has become a necessity
thanks to the 'Frameworkitis' that Java language seems to be suffering from.
But you are on a different path altogether when it comes to integrating
technologies different from each other. Much like what we are talking about
here.
|
Three different approaches
You can get the desired integration in three different ways in case of PHP and
JSP pages in the same Web application. The first is to make Tomcat the default
request handler and to allow PHP requests to be passed to the Apache HTTP
server.
The second is the reverse of the first, where you make Apache HTTP server the
default request processor while passing JSP, Servlet requests to Tomcat. And the
third, actually the simplest of them all, is allowing Tomcat to natively handle
PHP pages as well. However, the last solution is not so recommended as Apache
provides better container and security when it comes to PHP/CGI pages in
comparison to Tomcat's capabilities regarding the same.
We'll take up the first option in this article. And we would like to
clarify that this article is not about the coding that needs to be done in such
a process. Rather we talk about how the respective servers need to be configured
for achieving our goal. We used Apache 2.2 and Tomcat 5.5 for this exercise.
Configuring connectors
The first step involved in our approach is to configure Tomcat with Apache. The
Apache Software Foundation ships the mod_jk and mod_jk2 connectors for this
purpose. It is recommended not to use the latter, as no modifications have been
made to it since 2004 and its current status stands 'deprecated.'
Tomcat Server is the default handler with Apache Web Server Works for intercepting only the native requests viz. PHP and CGI pages |
You can download the latest, stable binary release from the URL
http://tomcat. apache.org/download-connectors.cgi. After you have installed both
Apache and Tomcat correctly and defined the environment variables viz. JAVA_HOME,
TOMCAT_ HOME, proceed with configuration of the connectors to make these two
talk to each other. The procedure differs with the versions of servers being
used. For a combination of Apache 1.3 and Tomcat 4.x, first of all you need to
tell the Tomcat server that it has to listen to AJP 1.3 requests, the protocol
used for these communications. Add the following entries to your server.xml file
for this:
port="8009" minProcessors="5"
maxProcessors="75"
acceptCount="10" debug="0"/>
In case of Apache 2.x and Tomcat 5.x this is not required as the connector
definition is provided by default. The port attribute specifies the port to
which Tomcat needs to listen to for the AJP 1.3 protocol, whereas 'className'
specifies that the requests are to be processed by the connector class
AJP13Connector. Next, configure the Apache server which is a bit tedious though
not overly complicated.
Create a Tomcat worker that defines the semantics of communication with
Tomcat to the Apache server we will be using. You can simply copy the sample
worker.properties file of the connector from the
directory to
A few sample lines of the worker file are as follows:
worker.list=pcqWorker
worker.pcqWorker.port=8009
worker.pcqWorker.host=localhost
worker.pcqWorker.type=ajp13
worker.tomcat_home=c:\tomcat5.5
worker.java_home=c:\Java
\j2sdk1.5.0
worker.ajp13.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_
workers=ajp13
worker.inprocess.jvm_lib=$(workers.
java_home)$(ps)bin$(ps)javai.dll
worker.inprocess.stdout=$(workers.
tomcat_home)$(ps)logs$(ps)inprocess.
stdout
worker.inprocess.stderr=$(workers.
tomcat_home)$(ps)logs$(ps)inprocess.
stderr
After this you need to copy the downloaded mod_jk connector to the
directory and edit the httpd.conf file as follows:
LoadModule mod_jk libexec\
AddModule mod_jk.c
In case of Apache 2.x and Tomcat 5.x the steps are different and simpler. You
can build the mod_jk module by compiling it using Visual C++ 6.0 and pasting the
compiled file in the 'module' directory of your Apache Server. The syntax of
the 'LoadModule' only changes with respect to the path being specified. Also
it is better to create an 'APACHE_HOME' variable in this case, which points
to the home directory of your Apache 2.x installation.
Final steps
Simply create a Virtual Host for specifying the Tomcat server to not handle the
PHP requests, by editing the httpd.conf file of Apache server as follows:
DocumentRoot www/webappps/ROOT
SetEnvIf Rquest_URI /.php no_jk
SetEnvIf Rquest_URI /.cgi no_jk
JKMount pcqWorker
It's a simple solution for making things work. While deploying the
application on Tomcat you can keep the PHP pages directly under the root
directory or specify a sub-directory that contains the PHP pages of your
application. It uses the same resources and scripts, be it style—sheets or
JavaScripts, as used by PHP and JSP in your Web application.