Sometimes, 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!
- 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…
- You also need to download the corresponding PECL modules.
- Let’s assume that your current Tomcat install can be found in c:\Tomcat5\. Create a c:\Tomcat5\php\ directory and unzip the PHP zip file in it.
- Rename php.ini-dist, in c:\Tomcat5\php\, to php.ini
- Extract php5servlet.dll from the PECL zip file to c:\Tomcat5\php\
- Create a directory under c:\Tomcat5\webapps\; in our case: phptest
- In c:\Tomcat5\webapps\phptest\, create a subdirectory: WEB-INF
- In c:\Tomcat5\webapps\phptest\WEB-INF\, create web.xml with the following content:
ini
-
<?xml version="1.0" encoding="ISO-8859-1"?>
-
<!DOCTYPE web-app PUBLIC
-
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-
"http://java.sun.com/dtd/web-app_2_3.dtd">
-
<web-app>
-
<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>
-
</web-app>
-
- Extract/unjar (using jar xvf or WinZip) php5srvlt.jar under c:\Tomcat5\php\tmp\
- Modify both c:\Tomcat5\php\tmp\net\reflect.properties and c:\Tomcat5\php\tmp\net\servlet.properties, replacing
ini
-
library=phpsrvlt
with
ini-
library=php5servlet
and save.
-
- Jar the content of c:\Tomcat5\php\tmp\ into a new version of php5srvlt.jar
- Move php5srvlt.jar to c:\Tomcat5\common\lib\
- Copy c:\Tomcat5\php\php5servlet.dll and c:\Tomcat5\php\php5ts.dll to c:\windows\system32\
- Create a test page in c:\Tomcat5\webapps\phptest\test.php with this contents:
PHP
- 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.
Sphere: Related Content
JSON, or JavaScript Object Notation, is fairly popular.
…almost 