All Things Techie With Huge, Unstructured, Intuitive Leaps

PHP on Tomcat with JSPs




First of all, in spite of the fact that the world runs on LAMPS (Linux, Apache, MySQL, PHP, SQL), I really dislike PHP or Python.  They are sort of pseudo-Object Oriented and do not have the power of J2EE where one can integrate pure Java classes with JSPs.  My preferred approach is LAMJS where J is JSP or Java.  Our webserver is Apache Tomcat.

At any rate, we have a rang-dang-doo enterprise app written with Java, JSP, ServerFaces, and all of the good stuff.  Our business requirements say that we have to have a blog and a newsletter and a marketing side to our enterprise app.  We only have so many developers and we believe in the AGILE approach with continuous iteration and working software.  So to put the marketing side in place, we decided not to re-invent the wheel.  Wordpress has templates galore and all of the features that we need.  Wordpress is written in PHP and uses PHP, so we have to make Tomcat play with JSPs and PHP.  It was a hair-pulling experience.

The first thought was to use JavaBridge and JavaBridge.jar.  We kept getting this stack trace:



Fatal Error: Failed to start PHP ["php-cgi", "-v"], reason: java.io.IOException: Cannot run program ""php-cgi"" (in directory "C:\Users\Delon"): CreateProcess error=2, The system cannot find the file specified
Could not start FCGI server: java.io.IOException: PHP not found. Please install php-cgi. PHP test command was: [php-cgi, -v] 

php.java.bridge.http.FCGIConnectException: Could not connect to server
at php.java.bridge.http.NPChannelFactory.test(NPChannelFactory.java:64)
at php.java.bridge.http.FCGIConnectionPool.<init>(FCGIConnectionPool.java:175)
at php.java.bridge.http.FCGIConnectionPool.<init>(FCGIConnectionPool.java:189)
at php.java.servlet.ContextLoaderListener.createConnectionPool(ContextLoaderListener.java:541)
at php.java.servlet.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:185)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: File \\.\pipe\C:\Program Files\Apache Software Foundation\Tomcat 7.0\temp\JavaBridge3030620009245189355.socket not writable
at php.java.bridge.http.FCGIConnectException.<init>(FCGIConnectException.java:37)
... 15 more
Caused by: java.io.IOException: PHP not found. Please install php-cgi. PHP test command was: [php-cgi, -v] 
at php.java.bridge.Util$Process.start(Util.java:1145)
at php.java.servlet.fastcgi.FCGIProcess.start(FCGIProcess.java:68)
at php.java.bridge.http.NPChannelFactory.doBind(NPChannelFactory.java:94)
at php.java.bridge.http.FCGIConnectionFactory.runFcgi(FCGIConnectionFactory.java:88)
at php.java.bridge.http.FCGIConnectionFactory$1.run(FCGIConnectionFactory.java:109)


Obviously we hadn't configured Tomcat to find the PHP executables which were supposed to be in the .war file.  We tried various things like trying to install PHP on Windows (our dev machines are Windows, but our production and test servers are Linux), all to no avail.  We found plenty of instructions on how to show Apache how to find the PHP executables, but none for Tomcat.

I was really looking for a plug and play solution, and I found it with Caucho and their Quercus war file.  I simply downloaded the war file, and took a copy of it.  I changed the copy's .war extension to a .zip extension and unzipped it.  I took the resultant folder and dropped it into our webapps folder where Tomcat could find it and it worked first time.  Someone ought to preserve their blood for posterity.  You too can make PHP play with Tomcat by downloading the war file from here:  http://quercus.caucho.com/

 Thank you Caucho and Quercus where ever you are.

No comments:

Post a Comment