Deploy PHP Pages and JSP Pages Within the Same Web Context : Page 2

You need to integrate PHP content and JSP content in a single Web application. Should you convert the PHP to JSP, or vice versa? A better question is why choose. You can deploy both in the same Web application context.

by Kyle Gabhart

Aug 7, 2006

Page 2 of 3

Solution Methodology

As with any problem, several solutions are available. The solutions for this problem can be grouped into the following three major categories:

Let Apache serve as the primary or default request processor, passing only Servlet/JSP requests to the Java container.

Let the Java container serve as the primary or default request processor, allowing only PHP/CGI requests to be natively handled within Apache.

Kick Apache to the curb and run PHP/CGI natively within Tomcat.

The first two categories represent fairly similar approaches. Which approach appeals most to you may largely be determined by your comfort level with Apache HTTP and Tomcat, respectively. The third category, although a temptingly simple solution, is undesirable because Apache handles PHP/CGI content so efficiently and it offers a more secure front end to Web applications.

The approach this solution takes is the second category: Tomcat serves as the primary request processor while Apache serves as the secondary supporting processor. (Figure 1 presents a simple visualization of this approach.) If your preference is more along the lines of the first category outlined above, it is simple enough to adapt this approach accordingly.

Configuring Apache

The first step in this whole process may very well be the most daunting. You need to install the Apache HTTP server and configure it to talk to Apache Tomcat. Google searches will quickly turn up a slew of tutorials, wikis, and mailing list entries on the subject. However you configure it, you will likely need to use an Apache Tomcat connector, either mod_jk or mod_jk2. I highly recommend that you use mod_jk, as the mod_jk2 branch was abandoned in 2004.

At the bottom of Apache’s HTTP config file (/$APACHE_INSTALL/conf/httpd.conf), you should find the following block of code:

#
# Use name-based virtual hosting.
#
#NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#

Below this section, create a VirtualHost element to capture all HTTP requests on port 80 and map them according to the methodology described previously (Tomcat is default, Apache only handles PHP and CGI requests):