UPDATE APRIL 2011:
This is an old post and as such is not current for PHP or Tomcat as they are today. Please do not follow this post’s instructions as they are now likely out of date.
More current information can be found at
http://wiki.apache.org/tomcat/UsingPhp
I would in fact now recommend simply running Quercus PHP:
http://www.caucho.com/resin-3.0/quercus/
Thank you,
-Chris.


.
.
PHP ScrewsSometimes, you want to run PHP with Tomcat. Why? Well, you may have a legacy product, for instance, that will require servlets for many more years. Or you may be using this gigantic Java program and are only interested in adding a tiny PHP piece on the side.

There are many guides showing how to do this available, but they become outdated almost as soon as they are published. So, it’s my turn to write a short-lived guide, this time for PHP 5.2.5 :)
Note thas this post relies greatly on information found here. Too bad even that guide got old so fast!

  1. Go to http://www.php.net/downloads.php and download the current version. I am going to do the setup on a Windows machine here, so I can simply download the binaries. On *nix, you will need to compile PHP. I know I will have to, anyway…
  2. You also need to download the corresponding PECL modules.
  3. Let’s assume that your current Tomcat install can be found in c:Tomcat5. Create a c:Tomcat5php directory and unzip the PHP zip file in it.
  4. Rename php.ini-dist, in c:Tomcat5php, to php.ini
  5. Extract php5servlet.dll from the PECL zip file to c:Tomcat5php
  6. Create a directory under c:Tomcat5webapps; in our case: phptest
  7. In c:Tomcat5webappsphptest, create a subdirectory: WEB-INF
  8. In c:Tomcat5webappsphptestWEB-INF, create web.xml with the following content:
    <!--?xml version="1.0" encoding="ISO-8859-1"?-->
     
                    php
                    net.php.servlet
     
                    php-formatter
                    net.php.formatter
     
                    php
                    *.php
     
                    php-formatter
                    *.phps
  9. Extract/unjar (using jar xvf or WinZip) php5srvlt.jar under c:Tomcat5phptmp
  10. Modify both c:Tomcat5phptmpnetreflect.properties and c:Tomcat5phptmpnetservlet.properties, replacing
    library=phpsrvlt

    with

    library=php5servlet

    and save.

  11. Jar the content of c:Tomcat5phptmp into a new version of php5srvlt.jar
  12. Move php5srvlt.jar to c:Tomcat5commonlib
  13. Copy c:Tomcat5phpphp5servlet.dll and c:Tomcat5phpphp5ts.dll to c:windowssystem32
  14. Create a test page in c:Tomcat5webappsphptesttest.php with this contents:
    <!--?php phpinfo(); ?-->
  15. Start Tomcat and go to http://localhost:8080/phptest/test.php

It should work. If it doesn’t, you can always post the stack trace here.

If you enjoyed this post, make sure you subscribe to my RSS feed!