Published on Jun 8 2022 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 bridge 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 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:

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:


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 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:

Possible errors

Sometimes php-cgi may not be in PATH on your server. You will see:

javax.servlet.ServletException: Could not connect to server Connection refused (Connection refused) PHP not found. Please install php-cgi. PHP test command was: [php-cgi, -v] 

Contact support to get correct path and assuming the php-cgi is at /opt/cpanel/ea-php56/root/usr/bin/php-cgi then you can:

  1. add to your JAVA_OPTS (e.g. in ~/.bashrc)


  1. Add /opt/cpanel/ea-php56/root/usr/bin to PATH environment variable in ~/.bashrc.

Reread .bashrc if needed (case #2) then restart Tomcat and the error should be gone.

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., 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!