RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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.


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.

Click to enlarge

Figure 1. Tomcat as Primary Request Processor and Apache as Secondary Supporting Processor

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):

<VirtualHost *:80>
    # Set DocumentRoot equal to Tomcat ROOT context
    DocumentRoot /www/webapps/ROOT

    # Exclude PHP & CGI (let Apache handle)
    SetEnvIf Request_URI \.php no-jk
    SetEnvIf Request_URI \.cgi no-jk

    # Tell mod_jk to handle everything else (non PHP and CGI content)
    # Assign the worker from worker.properties (‘myWorker’) to broker 
    #  the hand-off from Apache to Tomcat
    JkMount /* myWorker


As previously described, you could easily tailor the above approach to fit a variant methodology by changing the JkMount element to pass only certain requests (i.e. *.jsp or /*/servlet/*) to Tomcat.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date