Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Restructuring; cleaned up formatting; cleaned up description

Using PHP With Tomcat

This page describes tutorial shows how to use PHP (v4) version 4 with Tomcat (v4 version 4 or later). The contents were 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.

...

  • Download PHP (this tutorial was done with uses PHP 4.3.5)
  • Download Tomcat (this tutorial was done with uses Tomcat 5.0.19)
  • Define $JAVA_HOME for your JDK installation
  • Define $TOMCAT_HOME for your tomcat Tomcat installation
  • Define $PHP_HOME for your PHP installation

Configure and make PHP Installation:

  • ./configure --with-servlet=$TOMCAT_HOME --with-java=$JAVA_HOME
  • make
  • A jarfile and dynamic library are produced: sapi/servlet/phpsrvlt.jar and libs/libphp4.so.

Copy jar to your webapp's or tomcat's common class repository:

  • cp $PHP_HOME/sapi/servlet/phpsrvlt.jar $TOMCAT_HOME/common/lib

Declare PHP servlet and servlet-mapping in your or tomcat's common web.xml:

  • Copy from $PHP_HOME/sapi/servlet/web.xml servlet and servlet-mapping and paste into $TOMCAT_HOME/conf/web.xml.

Modify your LD_LIBRARY_PATH to include the dynamic library produced in step 1 above:

  • LD_LIBRARY_PATH=$PHP_HOME/libs
  • export LD_LIBRARY_PATH

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

Patch for PHP configure Script

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 Wiki MarkupThis is a patch for PHP's configure script to account for the servlet jar name change (from servlet.jar in Servlet Spec 2.3 and previous \[tomcat 4 and previous\] to servlet-api.jar in Servlet Spec 2.4 and above \[tomcat 5 and later\]):

No Format
--- 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

  • Extract the source code to PHP in a work directory
  • Patch if needed (that is, patch if building PHP to run with Tomcat version 5 or later)
  • Run configure, then make in the top directory of the PHP sources:
    No Format
    
     ./configure --with-servlet=$TOMCAT_HOME --with-java=$JAVA_HOME
     make
  • A jar file and dynamic library are produced from the make: sapi/servlet/phpsrvlt.jar and libs/libphp4.so.
  • Copy the jar file to your web application's class repository, or, alternately, to Tomcat's common class repository (as is shown here):
    No Format
    
     cp $PHP_HOME/sapi/servlet/phpsrvlt.jar $TOMCAT_HOME/common/lib

  • Declare PHP servlet and servlet-mapping in the web applications web.xml file, or in Tomcat's shared web.xml file:
    • Copy from $PHP_HOME/sapi/servlet/web.xml the servlet and servlet-mapping and paste into the file $TOMCAT_HOME/conf/web.xml.
  • Modify your LD_LIBRARY_PATH to include the dynamic library produced in the first step above:
    No Format
    
     LD_LIBRARY_PATH=$PHP_HOME/libs
     export LD_LIBRARY_PATH

Start Tomcat

No Format

 $TOMCAT_HOME/bin/startup.sh.

Testing

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

In that file, simply put:

No Format

   <?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.