david_m_1
david_m_1

Reputation: 61

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:

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?

Upvotes: 3

Views: 6992

Answers (4)

Ramjet
Ramjet

Reputation: 43

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

Upvotes: 0

Francis Gonzales
Francis Gonzales

Reputation: 495

Did you add this code in your php.ini?

allow_url_include = On

Upvotes: 2

david_m_1
david_m_1

Reputation: 61

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

Upvotes: 3

Narcissus
Narcissus

Reputation: 3194

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.

Upvotes: 0

Related Questions