Run a PHP App Using Tomcat

PHP on Tomcat 8

Your best bet to run PHP on top of Tomcat is probably to download and install the Quercus WAR file. Quercus is a Java-based implementation of PHP 5, and it can be found at http://quercus.caucho.com/.

How to run PHP and Tomcat on same server environment?

You can do that with mod_jk:

1) Enable module "mod_jk" in your Apache web servers httpd.conf. Uncomment this line, by removing the leading hash:

LoadModule jk_module modules/mod_jk.so

If you are on Linux type:

sudo apt-get install libapache2-mod-jk
sudo a2enmod jk

2) Edit [TOMCAT_DIR]/conf/server.xml. Add a "jvmRoute" attribute to the "engine" element:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat">

Uncomment the AJP connector (the http connector may be disabled):

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

3) Create a file "workers.properties", next to "httpd.conf". Add this content and set right ip/port:

worker.list=tomcat

worker.tomcat.type=ajp13
worker.tomcat.host=127.0.0.1
#This is the port from the AJP connector, not HTTP!
worker.tomcat.port=8009
worker.tomcat.lbfactor=10

4) Add this mapping at the end of httpd.conf and replace [PATH_TO_DIR] by the absolute path:

<IfModule jk_module>

JkWorkersFile [PATH_TO_DIR]\workers.properties
JkLogFile [PATH_TO_DIR]\mod_jk.log
JkLogLevel INFO
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

SetEnvIf Request_URI "/error/*" no-jk
SetEnvIf Request_URI "/blog*" no-jk

JkMount / tomcat
JkMount /* tomcat

</IfModule>

5) Start Tomcat and restart Httpd.

apache httpd handling tomcat and php at same time

You can for sure using different ports: Can a single Apache server handle both Tomcat and PHP?

Honestly, don't know if you can listen on the same port tho.

EDIT

Yes, you can: https://sites.google.com/a/ci2s.com.ar/wiki/technics/how-to-run-apache-httpd-and-tomcat-on-port-80-using-mod-proxy

Apache web server or Tomcat for java war and php

If your back-end uses the Servlet API, you require a servlet server and Tomcat is a common choice. Even if Tomcat provides CGIServlet, I believe nobody uses it on production servers to run PHP scripts. So you need at least two servers.

I would extend it to three servers and run:

  • the back-end on Tomcat (bound to localhost),
  • the front-end on a PHP FastCGI server like PHP-FPM (bound to a Unix socket),
  • a proxy server like NGINX (lighter than Apache2) to connect to PHP-FPM. It does not have to proxy Tomcat, since everything is on the same machine.


Related Topics



Leave a reply



Submit