PHP with Tomcat
Maybe some web developers are a bit confused about this title. Why should I run by PHP programs with Apache Tomcat web server?
At first I want to explain the initial situation. Normally we use the Apache web server as HTTP server. With this you can run static content (like html, htm and so on) or you can run different script languages (like PHP, Perl, …). When we need Java support, we can install Tomcat on top of Apache. Usually the Tomcat server communicates on a different Port than the Apache web server (usually Port 8081). With that configuration we can display our JSP, JSF and Servlets with domainname:8081.
The problem was, that my homepage was built with Java. This application should be displayed without the need to use the Tomcat port. But I have also different PHP applications, which also should be used with my domain. After some investigation I found a pretty good solution. If you want to see this in action, check my Homepage.
We assume, that the Apache web server is correctly installed and runs. You can find a tutorial for this at my article ‘The Apache web server‘.
There are some steps that have to be done:
- install Tomcat
For example with this command: apt-get install tomcat7 tomcat7-admin
- activate mod_proxy
Mod_Proxy is an apache module and routes requests to other web servers or addresses. You can activate it with:
- configure Mod_proxy. Before this you may restart your Apache.
You can define the routing for requests at the http.conf file. For our example we route it to another port.
12ProxyPass / http://localhost:8081/ProxyPassReverse / http://localhost:8081/
- configure Tomcat
The last step is to configure our Tomcat web server. We change the server.xml file and add following lines (please replace domainname with your Domain):
1<em><Connector port=8081 protocol=“HTTP/1.1“ connectionTimeout=“20000“ proxyName=“domainname“ URIEncoding=“UTF-8“ proxyPort=“80“/></em>
That’s it. Now every request is routed to Tomcat.
Deploy your PHP program with Tomcat
Now you have no possibility to send requests to your Apache web server, because all requests are routed to Tomcat. This means we need to enable Tomcat to deploy PHP applications. This is by far simpler than it sounds:
- download JavaBridgeTemplate621.war and deploy it with Tomcat manager.
Tomcat creates a directory for each project (*.war). You can find these directories in the webapps directory of Tomcat.
- simply copy your PHP program to this directory
you can also rename this directory to fits your needs.
On a new installed Tomcat there is already an index site. This site can be found in the Tomcat manager and is called /. In the webapps folder you can find it in the ROOT directory. You can easily change this index site. You only have to delete the / web application in the manager or the ROOT directory. If you now want to deploy a ned Java application as index site, you have do the following: simply rename your *.war file to ROOT.war and deploy it. Now the site index is you Java program. If you want to have a PHP index program, you simply need to install it with the JavaBridge and then rename the directory to ROOT.
- all sites are now manageable by the Tomcat manager
- you have an overview of the whole server with the Tomcat manager
- you loose the features of an simple Apache
If you really need that you can change the request routing so that only certain requests are rooted
- deploy an application with help of the Java Bridge is a little bit inconvenient
What do you think? Who else use that technique? Do you know other Pros or Cons?