Feb 15
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
Hi!!
Thanks, very nice effort.
I was successful setting up php5.2.5 / tomcat5.5 on windows xp.
One small suggestion is that in step(0 or 10), instead of jar-ing/un-jar-ing I managed by renaming the php5servlet.dll to php5srvlt.dll and got ride of the famous “Unspecified Link” exception.
A point to mention is that that “TOMCAT” does not seem use system classpath, you have to feed in -Djava.library.path(for pointing to php) when you start the service on windows.
Ha! I was wondering if merely renaming the DLL would work. Thanks!
Thanks — the test.php works fine with phpinfo(), but other scripts show nothing in the browser or in the page source. Any thoughts?
Kevin
the phpinfo works BUT if you run any other *.php file they show a blank page
I was trying to run php5 on tomcat 5.0 using the above intructions. when i opened the URL for the testpage i got the following error in the file “hs_err_pid3820″ and my webpage displayed a blank page.
Can you please help me. It is urgent.
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0×03b31e7b, pid=3820, tid=1824
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_15-b04 mixed mode, sharing)
# Problematic frame:
# C [php5ts.dll+0xa1e7b]
#
————— T H R E A D —————
Current thread (0×02f63808): JavaThread “http-8080-Processor25″ daemon [_thread_in_native, id=1824]
siginfo: ExceptionCode=0xc0000005, reading address 0×0000000a
Registers:
EAX=0×0000000a, EBX=0×009c1000, ECX=0×009c1000, EDX=0×0000000a
ESP=0×0390f3c4, EBP=0×00001505, ESI=0×00138200, EDI=0×009c1000
EIP=0×03b31e7b, EFLAGS=0×00010216
Top of Stack: (sp=0×0390f3c4)
0×0390f3c4: 030ec3f8 009c5054 0000001c 030056f8
0×0390f3d4: 009c1000 03b34e1b 030056f8 0000000a
0×0390f3e4: 009c1000 009c504c 0000003c 0390f424
0×0390f3f4: 00000002 030ec3f8 00000000 02d7a300
0×0390f404: 00000000 02d7a300 00000000 02e05bf0
0×0390f414: 03a983f1 009c2107 009c5010 0000001c
0×0390f424: 02e02f98 009c52e0 030ec3f8 009c4150
0×0390f434: 03f874c0 030ec3f8 03a970f9 00000001
Instructions: (pc=0×03b31e7b)
0×03b31e6b: ff 08 8b df bd 05 15 00 00 72 75 8b f7 c1 ee 03
0×03b31e7b: 0f be 10 8b cd 83 eb 08 c1 e1 05 03 cd 03 ca 40
Stack: [0×038d0000,0×03910000), sp=0×0390f3c4, free space=252k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [php5ts.dll+0xa1e7b]
[error occurred during error reporting, step 120, id 0xc0000005]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
V+24
V+199
V+285
V+64
V+6
V+42
V+154
V+611
V+113
V+45
V+102
j net.php.servlet.startup()V+0
j net.php.servlet.init(Ljavax/servlet/ServletConfi
j org.apache.catalina.core.StandardWrapper.loadServlet()Ljavax/servlet/Servlet;+717
j org.apache.catalina.core.StandardWrapper.allocate()Ljavax/servlet/Servlet;+75
j org.apache.catalina.core.StandardWrapperValve.invoke(Lorg/apache/catalina/connector/Request;Lorg/apache/catalina/connector/Respons
j org.apache.catalina.core.StandardContextValve.invoke(Lorg/apache/catalina/connector/Request;Lorg/apache/catalina/connector/Respons
j org.apache.catalina.core.StandardHostValve.invoke(Lorg/apache/catalina/connector/Request;Lorg/apache/catalina/connector/Respons
j org.apache.catalina.valves.ErrorReportValve.invoke(Lorg/apache/catalina/connector/Request;Lorg/apache/catalina/connector/Respons
j org.apache.catalina.core.StandardEngineValve.invoke(Lorg/apache/catalina/connector/Request;Lorg/apache/catalina/connector/Respons
j org.apache.catalina.connector.CoyoteAdapter.service(Lorg/apache/coyote/Request;Lorg/apache/coyote/Respons
j org.apache.coyote.http11.Http11Processor.process(Ljava/io/InputStream;Ljava/io/OutputStrea
j org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Lorg/apache/tomcat/util/net/TcpConnection;[Ljava/lang/Objec
j org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(Ljava/net/Socket;Lorg/apache/tomcat/util/net/TcpConnection;[Ljava/lang/Objec
j org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt([Ljava/lang/Objec
j org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run()V+167
j java.lang.Thread.run()V+11
v ~StubRoutines::call_stub
————— P R O C E S S —————
Java Threads: ( => current thread )
0×03054910 JavaThread “TP-Monitor” daemon [_thread_blocked, id=2956]
0×02e90cd8 JavaThread “TP-Processor4″ daemon [_thread_in_native, id=2508]
0×030ad3b8 JavaThread “TP-Processor3″ daemon [_thread_blocked, id=3136]
0×030ad1a0 JavaThread “TP-Processor2″ daemon [_thread_blocked, id=1340]
0×030ad018 JavaThread “TP-Processor1″ daemon [_thread_blocked, id=1904]
0×03024408 JavaThread “http-8080-Monitor” [_thread_blocked, id=1520]
=>0×02f63808 JavaThread “http-8080-Processor25″ daemon [_thread_in_native, id=1824]
0×02d3ac98 JavaThread “http-8080-Processor24″ daemon [_thread_in_native, id=1916]
0×02d0fce0 JavaThread “http-8080-Processor23″ daemon [_thread_blocked, id=4036]
0×02d0f688 JavaThread “http-8080-Processor22″ daemon [_thread_blocked, id=3212]
0×031dcc70 JavaThread “http-8080-Processor21″ daemon [_thread_blocked, id=948]
0×031dc160 JavaThread “http-8080-Processor20″ daemon [_thread_blocked, id=820]
0×031db700 JavaThread “http-8080-Processor19″ daemon [_thread_blocked, id=2120]
0×02fe3060 JavaThread “http-8080-Processor18″ daemon [_thread_blocked, id=1296]
0×02fe2550 JavaThread “http-8080-Processor17″ daemon [_thread_blocked, id=1740]
0×02d439f8 JavaThread “http-8080-Processor16″ daemon [_thread_blocked, id=2036]
0×031c2218 JavaThread “http-8080-Processor15″ daemon [_thread_blocked, id=2304]
0×031c1ba8 JavaThread “http-8080-Processor14″ daemon [_thread_blocked, id=1968]
0×031c1220 JavaThread “http-8080-Processor13″ daemon [_thread_blocked, id=3164]
0×031bdd00 JavaThread “http-8080-Processor12″ daemon [_thread_blocked, id=2776]
0×0310ae38 JavaThread “http-8080-Processor11″ daemon [_thread_blocked, id=816]
0×0310a820 JavaThread “http-8080-Processor10″ daemon [_thread_blocked, id=2196]
0×03043c98 JavaThread “http-8080-Processor9″ daemon [_thread_blocked, id=2724]
0×03055da8 JavaThread “http-8080-Processor8″ daemon [_thread_blocked, id=1676]
0×030557c8 JavaThread “http-8080-Processor7″ daemon [_thread_blocked, id=3236]
0×02f62868 JavaThread “http-8080-Processor6″ daemon [_thread_blocked, id=3284]
0×03025c08 JavaThread “http-8080-Processor5″ daemon [_thread_blocked, id=420]
0×02d3fc30 JavaThread “http-8080-Processor4″ daemon [_thread_blocked, id=2356]
0×031ddc38 JavaThread “http-8080-Processor3″ daemon [_thread_blocked, id=340]
0×031dd6b0 JavaThread “http-8080-Processor2″ daemon [_thread_blocked, id=396]
0×02fe3d50 JavaThread “http-8080-Processor1″ daemon [_thread_blocked, id=4088]
0×0304a880 JavaThread “ContainerBackgroundProcessor[StandardEngine[Catalina]]” daemon [_thread_blocked, id=3460]
0×00a72e00 JavaThread “Low Memory Detector” daemon [_thread_blocked, id=1004]
0×00a719e8 JavaThread “CompilerThread0″ daemon [_thread_blocked, id=2736]
0×00a70b38 JavaThread “Signal Dispatcher” daemon [_thread_blocked, id=3036]
0×00a6bba8 JavaThread “Finalizer” daemon [_thread_blocked, id=1040]
0×00a6a760 JavaThread “Reference Handler” daemon [_thread_blocked, id=1404]
0×00036da8 JavaThread “main” [_thread_in_native, id=716]
Other Threads:
0×00a47ac8 VMThread [id=2692]
0×00a74040 WatcherThread [id=2916]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap
def new generation total 576K, used 430K [0×24020000, 0×240c0000, 0×24500000)
eden space 512K, 71% used [0×24020000, 0×2407b8a8, 0×240a0000)
from space 64K, 99% used [0×240b0000, 0×240bfff8, 0×240c0000)
to space 64K, 0% used [0×240a0000, 0×240a0000, 0×240b0000)
tenured generation total 6132K, used 3827K [0×24500000, 0×24afd000, 0×28020000)
the space 6132K, 62% used [0×24500000, 0×248bce00, 0×248bce00, 0×24afd000)
compacting perm gen total 8192K, used 4672K [0×28020000, 0×28820000, 0×2c020000)
the space 8192K, 57% used [0×28020000, 0×284b0078, 0×284b0200, 0×28820000)
ro space 8192K, 67% used [0×2c020000, 0×2c582410, 0×2c582600, 0×2c820000)
rw space 12288K, 47% used [0×2c820000, 0×2cdcf748, 0×2cdcf800, 0×2d420000)
Dynamic libraries:
0×00400000 - 0×0040d000 C:\Program Files\Java\jdk1.5.0_15\bin\java.exe
0×7c900000 - 0×7c9b0000 C:\WINDOWS\system32\ntdll.dll
0×7c800000 - 0×7c8f5000 C:\WINDOWS\system32\kernel32.dll
0×77dd0000 - 0×77e6b000 C:\WINDOWS\system32\ADVAPI32.dll
0×77e70000 - 0×77f02000 C:\WINDOWS\system32\RPCRT4.dll
0×77fe0000 - 0×77ff1000 C:\WINDOWS\system32\Secur32.dll
0×77c10000 - 0×77c68000 C:\WINDOWS\system32\MSVCRT.dll
0×6d740000 - 0×6d8de000 C:\Program Files\Java\jdk1.5.0_15\jre\bin\client\jvm.dll
0×7e410000 - 0×7e4a0000 C:\WINDOWS\system32\USER32.dll
0×77f10000 - 0×77f57000 C:\WINDOWS\system32\GDI32.dll
0×76b40000 - 0×76b6d000 C:\WINDOWS\system32\WINMM.dll
0×76390000 - 0×763ad000 C:\WINDOWS\system32\IMM32.DLL
0×10000000 - 0×10015000 C:\WINDOWS\system32\APSHook.dll
0×77f60000 - 0×77fd6000 C:\WINDOWS\system32\SHLWAPI.dll
0×77c00000 - 0×77c08000 C:\WINDOWS\system32\VERSION.dll
0×76bf0000 - 0×76bfb000 C:\WINDOWS\system32\PSAPI.DLL
0×6d300000 - 0×6d308000 C:\Program Files\Java\jdk1.5.0_15\jre\bin\hpi.dll
0×6d710000 - 0×6d71c000 C:\Program Files\Java\jdk1.5.0_15\jre\bin\verify.dll
0×6d380000 - 0×6d39d000 C:\Program Files\Java\jdk1.5.0_15\jre\bin\java.dll
0×6d730000 - 0×6d73f000 C:\Program Files\Java\jdk1.5.0_15\jre\bin\zip.dll
0×6d540000 - 0×6d553000 C:\Program Files\Java\jdk1.5.0_15\jre\bin\net.dll
0×71ab0000 - 0×71ac7000 C:\WINDOWS\system32\WS2_32.dll
0×71aa0000 - 0×71aa8000 C:\WINDOWS\system32\WS2HELP.dll
0×71a50000 - 0×71a8f000 C:\WINDOWS\system32\mswsock.dll
0×662b0000 - 0×66308000 C:\WINDOWS\system32\hnetcfg.dll
0×71a90000 - 0×71a98000 C:\WINDOWS\System32\wshtcpip.dll
0×009c0000 - 0×009c8000 C:\WINDOWS\system32\php5servlet.dll
0×03a90000 - 0×03fb2000 C:\WINDOWS\system32\php5ts.dll
0×77120000 - 0×771ab000 C:\WINDOWS\system32\OLEAUT32.dll
0×774e0000 - 0×7761d000 C:\WINDOWS\system32\ole32.dll
0×74320000 - 0×7435d000 C:\WINDOWS\system32\ODBC32.dll
0×5d090000 - 0×5d12a000 C:\WINDOWS\system32\COMCTL32.dll
0×7c9c0000 - 0×7d1d6000 C:\WINDOWS\system32\SHELL32.dll
0×763b0000 - 0×763f9000 C:\WINDOWS\system32\comdlg32.dll
0×773d0000 - 0×774d3000 C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\comctl32.dll
0×20000000 - 0×20017000 C:\WINDOWS\system32\odbcint.dll
VM Arguments:
jvm_args: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=D:\apache-tomcat-5.5.26\conf\logging.properties -Djava.endorsed.dirs=D:\apache-tomcat-5.5.26\common\endorsed -Dcatalina.base=D:\apache-tomcat-5.5.26 -Dcatalina.home=D:\apache-tomcat-5.5.26 -Djava.io.tmpdir=D:\apache-tomcat-5.5.26\temp
java_command: org.apache.catalina.startup.Bootstrap start
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_15
JRE_HOME=C:\Program Files\Java\jdk1.5.0_15
CLASSPATH=C:\Program Files\Java\jdk1.5.0_15\lib\tools.jar;D:\apache-tomcat-5.5.26\bin\bootstrap.jar
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\HPQ\IAM\bin;D:\Work\php
USERNAME=asha
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 15 Model 36 Stepping 2, AuthenticAMD
————— S Y S T E M —————
OS: Windows XP Build 2600 Service Pack 2
CPU:total 1 (cores per cpu 1, threads per core 1) family 15 model 36 stepping 2, cmov, cx8, fxsr, mmx, sse, sse2, sse3, mmxext, 3dnowext, 3dnow
Memory: 4k page, physical 392560k(51600k free), swap 942556k(394852k free)
vm_info: Java HotSpot(TM) Client VM (1.5.0_15-b04) for windows-x86, built on Feb 9 2008 01:07:28 by “java_re” with MS VC++ 6.0
@asha It’s a JVM crash. That’s always bad news. Try upgrading to a more recent version of Java, this issue may have been patched by Sun.
@Kevin & @shorif Do you have an example of PHP code that fails this way? Does it make a difference it you start your scripts with error_reporting(E_ALL)?
Tomcat doesn’t seem to fill in the _SERVER variables except for REQUEST_TIME, argv and argc. argv is an empty array. I’d like to get the various kinds of HTTP information such HTTP_USER_AGENT, HTTP_REFERER, etc. Is there anything to do? I prefer using Tomcat.
I get a ClassNotFoundException on net.php.servlet. What library or jar file is that supposed to be in?
Step 9 talks about unjarring php5srvlt.jar. In the pecl-5.2.5-win32.zip file I downloaded the name is phpsrvlt.jar. Has anyone gotten the steps to work using this jar file? Just curious why the 2 properties files haven’t been updated - the original instructions referred at the top have a date of 12/11/2004. thx!
I answered my own question (post 8). The ClassNotFoundException I had with net.php.servlet was due to an incorrect re-jarring on my part.