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.
ProxyPass / 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):
<Connector port=8081 protocol=“HTTP/1.1“ connectionTimeout=“20000“ proxyName=“domainname“ URIEncoding=“UTF-8“ proxyPort=“80“/>
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?
Hi, Got JavaBridge working fine on my laptop, but when moving to Raspberry Pi it needs the php-cgi put in the WEB-APP/cgi/arm-linux folder. I copied one across from another Pi running php – but could not get it working.
My motivation is to run PHP on my Raspberry PI that is already serving servlets through Apache Tomcat, so allocation of the limited memory is all done through Tomcat.
Wow, interesting idea. I have never tried JavaBridge on my Pi, because Tomcat uses a lot of memory! Sounds as if it works 😉 Do you have no memory problems after some days? I discovered many memory issues with Java applications on my server so I moved completely away from Java.
To answer your question…do you have any additional information? I think it would be a good idea to check your logs for PHP error messages. I never ran into this issue. Isn’t it possible to install php-cgi with your packet manager?
Hi, It is a very good post. I have something similar problem.
I have a dashboard application written in PHP which now runs on a Apache HTTP web server. Thia application shows charts. I have another web application (JSP,Servlet) which runs on a Tomcat server. Now if I want to embed the pages of the dashboard application in the 2nd application which runs in Tomcat, what should be the correct way to do that?
I am thinking about the below two approaches –
1 – Run the two applications on two different servers as they are running now. Embed the page of the page of the dashboard app by using it’s url.
2 – Shift the PHP application from Apache to Tomcat and then internally do some tweaking so that it can be embedded to the JSP pages.
Hola! I’ve been reading your website for a while now and finally got the bravery
to go ahead and give you a shout out from Austin
Texas! Just wanted to say keep up the fantastic work!