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?

4

4 回答 4

3

Narcissus - 非常感谢您的意见,但事实证明这是 SELinux 的权限问题,导致 PHP 无法调用 JavaBridge。

我通过以 root 用户身份发出以下命令来关闭 SELinux,暂时解决了这个问题:

setenforce 0

我需要找到一个更令人满意的长期解决方案来更改 SELinux 权限以允许 PHP 和 Javabridge 之间的交互,但这是一个不同的问题......

于 2011-11-23T09:00:17.310 回答
2

您是否在 php.ini 中添加了此代码?

allow_url_include = On
于 2012-09-17T15:08:51.223 回答
0

为了给其他人一个偶然发现的额外故障排除选项——这对我有用:

sudo setsebool -P httpd_can_network_connect_db=1

这使 SELinux 允许 apache 连接到您的数据库。我收到这样的错误:

httpd错误日志:

java.sql.SQLNonTransientConnectionException: .... 权限被拒绝。引起:java.net.SocketException:权限被拒绝

于 2014-10-28T00:02:29.907 回答
0

在设置 PHP Java Bridge 时,我也遇到了一些问题。不过,查看我的设置,我发现我将 JAVA_HOSTS 定义为 127.0.0.1:8087。

如果您的浏览器能够在该端口上看到某些内容,但桥接仍然无法正常工作,则可能是侦听应用程序未正确侦听。

如果您转到 php-java-bridge.sourceforge.net/pjb/desktop-apps.php 并按照“将 PHP/Java Bridge 库添加到您的 Java 应用程序”中的粗体代码部分,您可以看到您需要:

  • 将 JavaBridge.jar 文件添加到您的项目中
  • 添加public static final String JAVABRIDGE_PORT="8087";到你的班级
  • 添加static final php.java.bridge.JavaBridgeRunner runner = php.java.bridge.JavaBridgeRunner.getInstance(JAVABRIDGE_PORT);到你的班级
  • 在您的主要功能中,调用runner.waitFor();

这基本上会将您的应用程序设置为监听。

于 2011-11-22T16:31:17.840 回答