Using PHP With Tomcat

This tutorial shows how to use PHP version 4 with Tomcat version 4 or later. The tutorial was originally written and sent to the tomcat-dev mailing list by Jean-Frederic Clere (on his vacation, no less (wink) ) and Henri Gomez.

Prerequisites

Patch for PHP configure Script

There is a patch required to compile PHP to use Tomcat 5.

Prior to version 2.4 of Servlet Specification, the name of the servlet jar file was servlet.jar. In version 2.4 of the Servlet Specification, this name was changed to servlet-api.jar. Tomcat 4 uses the name servlet.jar, whereas Tomcat 5 and later uses servlet-api.jar. This causes problems with PHP's configure script.

This patch for PHP's configure script will fix this problem:

--- configure.org       2004-04-07 11:20:24.000000000 +0200
+++ configure   2004-04-07 11:22:50.000000000 +0200
      if test "$withval" = "yes"; then
        SERVLET_CLASSPATH=.
      else
+      if test -f $withval/common/lib/servlet-api.jar; then
+        SERVLET_CLASSPATH=$withval/common/lib/servlet-api.jar
+      fi
+
        if test -f $withval/lib/servlet.jar; then
          SERVLET_CLASSPATH=$withval/lib/servlet.jar
       fi

PHP Installation

Fedora Core 1 Issues with Tomcat 5.5.9, PHP 4.3.11 and jdk1.5.0_03

This may have just been an issue with the particular system I was building, but I was unable to set $JAVA_HOME, $PHP_HOME, $TOMCAT_HOME, or $LD_LIBRARY_PATH at the command line. The workaround was to edit /etc/profile and add the variables there (i.e., and the line

 JAVA_HOME=/usr/java/jdk1.5.0_03

and add JAVA_HOME to the export variables).

Start Tomcat

 $TOMCAT_HOME/bin/startup.sh.

Testing

Create a file named test.php in the docBase directory of your webapp.

In that file, simply put:

   <?php phpinfo(); ?>

Point your browser at the file by navigating to http://localhost:8080/test.php

If everything is working as it should, you will see an informational status page produced by PHP.