Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Using PHP With Tomcat

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

PHP version 5 is not currently supported as it does not include the necessary servlet code.

...

0. Prerequisites:

  • Download PHP (this tutorial

...

  • was done with PHP 4.3.5)
  • Download Tomcat (this tutorial

...

  • was done with Tomcat 5.0.19)
  • Define $JAVA_HOME for your JDK installation
  • Define $TOMCAT_HOME for your

...

  • tomcat installation
  • Define $PHP_HOME for your PHP installation

Patch for PHP configure Script

There is a patch required to compile PHP 4 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:

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

Patch for sapi/servlet/servlet.java

enum is now a reserved word with Java 5, thus causing servlet.java to break the make process.

No Format

--- servlet.java.orig   2005-09-26 22:25:55.000000000 -0400
+++ servlet.java        2005-09-26 22:26:11.000000000 -0400
@@ -63,12 +63,12 @@
     if (!request.getMethod().equals("POST")) {
       result = request.getQueryString();
     } else {
-      Enumeration enum = request.getParameterNames();
+      Enumeration xenum = request.getParameterNames();
       String concat = "";
       result = "";

-      while (enum.hasMoreElements()) {
-        String name  = (String)enum.nextElement();
+      while (xenum.hasMoreElements()) {
+        String name  = (String)xenum.nextElement();
         String value = request.getParameter(name);

         try {

PHP Installation

...

  1. Configure PHP Installation:
    ./configure --with-servlet=$TOMCAT_HOME

...

  1. --with-java=$JAVA_HOME

...

A

...

jarfile and dynamic library are produced

...

:

  • sapi/servlet/phpsrvlt.jar

...

  • libs/libphp4.so

2.

...

Copy

...

jar

...

to your

...

webapp's

...

or tomcat's common class repository

...

:

...


...

cp

...

$PHP_HOME/sapi/servlet/phpsrvlt.jar

...

$TOMCAT_HOME/common/lib

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

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

...

No Format

  /usr/lib/jdk1.5.0_04/jre/lib/i386/client:/usr/lib/jdk1.5.0_04/jre/lib/i386:/usr/lib/jdk1.5.0_04/jre/../lib/i386

5. Start tomcat:
$TOMCAT_HOME/bin/startup.sh.

...

Testing:

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

2. In that file, simply put:
+++
<?php phpinfo(); ?>
+++

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

...

Wiki Markup
This 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\]):

— 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
++++

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

No Format
 JAVA_HOME=/usr/java/jdk1.5.0_03

and add JAVA_HOME to the export variables).

  • If make returns an error where javac is not a recognized command, you'll need to patch the Makefile produced by ./configure. Look for "&& javac" and replace it with the full path to javac (i.e., "
    No Format
     && /usr/java/jdk1.5.0_03/bin/javac
    ").
  • If make returns an error regarding "enum" while trying to build phpsrvlt.jar, you'll need to edit $PHP_HOME/sapi/servlet/servlet.java and replace enum with xenum.

Start Tomcat

No Format

 $TOMCAT_HOME/bin/startup.sh.

Testing

Verify the following is in your webapp's web.xml (creates the servlet entries and maps .php to that servlet and mentioned in the PHP installation steps above):

No Format

    <servlet>
        <servlet-name>php</servlet-name>
        <servlet-class>net.php.servlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>php-formatter</servlet-name>
        <servlet-class>net.php.formatter</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>php</servlet-name>
        <url-pattern>*.php</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>php-formatter</servlet-name>
        <url-pattern>*.phps</url-pattern>
    </servlet-mapping>

Verify that phpsrvlt.jar is in you WEB-INF/lib directory, or the tomcat common/lib directory (as mentioned above in the PHP installation steps)

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.

CategoryFAQ