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 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):
- http://username.jvmhost.net:YOURTOMCATHTTPPORT/JavaBridgeTemplate621
- http://username.jvmhost.net:YOURTOMCATHTTPPORT/JavaBridgeTemplate621/test.php
- http://username.jvmhost.net:YOURTOMCATHTTPPORT/JavaBridgeTemplate621/getjavaproperties.php
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:
- http://username.jvmhost.net/JavaBridgeTemplate621
- http://username.jvmhost.net/JavaBridgeTemplate621/test.php
- http://username.jvmhost.net/JavaBridgeTemplate621/getjavaproperties.php
Possible errors
Sometimes php-cgi may not be in PATH on your server. You will see:
javax.servlet.ServletException: php.java.bridge.http.FCGIConnectException: Could not connect to server
java.io.IOException: java.net.ConnectException: Connection refused (Connection refused)
java.io.IOException: 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:
- add
-Dphp.java.bridge.php_exec=/opt/cpanel/ea-php56/root/usr/bin/php-cgi
to yourJAVA_OPTS
(e.g. in~/.bashrc
)
or
- Add
/opt/cpanel/ea-php56/root/usr/bin
toPATH
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. 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!