Search code examples
phpphp-java-bridge

PHP JavaBridge permission error


I have an existing large(ish) PHP web app (using Apache and MySQL) which now needs to be able to call a Java based reporting engine. So, what I'm trying to achieve is the ability to access java classes from within the existing PHP app.

So far, on a new dev server, I have successfully installed the open source PHP-Javabridge project (http://php-java-bridge.sourceforge.net/pjb/index.php) and have it running under Tomcat (7.0.22) on a Fedora 15 box using port 8080. I can't use the Zend Javabridge because of hosting restrictions for the live system and unfortunately changing provider is not an option at the moment.

I also have Apache and PHP running on the dev box using port 80.

I can access the JavaBridge webapp in Tomcat and all the PHP examples work fine. However, I am running into a problem when trying to access the JavaBridge from within my existing application.

I am assuming that it should be possible for me to call the php 'java' function from within a script located in the web root for Apache (/var/www/html).

I have used the script supplied in the JavaBridge application as follows:

<?php
    require("http://127.0.0.1:8080/JavaBridge/java/Java.inc");
    echo java("java.lang.System")->getProperties();
?>

This produces the following errors in /etc/httpd/logs/error_log

[Tue Nov 22 15:01:08 2011] [error] [client ::1] PHP Warning: require_once(http://localhost:8080/JavaBridge/java/Java.inc): failed to open stream: Permission denied in /var/www/html/javatest.php on line 2

[Tue Nov 22 15:01:08 2011] [error] [client ::1] PHP Fatal error: require_once(): Failed opening required 'http://localhost:8080/JavaBridge/java/Java.inc' (include_path='.:/php/includes:/usr/share/apache-tomcat-7.0.22/webapps/JavaBridge') in /var/www/html/javatest.php on line 2

The other suggested script is: (note: I have a copy of Java.inc in /var/www/html)

<?php
    define("JAVA_HOSTS", "127.0.0.1:8080");
    define("JAVA_SERVLET", "/JavaBridge/servlet.phpjavabridge");
    require_once("./Java.inc");
    echo java("java.lang.System")->getProperties();
?>

This produces the following errors:

[Tue Nov 22 12:57:51 2011] [error] [client ::1] PHP Warning: fsockopen(): unable to connect to 127.0.0.1:8080 (Permission denied) in /usr/share/apache-tomcat-7.0.22/webapps/JavaBridge/java/Java.inc on line 994

[Tue Nov 22 12:57:51 2011] [error] [client ::1] PHP Fatal error: Uncaught Could not connect to the JEE server 127.0.0.1:8080. Please start it. Error message: Permission denied (13)\n\n thrown in /usr/share/apache-tomcat-7.0.22/webapps/JavaBridge/java/Java.inc on line 989

The steps I've taken to rule out problems are:

  • Turned off firewall on the server
  • Chmod'd everything to 777
  • Checked that Tomcat is running before and after I run the PHP script (I assume, possibly wrongly, that Tomcat is the JEE server referred to in the second error message?) 4). PHP ini file has no open_basedir restrictions, safe mode is not on and the allow_furl_open and allow_url_include options

I'm really stuck on this. No amount of Googling finds any similar specific problem.

I must say that I'm very unfamiliar with Java and may have got the wrong end of the stick on the JavaBridge insofar as it may simply not be possible to run the java function from within the /var/www/html location and that any PHP scripts must be run from within the Tomcat JavaBridge app.

I'm assuming that all the servlets are working but my lack of knowledge means I don't know to check that.

As this is on Fedora could it be connected to a SELinux permissions issue?


Solution

  • Narcissus - many thanks for your input, but it turned out to be a permissions issue with SELinux which was preventing PHP from making the call to the JavaBridge.

    I have temporarily solved the issue by shutting off SELinux by issuing the following command as root user:

    setenforce 0
    

    I need to find a more satisfactory long term term solution to alter SELinux permissions to allow the interaction between PHP and Javabridge but that is a different issue...