Java developers all know that tomcat is used to deploy java web projects. During this period, there was a project that required the same domain name and port as the PHP project. How to achieve this without using nginx? I learned that tomcat can support running php through Java Bridge.
Let’s try it too. Here are the detailed steps.
1. Environment preparation
Installed the php environment, installed the java virtual machine, and tomcat
The minimum configuration of these tools It is PHP 5.x, Java 6 or above, Tomcat 6 or above.
2. Configure tomcat (Recommended learning: PHP programming from entry to proficiency)
Integrate the JavaBridge of PHP/Java Bridge. jar, php-servlet.jar and php-script.jar are copied to the lib directory of tomcat;
Modify the web.xml file in the conf folder in the tomcat installation directory and add the following to the web-app tag Code;
<listener> <listener-class>php.java.servlet.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>PhpJavaServlet</servlet-name> <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class> </servlet> <servlet> <servlet-name>PhpCGIServlet</servlet-name> <servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class> <init-param> <param-name>prefer_system_php_exec</param-name> <param-value>On</param-value> </init-param> <init-param> <param-name>php_include_java</param-name> <param-value>Off</param-value> </init-param> </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>
After completing the above steps, restart tomcat, you can execute the php script under any project, but it cannot be run directly under webapps/, because the tomcat official website explains cgiPathPrefix as follows:
The CGI search path will start at the web application root directory + File.separator + this prefix.
When accessing the file index.php directly in the webapps directory
<?php phpinfo();?>
The above is the detailed content of Does tomcat support php?. For more information, please follow other related articles on the PHP Chinese website!