Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Oct 8 '11 at 11:23

This question came from our site for professional and enthusiast programmers.

3 Answers 3

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/
</Location>
share|improve this answer

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

i.e.

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

share|improve this answer

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.

share|improve this answer
    
"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 '11 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. –  Dermot Williams Oct 12 '11 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. –  Shivan Dragon Oct 12 '11 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. –  Shivan Dragon Oct 12 '11 at 15:16
    
@AndreiBodnarescu I never said it was a good suggestion :P –  Dermot Williams Oct 12 '11 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.