I am pretty new to Java for Web and my question is if I can use Java and PHP together? I know I can use them together, let's rephrase the question, is there a way to access them at the same port?

Java uses Tomcat, can PHP use tomcat also?


3 Answers 3


Tomcat does not do PHP, and (unless something happend in the recent past) there's no "mixed" application server that supports java and php all at once. However what you can do is have an Apache server installed and configured for PHP. This way you get a PHP application server configured on some port. Then you configure this Apache to redirect certain URL patterns to a Tomcat server (like all url with prefix /java/app/* go to Tomcat, all others are treated by Apache directly). Now effectively if you deploy web applications done in PHP in the Apache server and Java web apps in the Tomcat server, they will all "answer" to the same port.

  • "there's no "mixed" application server that supports java and php all at once". Ah, but there is! Quercus: caucho.com/resin-3.0/quercus
    – HTTP500
    Oct 8, 2011 at 12:28
  • Couldn't you use Java as a cgi program? It won't let you deploy Tomcat-style WebApps but it would let you write server-side applications in Java. Oct 12, 2011 at 14:38
  • @jasondbecker: Quercus goes way astray from the standards of both PHP and Java to do something that, at least in the context of this question, can beachieved a lot simpler. Oct 12, 2011 at 15:12
  • @DermotWilliams: yes you could, but good God why you'd want to do that? You can replace Tomcat with a lighter application server like Jetty if you don't need all the APIs, or you can even roll your own using only Java SE and the included HttpServer library (tho that's just for JDK 1.6 and above). CGI engines do A LOT of overhead (like running each instance in a separate process instead of multithreading in the same process) to basically compensate for stuff that was needed 15 years ago (like coding errors that would freeze an instance down) but are no longer an issue. Oct 12, 2011 at 15:16
  • @AndreiBodnarescu I never said it was a good suggestion :P Oct 12, 2011 at 18:16

As an adjunct to Andrei's suggestion, install a standard Apache+PHP combo listening on [your-ip]:80 and/or [your-ip]:443 and then install Tomcat, listening on localhost:8080. Then just do this in the normal Apache config:

<Location /myjavapps>
    order deny,allow
    allow from all
    ProxyPass http://localhost:8080/
    ProxyPassReverse http://localhost:8080/
  • Is Andrei's answer here anymore? What was it?
    – cellepo
    Sep 17, 2015 at 20:24

You can use mod_jk or proxy_pass within your apache config to interact with your tomcat application on port 80


ProxyPass /servlets ajp://localhost:8009/servlets ProxyPassReverse /servlets ajp://localhost:8009/servlets

This would mean /servlets - apache is sending requests to tomcat

PHP is a compiled module and is defined to where you want it to be within your virtual host

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .