I have decided to blog about this, as I have had never ending problems in the past to try and get this configured. Here is the scenario; you have a project that requires hosting a single website in a single web domain, but you have a mix of PHP applications and Java (or in my instance, GRAILS) applications that you need to run. The way you do this, is configure both PHP and Java applications to run through an Apache front end. The problem is, that there is no good documentation to help you do this, so here I go!
In this example, I am going to show you how to run a static html site, plus a PHP application (we’ll use WordPress for this example) and a java .WAR applications all off the same instance of apache on the same server. Our site will look like this;
I am assuming that you have access to your own server, or virtual private server (I recommend NeoSurge hosting if you need one), and that you are running Ubuntu Server (Although my instructions are for Ubuntu, they are easily transferable to other falvours of Linux). Here is the process form start to end to get it setup;
I am using Ubuntu Server 9.04, and I have set up a ‘vanilla’ instance of the server running with none of the options configured (DNS etc). Now start the server & login with the userename and password elected on the install
Firstly we will install some tools that will come in handy later such as ssh (for terminal access), I use nano for text editing (which may not installed), but you may prefer to install vi or vim, and an ftp server
NB: I have included SSH installation, but with most VPS and hosted providers, this will be done for you (Otherwise, you wouldn’t be able to get into your server
You will only need to do this if you want to run a virtual machine in your own environment).
Install OpenSSH
To install openSSH, type the following in the command line (NB: you will be prompted to enter your password again to gain SuperUser access – which is what the sudo command is)
sudo apt-get install openssh-server
After the installation process, you will have openSSH on the server. Next we install FTP
Install FTP Server – vsftpd
sudo apt-get install vsftpd
To allow log on access for authenticated users, you need to edit /etc/vsftpd.conf
sudo nano /etc/vsftpd.conf
find the following two lines and uncomment them (they are near each other)
local_enable=YES
write_enable=YES
Now restart the ftp server so your new settings can be picked up
sudo /etc/init.d/vsftpd restart
Now you should be able to ftp, and ssh to the server so that you can make changes and upload files – which you will need to do when you start configuring mod-proxy
Next you will need to install all the software such as mysql, apache, PHP, tomcat etc. I will be just installing all the basic software, so I won’t go into the details of installing PHPMyAdmin if you require it. As there are lots of tutorials out there on how to do that.
Install mysql
sudo apt-get install mysql-server
This will take a while, but after its done, you will have MySql installed. Again, you will need to configure MySql to be able to create databases etc remotely, but as is, you can ssh into your machine and do what you need to do in the MySql command line.
Install apache, java and tomcat
Now you will install Apache, Java, Tomcat and Tomcat Admin application so that your web server and Java setup is complete, this can be done in a single command;
sudo apt-get install apache2 sun-java6-jdk tomcat6 tomcat6-admin
You will need to go through the license acceptance process, and depending on your internet link speed, the above may take a while.
Install php
Now that Apache is installed, you can go ahead and install the PHP extensions so that php code can be parsed and rendered.
sudo apt-get install php5
sudo /etc/init.d/apache2 restart
That’s it!!! All the software is now installed and its time to do some configuration.
First we are going to enable mod-proxy. Mod-proxy is the extension to apache that allows you to use apache as the proxy for web requests but then passes on the processing to tomcat (in our example). We are going to be using the AJP connector, but its pretty much the same process if you wanted to use the http connector or one of the other various adapters that apache provides.
Enable mod-proxy
sudo a2enmod proxy_ajp
After you enable the proxy, you now need to allow the proxying to occur. You do this by modifying your proxy.conf file.
sudo nano /etc/apache2/mods-enabled/proxy.conf
Find where it says “Order deny,allow”, and change the text underneath so it looks like the following
Order deny,allow
Allow from all
#Deny from all
Quite simply, you are now allowing all rules to be proxied, and turning off the Deny rules (you can obviously get more fine tuned here, and you will see lots of security warnings, which I recommend you listen to, but for the moment this will get you by.).
Allow AJP port by uncommenting the connector in Tomcat
sudo nano /etc/tomcat6/server.xml
and uncomment
<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
Add an admin and manager user for tomcat
sudo nano /etc/tomcat6/tomcat-users.xml
<role rolename=”admin”/>
<role rolename=”manager”/>
<user username=”yourUser” password=”aPassword” roles=”admin,manager”/>
Give tomcat some more memory (default is never enough) and disable tomcat’s security
sudo nano /etc/default/tomcat6
and uncomment and change the following
JAVA_OPTS=”-Djava.awt.headless=true -Xms128M -Xmx1280M -XX:MaxPermSize=256m”
TOMCAT6_SECURITY=no
Setup tomcat
So now you are going to setup tomcat so that you can run WARs in the tomcat Servlet Container.
sudo nano /etc/tomcat6/server.xml
First thing we are going to do is change the host setting. I do this by leaving the default host and creating a new one for our purposes. Add an another <Host/> node below the existing one in server.xml
<Host name=”ServletName” appBase=”webapps/ServletName”
unpackWARs=”true” autoDeploy=”true” xmlValidation=”false” xmlNamespaceAware=”false”>
</Host>
The important thing in the above node is the appBase. This should point to the webapps directory (unless you have created a new one), and then the directory that will contain the servlet container. Even though unpackWARs is set to true, I find this a bit strange and like to unpack my own WARs, so I would also create the directory and in the location that is going to hold the exploded war.
sudo mkdir /var/lib/tomcat6/webapps/ServletName
sudo chown tomcat6:tomcat6 /var/lib/tomcat6/webapps/ServletName
Uplaod the War to /var/lib/tomcat6/webapps
And unpack it (unzip, and make sure it has the same name as the directory under webapps)
OK, now configure the proxy settings for apache, back in the proxy.conf
sudo nano /etc/apache2/mods-enabled/proxy.conf
Turn On Proxy requests on and configure the proxying (you can put this anywhere in the file, but I like to put the following at the top of the file)
ProxyRequest On
ProxyPreserveHost On
ProxyPass /ServletName ajp://localhost:8009/ServletName
ProxyPassReverse /ServletName ajp://localhost:8009/ServletName
and at the end of the file
ProxyVia On
The important part of the above change is that you are configuring what requests to apache are going to be passed to tomcat for processing. Above I am telling any request to /ServletName will be passed through. you can also tell it to ignore certain directories as well. For example, one common use would be to route all traffic to tomcat, but ignore static files that you want to apache to process for performance reasons. To do this, you would do something like;
ProxyPass /css !
ProxyPass /images !
ProxyPass / ajp://localhost:8009/
Pretty self explanatory, the only thing to keep in mind is any directories you want to ignore need to come first.
Finally, restart apache and then tomcat
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/tomcat6 restart
That’s it – you are done!!!!
At the start of this post, I talked about configuring the following sites;
Well now that the configuration has been done – its pretty easy. To host your static site, just upload all your html, css, js & images etc into your root directory. You should find this in /var/www. To host your WordPress blog, its as easy as creating the directory /var/www/blog and uploading wordpress here.
PHP is all configured and you should be able to go through the standard installation process for WordPress by going to your url http://someurl.com./blog.
And finally the /quotation directory in my example is the directory that you need to use in the tutorial above where I have used ServletName.
Once you have done all this, and restarted tomcat and apache, all three sites; static, php and java should be working seamlessly….!!
Have Fun….
RSS feed for comments on this post. TrackBack URL
Configure Apache to run Tomcat and PHP…
I have decided to blog about this, as I have had never ending problems in the past to try and get this configured. Here is the scenario; you have a project that requires hosting a single website in a single web domain, but you have a mix of PHP applica…
[...] James has a new post looking at configuring Apache to run Tomcat and PHP together so you can have both Java and PHP apps [...]
[...] Configure Apache to run Tomcat and PHP Vía / PHPDeveloper.org [...]
This is exactly what I wanted to do! Thanks much for this concise how-to!
Glad to help!!! I spent days looking for documentation, and when I didn’t find any, I needed to put down what I learnt…
I’ve made a note to get a template together with this already done on it for our VPS customers.
Thanks for the mention Rob, it’s much appreciated.
Rob, I have been trying to do this on windows (XP)without success. Could you please show me how I can do this on windows. Thanks.
Bookmarked it. Been long for such a tech article for both Tomcat and Apache
@Brian pleasure, a script would be useful so that it is fairly straight forward, otherwise, its quite a headache as you can see.
@Rohit I am not much of a windows person, but I would imagine the installation process would be windows specific, but the configuration would be similar to what I wrote above. The only thing that would be very different is file locations.
@Kevin I hope yo get value out of it!
What do you think about authentication?
I have something similar. I would like a SSO like scenario where all the users accessing wordpress are authenticated against application database not against wordpress database.
How would you recommned to do that?
I believe that the problem you are trying to solve is quite different to the above.
You will want to get WordPress’s Authentication mecnahism to auth against something else other than itself (ie/ oAuth or LDAP etc). I believe there are some plugins available for WordPress that allow you to do that. Failing that, you may have to change some of the code on WordPress’s Auth files.
Tomcat has container security, but since WordPress is PHP and runs on Apache, you can’t use that.
Thanks Rob for your response.
Yup, the problem is different but I was looking for some ideas in this area. How we can do SSO where Apache is frontending and forwarding the request to tomcat using ajp or rendering wordpress.
Portal could be one of the option but I don’t want to go there yet.
I am still looking for a clean solution.
Anywayz, thanks for your response.
Thanks for a wonderful post, l ve been looking for such information, I will join jour rss feed now.