Running PHP scripts on Apache Tomcat has many benefits for developers who want to use its scalability and strong features. By combining PHP with this powerful Java-based web server, developers can create dynamic, efficient, and flexible web applications easily.
3. Testing PHP Scripts on Apache Tomcat.
Verify your setup by testing PHP scripts in the Apache Tomcat environment, ensuring everything runs smoothly.
Access the Official Apache Tomcat Website: Navigate to the Apache Tomcat homepage at https://tomcat.apache.org.
Download the Tomcat distribution: Navigate to the “Downloads” section
# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.76/bin/apache-tomcat-9.0.76.tar.gz Extract the distribution archive # tar -xvf apache-tomcat-9.0.76.tar.gz Start Tomcat # sh startup.sh
# wget https://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_7.2.1/JavaBridgeTemplate721.war/download
After downloading the JavaBridgeTemplatexx.war file, extract its contents and move the two essential JAR files, JavaBridge.jar and php-servlet.jar, into the lib directory of your Apache Tomcat instance.
<!-- PHP FOR TOMCAT --> <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>
=======================================================================
Go to the parent directory of your Tomcat installation and find the bin folder.
Ensure the catalina.sh file has execution permissions.
Place your PHP scripts in either of these directories:webapps/ROOT/
webapps/yourapplication/index.php
For example create “phpinfo.php” file in webapps/ROOT/
After creating the PHP file, restart Tomcat:
# ./shutdown.sh
# ./startup.sh

Running PHP scripts on Apache Tomcat combines the power of a robust Java server with PHP’s versatility, eliminating the need for separate environments. This setup simplifies resource management, reduces system complexity, and enables efficient deployment of dynamic and scalable web applications.