In this post we’re going to setup Apache to act as a proxy for the Tomcat application server on Ubuntu. First off we need to install the “tomcat6” package from the Ubuntu repositories, which is as simple as:
sudo apt-get install tomcat6
and answering “Y” to download Tomcat along with all of its dependencies. To make sure that the Tomcat server is running, try to open up port 8080 on the machine in your browser. If all is well you will see the Tomcat server Welcome page. If not, you may need to start up the server, which can be done with:
sudo service tomcat6 start
Next we need to install the Apache HTTP server, which “apt-get” also makes easy for us:
sudo apt-get install apache2
Again, just enter “Y” when asked whether to download the package and all of its dependencies.
In order to enable Apache to act as a proxy for Tomcat, we’re going to need to make use of the “proxy” and “proxy_http” modules. Unfortunately these two modules don’t come enabled by default, so we’re going to have to enable them and restart apache for the changes to take effect:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo service apache2 restart
Now we need to tell the proxy module how to proxy the requests and where to proxy them to. For this I’ve created a “tomcat-proxy” file under /etc/apache2/sites-available/, which we’re going to enable using Apache’s a2ensite command. The file itself looks like the following:
After editing the file we enable the site and reload Apache’s configuration:
sudo a2ensite tomcat-proxy
sudo service apache2 reload
And that’s it! If everything’s gone to plan we should be able to hit up port 80 on our server and get the Tomcat welcome page.
Note that this isn’t the only way to configure Apache as a proxy. A more sophisticated way is to make use of the AJP protocol/module, which is custom designed to work with Tomcat.
To get Apache proxying to Tomcat using the AJP protocol, we have to enable the Apache module and restart Apache for the changes to take effect:
sudo a2enmod proxy_ajp
sudo service apache2 restart
Next we have to enable the AJP connector in Tomcat. This is done in the /etc/tomcat6/server.xml file. If you edit this file you’ll need to uncomment a line, which looks like:
<connector port=”8009″ protocol=”AJP/1.3″ redirectport=”8443″></connector>
and restart Tomcat:
sudo service tomcat6 restart
The next step is to simply go back to our configuration file under /etc/apache2/sites-available/tomcat-proxy and change the protocol and port of the URL’s we supplied the ProxyPass and ProxyPassReverse directives:
# Configure the mod_proxy
ProxyPass / ajp://127.0.0.1:8009/
ProxyPassReverse / ajp://127.0.0.1:8009/
You’ll notice we’ve replaced “http” in the URL with the custom “ajp” protocol. Restart Apache and hitting port 80 on the server should redirect you to the Tomcat welcome page as before.
What’s the difference between the two approaches to proxing? Functionally there’s not really any difference that the user gets to see. However, the AJP is a binary protocol, compared to the regular HTTP proxy method that the first approach uses. This should mean less data passed between the proxy and application server as well as lower latencies. Look out for a future post benchmarking the two approaches to see what the real-world difference in performance between the two approaches is.