Using Apache HTTP Server in front of Tomcat 8
Feb 04, 2015
Previously, I wrote about installing Java 8 and Tomcat 8. In this post, we’ll see how to use an Apache HTTP server in front of Tomcat 8. Note that the configuration described in this post is what worked for me. The official documentation (here and here) is heavy on the various configuration options that are available, but doesn’t help much if you are looking for a simple example of how to configure Tomcat with Apache HTTP server, and then tweak that simple configuration as you need. So depending on your goal, the information in this post may not be sufficient. With that caveat, here’s how I got things working:
Install and configure the Tomcat-Apache connector
(The version of Apache HTTP server that I’m using is 2.2, from Debian Wheezy repositories).
Install the Apache-Tomcat connector module:
sudo apt-get install libapache2-mod-jk
That should also enable the jk module in Apache. But just to be sure, enable it explicitly:
sudo a2enmod jk
The Apache-Tomcat connecter uses worker processes to handle requests forwarded by the Apache HTTP server. The configuration of thes processes is in
/etc/libapache2-mod-jk/workers.properties. There, update the paths to
java_home. as I described in my previous post, if you are using separate Tomcat directories - one for Tomcat binaries and one for the config files and webapps, then
tomcat_home should point to directory having the webapps.
The configuration for mod_jk is in
/etc/apache2/mods-enabled/jk.conf. I left it unchanged, but do take a look at it to see if there’s something you’d like to change.
Tomcat virtual host configuration
Next, if you are using virtual hosts in Apache HTTP server, then you need to configure a virtual host in Tomcat’s
server.xml. The Tomcat site has documentation on how to do that.
This is how my
<Engine> tag ended up looking like:
<Engine name="Catalina" defaultHost="satishchilukuri.com"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="satishchilukuri.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine>
(Since I know that I’m not going to use
http://localhost:8080 to access applications deployed in Tomcat, I changed the
Update Apache’s virtual host configuration
The final step is telling the Apache HTTP server to forward all requests to Tomcat. First you need to tell Apache where the root of your website is. Since we are using Tomcat to run our web application, this path will be the path to our deployed web application:
(I configured my Tomcat in such a way that my blog application is the root. If you want to keep your Tomcat’s ROOT intact and deploy your application in another directory, give the name of that directory instead of ROOT).
Then you need a
<Directory> directive to allow access to resources inside
<Directory /opt/tomcat-base/webapps/ROOT> Order allow,deny Allow from all Options -Indexes </Directory>
Options -Indexes” directive disables directory listing).
Next, define a
JkMount directive in your
<VirtualHost> directive. This tells Apache to forward all requests to the Tomcat-Apache connecter:
JkMount /* ajp13_worker
‘/*’ says to forward all requests. “
ajp13_worker” refers to the worker defined in
This is how my final Apache VirtualHost configuration looks like:
<VirtualHost *:80> ServerAdmin email@example.com ServerName satishchilukuri.com ServerAlias www.satishchilukuri.com DocumentRoot /opt/tomcat-base/webapps/ROOT JkMount /* ajp13_worker ErrorLog /srv/www/satishchilukuri.com/logs/error.log CustomLog /srv/www/satishchilukuri.com/logs/access.log combined <Directory /opt/tomcat-base/webapps/ROOT> Order allow,deny Allow from all Options -Indexes </Directory> </VirtualHost>