3

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?

david_m_1
  • 61
  • 1
  • 6

4 Answers4

3

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

david_m_1
  • 61
  • 1
  • 6
2

Did you add this code in your php.ini?

allow_url_include = On
Francis Gonzales
  • 486
  • 7
  • 21
0

In the interest of giving others an extra trouble shooting option that stumble across this -- this worked for me:

sudo setsebool -P httpd_can_network_connect_db=1

This enables SELinux to allow apache to connect to your database. I was getting errors like this:

httpd error log:

java.sql.SQLNonTransientConnectionException: .... Permission Denied. Caused by: java.net.SocketException: Permission denied

Jawa
  • 2,336
  • 6
  • 34
  • 39
Ramjet
  • 43
  • 1
  • 7
0

I had some problems with the PHP Java Bridge too while setting it up. Looking in my setup, though, I see that I have JAVA_HOSTS defined as 127.0.0.1:8087.

If your browser is able to see something on that port but the bridge connection still doesn't work, it could be that the listening app is not listening correctly.

If you go to php-java-bridge.sourceforge.net/pjb/desktop-apps.php and follow the bolded sections of code in the 'Add the PHP/Java Bridge library to your Java application' you can see that you need to:

  • add the JavaBridge.jar file to your project
  • add public static final String JAVABRIDGE_PORT="8087"; to your class
  • add static final php.java.bridge.JavaBridgeRunner runner = php.java.bridge.JavaBridgeRunner.getInstance(JAVABRIDGE_PORT); to your class
  • in your main function, call runner.waitFor();

That will basically set your application up to listen.

Narcissus
  • 3,144
  • 3
  • 27
  • 40
  • Thanks for the quick reply! But no joy I'm afraid - still getting the permission denied error. Its obviously finding the Java.inc file but then something appears to be blocking it from running the JavaBridge... – david_m_1 Nov 22 '11 at 16:39
  • So when you send your browser to that IP address / port number, do you get anything? If I'm not running my Java app on the other side (the app that calls 'runner.waitFor();' where runner is an instance of php.java.bridge.JavaBridgeRunner) I get a timeout. If the app is running however, I seem to get a directory listing. Sorry if you've already answered that! – Narcissus Nov 22 '11 at 16:44
  • Yes - that's why its all a bit odd. I can access JavaBridge (and all the examples) fine through 127.0.0.1:8080/JavaBridge... – david_m_1 Nov 22 '11 at 16:57
  • If you can get the examples running with the 'sample' project, I'd check that your Java app is running the JavaBridgeRunner properly... can you check that it's listening on port 8080 at all? – Narcissus Nov 22 '11 at 17:31
  • Ah! That's where my lack of Java comes in! I guess I'd assumed that Tomcat was handling all the listening on 8080 and was running all the necessary servlets (is that what JavaBrisgeRunner is?). I don't know how to check that JavaBridgeRunner is running and if so, what ports it is listening on. Thanks for your continued help... – david_m_1 Nov 22 '11 at 17:51
  • No problems, I'm not much at Java myself :) The comment field is pretty tiny here, but suffice to say if you go to http://php-java-bridge.sourceforge.net/pjb/desktop-apps.php and follow the bolded sections of code in the 'Add the PHP/Java Bridge library to your Java application' section you should get there. Basically you set your Java application up as a little looping listener, waiting for requests. Hopefully that will get you somewhere! – Narcissus Nov 22 '11 at 18:37
  • How did you solve this? Im facing the same error but when calling from an apache web server to a tomcat server where I have installed the javabridge – Rodolfo Velasco Jan 30 '15 at 19:33