Monday, December 13, 2010

PHP & JAVA Bridge

Running PHP applications in Tomcat 6

This guide shows how to install and run PHP applications like Moodle, mediaWiki, Joomla as Tomcat 6 web applications. And how to install PHP 5.x for all existing web applications.

Download and install Java, Tomcat and PHP

Install a PHP web application into Tomcat


  1. Copy the PHP web application JavaBridgeTemplate.war or the demo JavaBridge.war to the Tomcat webapps directory.
  2. Wait two seconds until Tomcat has loaded the web application.
  3. Browse to http://127.0.0.1:8080/JavaBridgeTemplate5541 and http://127.0.0.1:8080/JavaBridgeTemplate5541/test.php to see the PHP info page.
  4. Rename the file file:webapps/JavaBridgeTemplate5541 directory, for example to file:webapps/Moodle, and download and install your PHP application to this directory.
  5. Browse to the name of your web application, for example http://127.0.0.1:8080/Moodle, to run it from your internet browser.

PHP support for all existing Tomcat web applications

If you want to enable PHP for all of your web applications, move the Java libraries from the local web application folder to the Tomcat library folder and edit the Tomcat web configuration as follows:

  1. Stop Tomcat.
  2. Move the libraries JavaBridge.jar, php-servlet.jar and php-script.jar from the webapps/JavaBridgeTemplate5541/WEB-INF/lib directory over to the tomcat lib directory.
  3. Edit the Tomcat conf/web.xml. Add the following 9 lines marked with a +:

    <web-app xmlns=... >

    + php.java.servlet.ContextLoaderListener
    + PhpJavaServletphp.java.servlet.PhpJavaServlet
    +

    + PhpCGIServletphp.java.servlet.fastcgi.FastCGIServlet
    + prefer_system_php_execOn
    + php_include_javaOff
    +

    + PhpJavaServlet*.phpjavabridge
    + PhpCGIServlet*.php
    ...

    </web-app>
  4. Start Tomcat again. Now you can add PHP scripts to tomcat.
  5. Add a PHP test file
    echo java("java.lang.System")->getProperties(); ?>
    to some web context, for example "examples", and browse to http://yourHost.com:8080/examples/test.php.
  6. Check the process list using the Unix/Linux or Windows task manager. There should be 5 php-cgi FastCGI executables waiting for requests from Java.
  7. Please see our FAQ for more information how to set up a load balancer or how to create a distributable PHP/Java web application.

Cross the Gap Between PHP and Java

Using sophisticated application frameworks to build your network server applications is fine, but sometimes these frameworks are overkill. With a little communications programming between PHP and Java, you can establish an extensible layer that handles the annoying details of creating a custom network protocol. 

hese days, using sophisticated application frameworks to build your network server applications is understandable, but sometimes such frameworks are overkill. When you strip networking down to its bare essentials, network sockets offer lot of flexibility and simplicity. With a little communications programming between PHP and Java, you can establish an extensible layer that handles the annoying details of creating a custom network protocol.



As you'll see, PHP is not only excellent for quick-and-dirty scripting. More and more developers also are using it to build sophisticated Web applications. Java, of course, has been used to build Web applications for years, but as anyone who has used a Java-based Web framework knows, it isn't the easiest technology to use. This article shows how to connect a PHP-based application to a custom Java server. The demonstration doesn't use Enterprise Java Beans (EJB) or any particular Java-based server framework. Instead, it creates a simple custom TCP/IP Java server to demonstrate how easy it is. Then, you'll see how to access such a server from Java.
Architecture
PHP programming is sometimes mistakenly referred to as client-side scripting. Technically, PHP runs on the server side, but because it is usually used to generate HTML—which the user sees directly—it is sometimes thought of as the client-side portion of server-side code.
In fact, the system in this article uses PHP in this very sense (click here to download the source code). It consists of a Java portion, which performs server-side computation, and a PHP portion, which generates HTML. Both portions run on the server and communicate using a TCP/IP socket. The terms client and server can be a little ambiguous in this context, so the article refers instead to the PHP portion and the Java portion.