使用 tomcat 运行 php 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/779246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a php app using tomcat?
提问by GBa
Is it possible to run a PHP app using tomcat?
是否可以使用 tomcat 运行 PHP 应用程序?
Before you tell me to just use httpd, I already have a Java application running on my webserver at host/myapp. Now I want to install RoundCube at host/roundcube.
在您告诉我只使用 httpd 之前,我已经在我的网络服务器上运行了一个 Java 应用程序,地址为host/myapp. 现在我想在 host/roundcube 上安装 RoundCube。
One is PHP and one is Java. I keep seeing offhand references saying this is possible but no real instructions. I do not want to put them on separate ports, I need the standard HTTP and HTTPS ports.
一种是 PHP,一种是 Java。我一直看到临时引用说这是可能的,但没有真正的说明。我不想将它们放在单独的端口上,我需要标准的 HTTP 和 HTTPS 端口。
Does anyone have any resources for this?
有没有人有这方面的资源?
回答by Saunik Singh
Yes it is Possible Will Den. we can run PHP code in tomcat server using it's own port number localhost:8080
是的,它是可能的Will Den。我们可以使用自己的端口号 localhost:8080 在 tomcat 服务器中运行 PHP 代码
here I'm writing some step which is so much useful for you.
在这里,我正在编写一些对您非常有用的步骤。
How to install or run PHP on Tomcat 6 in windows
如何在 windows 中的 Tomcat 6 上安装或运行 PHP
download and unzip PHP 5 to a directory,
c:\php-5.2.6-Win32- php-5.2.9-2-Win32.zip Downloaddownload PECL 5.2.5 Win32binaries - PECL 5.2.5 Win32 Download
rename
php.ini-disttophp.iniinc:\php-5.2.6-Win32Uncomment or add the line (remove semi-colon at the beginning) in
php.ini:;extension=php_java.dllcopy
php5servlet.dllfrom PECL 5.2.5 toc:\php-5.2.6-Win32copy
php_java.dllfrom PECL 5.2.5toc:\php-5.2.6-Win32\extcopy
php_java.jarfrom PECL 5.2.5totomcat\libcreate a directory named
"php"(or what ever u like) intomcat\webappsdirectorycopy
phpsrvlt.jarfrom PECL 5.2.5totomcat\webapps\php\WEB-INF\libUnjar or unzip
phpsrvlt.jarfor unzip use winrar or winzip for unjar use :jar xfv phpsrvlt.jarchange both
net\php\reflect.propertiesandnet\php\servlet.propertiestolibrary=php5servletRecreate the jar file -> jar cvf php5srvlt.jar net/php/.PS: if the jar file doesnt run you have to add the Path to system variables for me I added
C:\Program Files\Java\jdk1.6.0\bin; to System variables/Pathcreate
web.xmlintomcat\webapps\php\WEB-INFwith this content:<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <servlet> <servlet-name>php</servlet-name> <servlet-class>net.php.servlet</servlet-class> </servlet> <servlet> <servlet-name>php-formatter</servlet-name> <servlet-class>net.php.formatter</servlet-class> </servlet> <servlet-mapping> <servlet-name>php</servlet-name> <url-pattern>*.php</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>php-formatter</servlet-name> <url-pattern>*.phps</url-pattern> </servlet-mapping> </web-app>Add PHP path(
c:\php-5.2.6-Win32) to your System or User Path in Windows enironment (Hint: Right-click and select Properties from My Computercreate
test.phpfor testing undertomcat\webapps\phplikeRestart tomcat
browse
localhost:8080/php/test.php
下载并解压 PHP 5 到一个目录,
c:\php-5.2.6-Win32- php-5.2.9-2-Win32.zip 下载下载PECL 5.2.5 Win32二进制文件 - PECL 5.2.5 Win32 下载
重命名
php.ini-dist以php.ini在c:\php-5.2.6-Win32取消注释或添加以下行(删除开头的分号)
php.ini:;extension=php_java.dllphp5servlet.dll从 PECL 5.2.5复制到c:\php-5.2.6-Win32php_java.dll从PECL 5.2.5复制到c:\php-5.2.6-Win32\extphp_java.jar从PECL 5.2.5复制到tomcat\lib在目录中创建一个名为
"php"(或您喜欢的任何名称)的tomcat\webapps目录phpsrvlt.jar从PECL 5.2.5复制到tomcat\webapps\php\WEB-INF\libUnjar 或 unzip
phpsrvlt.jar解压使用 winrar 或 winzip 解压使用:jar xfv phpsrvlt.jar同时改变
net\php\reflect.properties和net\php\servlet.properties对library=php5servlet重新创建 jar 文件 -> jar cvf php5srvlt.jar net/php/ 。PS:如果jar文件没有运行你必须为我添加系统变量的路径我添加
C:\Program Files\Java\jdk1.6.0\bin; to System variables/Path建立
web.xml在tomcat\webapps\php\WEB-INF这个内容:<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <servlet> <servlet-name>php</servlet-name> <servlet-class>net.php.servlet</servlet-class> </servlet> <servlet> <servlet-name>php-formatter</servlet-name> <servlet-class>net.php.formatter</servlet-class> </servlet> <servlet-mapping> <servlet-name>php</servlet-name> <url-pattern>*.php</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>php-formatter</servlet-name> <url-pattern>*.phps</url-pattern> </servlet-mapping> </web-app>将 PHP 路径(
c:\php-5.2.6-Win32)添加到 Windows 环境中的系统或用户路径(提示:右键单击并从我的电脑中选择属性创建
test.php用于测试下tomcat\webapps\php喜欢重启tomcat
浏览
localhost:8080/php/test.php
回答by Arjan
It's quite common to run Tomcat behind Apache. In Apache you can then direct certain URLs to Tomcat, and have Apache/PHP handle the others (including the static images).
在 Apache 后面运行 Tomcat 是很常见的。在 Apache 中,您可以将某些 URL 定向到 Tomcat,并让 Apache/PHP 处理其他 URL(包括静态图像)。
(On Unix, Tomcat itself cannot securely made to run on port 80, while Apache can. Tomcat, being a Java process, would be required to run as root, while Apache will switch to non-root privileges as soon as port 80 has been claimed. So, running Apache on port 80 and have it redirect some or all requests to Tomcat, is quite common on Unix.)
(在 Unix 上,Tomcat 本身不能安全地在端口 80 上运行,而 Apache 可以。Tomcat 是一个 Java 进程,需要以 root 身份运行,而 Apache 将在端口 80 上切换到非 root 权限声称。因此,在端口 80 上运行 Apache 并将部分或所有请求重定向到 Tomcat,这在 Unix 上很常见。)
回答by Will Hartung
回答by phil
A tad late, but here goes.
有点晚了,但是来了。
How about http://wiki.apache.org/tomcat/UsingPhpif you just want to run the real php on tomcat.
如果您只想在 tomcat 上运行真正的 php,那么http://wiki.apache.org/tomcat/UsingPhp怎么样。
Concerning running tomcat on port 80 there's always jsvc, just google jsvc + tomcat.
关于在端口 80 上运行 tomcat,总是有 jsvc,只是 google jsvc + tomcat。
回答by lwpro2
tomcat is designed as JSP servlet container. Apache is designed PHP web server. Use apache as web server, responding for PHP request, and direct JSP servlet request to tomcat container. should be better implementation.
tomcat 被设计为 JSP servlet 容器。Apache 是专门设计的 PHP Web 服务器。使用apache作为web服务器,响应PHP请求,将JSP servlet请求定向到tomcat容器。应该更好的执行。
回答by mithil-studytrails
回答by Michael Kern
- Make sure you have php installed on your server
- Find the latest release of php-java-bridge off of sourceforge
- From the exploded directory on Sourceforge, download
php-servlet.jarandJavaBridge.jar - Place those jar files into
webapp/WEB-INF/libfolder of your project - Edit webapp/WEB-INF/web.xml to look like:
- 确保您的服务器上安装了 php
- 在sourceforge 上找到最新版本的 php-java-bridge
- 从 Sourceforge 上的分解目录中,下载
php-servlet.jar并JavaBridge.jar - 将这些 jar 文件放入
webapp/WEB-INF/lib项目的文件夹中 - 编辑 webapp/WEB-INF/web.xml 看起来像:
ok
好的
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>PhpCGIFilter</filter-name>
<filter-class>php.java.servlet.PhpCGIFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PhpCGIFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- the following adds the JSR223 listener. Remove it if you don't want to use the JSR223 API -->
<listener>
<listener-class>php.java.servlet.ContextLoaderListener</listener-class>
</listener>
<!-- the back end for external (console, Apache/IIS-) PHP scripts; remove it if you don't need this -->
<servlet>
<servlet-name>PhpJavaServlet</servlet-name>
<servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
</servlet>
<!-- runs PHP scripts in this web app; remove it if you don't need this -->
<servlet>
<servlet-name>PhpCGIServlet</servlet-name>
<servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PhpJavaServlet</servlet-name>
<url-pattern>*.phpjavabridge</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PhpCGIServlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>
</web-app>
You may have other content inside this file, just make sure you have added everything between the web-app tag.
您可能在此文件中包含其他内容,只需确保在 web-app 标记之间添加了所有内容。
- Add your php files to the webapp directory
- 将你的 php 文件添加到 webapp 目录
You can do other special things with this as well. You cal learn more about it here: http://php-java-bridge.sourceforge.net/pjb/how_it_works.php
你也可以用它做其他特殊的事情。你可以在这里了解更多信息:http: //php-java-bridge.sourceforge.net/pjb/how_it_works.php

