使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:49:14  来源:igfitidea点击:

Run a php app using tomcat?

phptomcatapache

提问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

  1. download and unzip PHP 5 to a directory, c:\php-5.2.6-Win32- php-5.2.9-2-Win32.zip Download

  2. download PECL 5.2.5 Win32binaries - PECL 5.2.5 Win32 Download

  3. rename php.ini-distto php.iniin c:\php-5.2.6-Win32

  4. Uncomment or add the line (remove semi-colon at the beginning) in php.ini: ;extension=php_java.dll

  5. copy php5servlet.dllfrom PECL 5.2.5 to c:\php-5.2.6-Win32

  6. copy php_java.dllfrom PECL 5.2.5to c:\php-5.2.6-Win32\ext

  7. copy php_java.jarfrom PECL 5.2.5to tomcat\lib

  8. create a directory named "php"(or what ever u like) in tomcat\webappsdirectory

  9. copy phpsrvlt.jarfrom PECL 5.2.5to tomcat\webapps\php\WEB-INF\lib

  10. Unjar or unzip phpsrvlt.jarfor unzip use winrar or winzip for unjar use : jar xfv phpsrvlt.jar

  11. change both net\php\reflect.propertiesand net\php\servlet.propertiesto library=php5servlet

  12. Recreate 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/Path

  13. create web.xmlin tomcat\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>
    
  14. 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 Computer

  15. create test.phpfor testing under tomcat\webapps\phplike

  16. Restart tomcat

  17. browse localhost:8080/php/test.php

  1. 下载并解压 PHP 5 到一个目录,c:\php-5.2.6-Win32- php-5.2.9-2-Win32.zip 下载

  2. 下载PECL 5.2.5 Win32二进制文件 - PECL 5.2.5 Win32 下载

  3. 重命名php.ini-distphp.inic:\php-5.2.6-Win32

  4. 取消注释或添加以下行(删除开头的分号)php.ini;extension=php_java.dll

  5. php5servlet.dll从 PECL 5.2.5复制到c:\php-5.2.6-Win32

  6. php_java.dllPECL 5.2.5复制到c:\php-5.2.6-Win32\ext

  7. php_java.jarPECL 5.2.5复制到tomcat\lib

  8. 在目录中创建一个名为"php"(或您喜欢的任何名称)的tomcat\webapps目录

  9. phpsrvlt.jarPECL 5.2.5复制到tomcat\webapps\php\WEB-INF\lib

  10. Unjar 或 unzipphpsrvlt.jar解压使用 winrar 或 winzip 解压使用:jar xfv phpsrvlt.jar

  11. 同时改变net\php\reflect.propertiesnet\php\servlet.propertieslibrary=php5servlet

  12. 重新创建 jar 文件 -> jar cvf php5srvlt.jar net/php/ PS:如果jar文件没有运行你必须为我添加系统变量的路径我添加C:\Program Files\Java\jdk1.6.0\bin; to System variables/Path

  13. 建立web.xmltomcat\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>
    
  14. 将 PHP 路径( c:\php-5.2.6-Win32)添加到 Windows 环境中的系统或用户路径(提示:右键单击并从我的电脑中选择属性

  15. 创建test.php用于测试下tomcat\webapps\php喜欢

  16. 重启tomcat

  17. 浏览 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 Equistatic

Caucho Quercuscan run PHP code on the jvm.

Caucho Quercus可以在 jvm 上运行 PHP 代码。

回答by Will Hartung

There this PHP/Javabridge. This is basically running PHP via FastCGI. I have not used it myself.

有这个PHP/Java桥。这基本上是通过 FastCGI 运行 PHP。我自己没用过。

回答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

If anyone's still looking - Quercushas a war that allows to run PHP scripts in apache tomcat or glassfish. For a step by step guide look at thisarticle

如果有人还在寻找 - Quercus有一个允许在 apache tomcat 或 glassfish 中运行 PHP 脚本的War。对于在一步一步地指导一下这个文章

回答by Michael Kern

  1. Make sure you have php installed on your server
  2. Find the latest release of php-java-bridge off of sourceforge
  3. From the exploded directory on Sourceforge, download php-servlet.jarand JavaBridge.jar
  4. Place those jar files into webapp/WEB-INF/libfolder of your project
  5. Edit webapp/WEB-INF/web.xml to look like:
  1. 确保您的服务器上安装了 php
  2. sourceforge 上找到最新版本的 php-java-bridge
  3. 从 Sourceforge 上的分解目录中,下载php-servlet.jarJavaBridge.jar
  4. 将这些 jar 文件放入webapp/WEB-INF/lib项目的文件夹中
  5. 编辑 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 标记之间添加了所有内容。

  1. Add your php files to the webapp directory
  1. 将你的 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