简体   繁体   English

允许Glassfish和PHP在使用Apache的同一服务器中协同工作

[英]Allow Glassfish and PHP to work together in the same server using Apache

It's possible make a bridge from Java to php file? 有可能建立一个从Java到php文件的桥梁吗?

I've got an application written in Java and I need execute http://piwik.org/ that is written in PHP. 我有一个用Java编写的应用程序,我需要执行用PHP编写的http://piwik.org/ In the server I have PHP running but I cannot access from the browser to the php directory because all incoming traffic is redirected by apache to glassfish Application server. 在服务器上我运行PHP,但我无法从浏览器访问到php目录,因为所有传入的流量都被apache重定向到glassfish应用服务器。

So my idea is to use Java servlet to execute php files with: 所以我的想法是使用Java servlet执行php文件:

Runtime.getRuntime().exec("php /path/to/file/file.php");

Then write the PHP output as java servlet response. 然后将PHP输出编写为java servlet响应。

The only problems to accomplish this are: 实现这一目标的唯一问题是:

How can I execute PHP cli that act like a browser? 如何执行像浏览器一样的PHP cli?

Which parameters I need to pass to PHP to allow PHP to read or write cookie and session? 我需要将哪些参数传递给PHP以允许PHP读取或写入cookie和会话?

When you execute a php script from the command line the GET / POST / SESSION / COOKIE variables are meaningless. 当您从命令行执行php脚本时, GET / POST / SESSION / COOKIE变量毫无意义。 When your file.php send a cookie there is no browser to receive it, save it and use it for subsequent requests. 当您的file.php发送cookie时,没有浏览器可以接收它,保存它并将其用于后续请求。

What you can do is use CGI SAPI, so that all HTTP_* variables will be usable, and the headers will be written to the output. 您可以做的是使用CGI SAPI,以便所有HTTP_*变量都可用,并且标题将被写入输出。

The php-cgi binary implements the CGI interface, which allows you to pass cookies on the command line like this: php-cgi二进制文件实现了CGI接口,允许您在命令行上传递cookie,如下所示:

HTTP_COOKIE='PHPSESSID=XXXX' php-cgi /path/to/file/file.php

Where XXXX can be the session id of an user. 其中XXXX可以是用户的会话ID。 You can read the cookie analyzing the headers on the output. 您可以阅读分析输出标题的cookie。

If you're using Apache anyway to proxy the traffic, I'd just exclude all traffic to Piwik and serve those directly from the file system / mod_php / php-fpm / whatever you normally use. 如果您仍在使用Apache来代理流量,我只会将所有流量排除在Piwik之外,直接从文件系统/ mod_php / php-fpm /中提供这些流量。

You could also use php-cgi and pass the appropriate environment variables, but that complicates a lot of stuff, like you'd have to proxy the response back to the browser, too. 你也可以使用php-cgi并传递适当的环境变量,但这会使很多东西变得复杂,比如你必须将响应代理回浏览器。 Apache has already support for this, so don't implement another proxy in your application, do it directly in Apache. Apache已经支持这一点,所以不要在你的应用程序中实现另一个代理,直接在Apache中执行。

You can exclude a directory from being proxied: 您可以从代理中排除目录:

ProxyPass /piwik ! 
ProxyPass / 127.0.0.1:8080 
ProxyPassReverse / 127.0.0.1:8080

Thanks to the idea of kelunik to use Apache to exclude the traffic and the help By Federico I've resolved the problem using this Apache rule: 感谢kelunik使用Apache排除流量和帮助的想法Federico我已经使用这个Apache规则解决了这个问题:

ProxyPass /phpdir ! 
ProxyPass / 127.0.0.1:8080 
ProxyPassReverse / 127.0.0.1:8080

Apache redirect all request to port 8080, except the folder /phpdir . Apache将所有请求重定向到端口8080,文件夹/ phpdir除外 Into the Apache document root I've created the directory phpdir that is the new root where the PHP applications will be run. 在Apache文档根目录中,我创建了目录phpdir ,它是运行PHP应用程序的新根目录。

That's it 而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM