Published on Feb 12 2012 in Java Tomcat

Sometimes Java hosting clients need ability to combine their Java code with PHP code. See how it can be achieved.

This is sometimes done by leaving PHP code in a directory under Apache Document Root (e.g. ~/public_html/drupal) and excluding requests to it from being passed to a Java application server e.g. Tomcat. It is helpful in separating Java and PHP applications running within 1 hosting account and static data like images, CSS, JavaScript. But there are cases when you need much closer integration of Java and PHP.

In this short tutorial we will show you how to use PHP-java bridege in JVM Host's Java hosting environment. We assume you already have a Tomcat account ready for use. Let's go!

Download JavaBridgeTemplate621.war. Optionally get php-java-bridge_6.2.1_documentation.zip from the above location. It includes full JavaBridge.war with documentation and sources.

Put the JavaBridgeTemplate621.war into your Tomcat's webapps directory. To test for excecution of Java code put into webapps/JavaBridgeTemplate62/getjavaproperties.php:

<?php
require_once("java/Java.inc");
echo str_replace(",","<br/>",java("java.lang.System")->getProperties());
?>

To use system PHP, modify webapps/JavaBridgeTemplate62/WEB-INF/web.xml and make sure PhpCGIServlet has prefer_system_php_exec parameter set to On:

<servlet>                                                                         <servlet-name>PhpCGIServlet</servlet-name>
<servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>           
<load-on-startup>0</load-on-startup>
<init-param>                                                                     
  <param-name>prefer_system_php_exec</param-name>
  <param-value>On</param-value>
</init-param>
<init-param>
  <param-name>php_include_java</param-name>
  <param-value>Off</param-value>
</init-param>
</servlet>

With the above setting you don't need to populate WEB-INF/cgi/amd64-linux and WEB-INF/cgi/amd64-linux/ext with copies of your php-cgi and php modules.

Check the following URLs (you should replace username.jvmhost.net alias with your domain if it is hosted on our server):

NOTE: In case you want to use URLs without port number, make sure your proxy module is set to mod_proxy_ajp_. You can check and change it with our custom Java Control Panel (JCP) where you can also see YOURTOMCATHTTPPORT.

With mod_proxy_ajp_ set you can also verify the following:

Optional steps

Rename the webapps/JavaBridgeTemplate621 directory, for example to webapps/drupal, and download your PHP application to this directory. Access the application in your browser e.g. http://username.jvmhost.net/drupal, and continue installation as usual.

If you want all your web applications to have the ability to use PHP-Java bridge, install it Tomcat wide. See instructions in PHP-Java bridge FAQ.

If you only want to call Java with a jar contained application from a PHP script see also How to call Java executable from PHP?. See you in a next Java hosting related article by JVM Host soon!