Integrating PHP into Tomcat
You can integrate PHP into Tomcat. To do so, you'll need PHP4 so you can build the servlet SAPI. To date, I have not found a servlet SAPI for PHP5.
To successfully build PHP and the Java jar file, you must patch PHP's configure
script before you can configure PHP itself. You must also patch one of the Java class files if you are using a recent JDK.
Patching the Java class file
The file you need to edit is in the sapi/servlet directory in your PHP source tree. Edit the servlet.java
file. You are going to change a variable name so it does not clash with a reserved word in Java. The variable is only used on lines 66, 70 and 71.
Change the 'enum' variable to 'xenum', as highlighted below:
if (!request.getMethod().equals("POST"))
Unknown macro: { result = request.getQueryString(); }
else {
Enumeration '''xenum
'{{ = request.getParameterNames();
String concat = "";
result = "";
while (}}'xenum
'{{.hasMoreElements()) {
String name = (String)}}'xenum
'{{.nextElement();
String value = request.getParameter(name);
Patching the }}configure{{ script
The PHP }}configure{{ script has never been updated for the latest Java Servlet API specification. In the specification, the servlet jar file was renamed from }}servlet.jar{{ to }}servlet-api.jar''. To build PHP's servlet SAPI module, you need to update the configure
script to check for the existence of servlet-api.jar
. Edit the configure
file and search for SERVLET_CLASSPATH
. The reference you're looking for is around line 8,445. You need to add some "if" blocks in the "else" branch, like this:
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/common/lib/servlet.jar; then
SERVLET_CLASSPATH=$withval/common/lib/servlet.jar
fi
The highlighted if block needs to be added.
Configuring PHP
The relevant configure
parameters for PHP are:
- --with-javaOPS:=path to JDK : this is where your JDK is installed
- --with-servletOPS:=path to Tomcat : this is where Tomcat is installed
The make
and make install
steps should should be error-free. If not, then you need to fix those problems first.
Installation
The make install
step above should copy the phpsrvlt.jar
jar file from the PHP build tree to Tomcat's common/lib directory. If it doesn't then you need to do that manually.
Tomcat's web.xml
needs to be updated to include the servlet mappings. A web.xml
exists in the sapi/servlet directory of the PHP source tree with the proper mappings. Copy them into the appropriate Tomcat xml file.
Finally, you need to create a symbolic link in the lib/php directory where you installed PHP. Create a linked named php4.so
pointing to libphp4.so
as follows:
<code>
ln -s libphp4.so php4.so
</code>
Now, you need to restart Tomcat.
Testing
Add this code to a file in Tomcat's webapps/ROOT directory:
<code>
<?php
phpinfo();
?>
</code>
and access this file from a browser. You should get PHP's standard phpinfo page.