Getting Apache 2 and Tomcat 6 to Collaborate on a Ubuntu 10 server

If there is any topic I have researched till the point of further hair loss, it is getting an Apache web server to collaborate with Tomcat easily, on a Ubuntu system. In fact, I think collaborate may not be the right term, but a more proper description would be having Apache delegate certain tasks to Tomcat. Essentially, Tomcat would handle the access to the Java applications, while Apache would take care of everything else.

There are alot of articles, blog posts, and how-to’s that take this topic into depth. Most of them are either out-dated, or haven’t quite worked with my present scenario.

I’ve got a Ubuntu 10 system with the latest install of Apache. Apache is hosting multiple sites for multiple users. Of the multiple users, a couple have a requirement to run a java application, and this is where Tomcat comes into play. It would be easier if you just do a standard install of Tomcat and have the apps called through the browser using a port, but this is not ideal. Also, there is the possibility that each user might require the app to be accessed using SSL (btw, you’ll need multiple IPs for each one) and this just makes everything alot more tricky.

This is an introductory post on getting the ball rolling. I’ll jump into some detail in the next couple posts.

Step 1 – Apache 2

sudo aptitude install apache2

Step 2 – Open JDK

sudo aptitude install java-6-openjdk

Step 3 – Installing Tomcat 6 and Connectors

sudo aptitude install tomcat6 libapache-mod-jk

Step 4 – Activate Mod JK

sudo a2enmod jk

Step 5 – Create a workers file

sudo nano /etc/apache2/

Copy the following code in it and save


Step 6 – Modify the Apache Config
We’ll have to let Apache know about the file you just created. Copy the following code to the end of the apache2.conf file.

JkWorkersFile /etc/apache2/
JkShmFile /var/log/apache2/mod_jk.shm
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

Step 7 – Create a site in Apache

sudo nano /etc/apache2/sites-available/

Our current site resides in a the directory (/home/admin/users/username/ and the app is located in the app directory. The code below is for my virtual host as well as the app.

<VirtualHost *:80>


DirectoryIndex index.php index.html index.htm index.shtml index.cgi index.xhtml
DocumentRoot /home/admin/users/username/

LogLevel warn
ErrorLog  /home/admin/users/username/
CustomLog /home/admin/users/username/ combined

ErrorDocument 400 /error/400.html
ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 405 /error/405.html
ErrorDocument 500 /error/500.html
ErrorDocument 503 /error/503.html

ScriptAlias /cgi-bin/ /home/admin/users/username/
  AddHandler cgi-script .cgi
  AddHandler cgi-script .pl
  <Location /cgi-bin>
    Options +ExecCGI
<Directory /home/admin/users/username/>
  Options -Indexes
  AllowOverride All
  Order allow,deny
  Allow from all

<IfModule mod_jk.c>
  JkMount /app tomcat6
  JkMount /app/* tomcat6


Now that this is done, you’ve got to enable the site for Apache to know about it.

sudo a2ensite

Step 8 – Modify Tomcat Configuration
Remove the comment that defines the port just below the AJP Connector

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Create the link to your site in the host name section towards the bottom of the configurations

 <!-- -->
<Host name="" appBase="/home/admin/users/username/" unpackWARs="true" autoDeploy="true">
  <context path="" docBase="app" debug="0" reloadable="true"/>
  <valve className="org.apache.catalina.valves.AccessLogValve" directory="/home/admin/users/username/"  prefix="tomcat_access_" suffix=".log" pattern="common" resolveHosts="false"/>

Step 9 – Restart Apache and Tomcat
This is grand finale of it all. You’ll have to stop Apache, restart Tomcat, startup Apache, and basically hit the ground running.

sudo /etc/init.d/apache2 stop
sudo /etc/init.d/tomcat6 restart
sudo /etc/init.d/apache2 start

Visit your page and test out your application. More than likely, your setup and mine would probably not be the same. Let me know if you’ve got any problems.


  1. Farid Diego Astorga

    Ahmeddirie excellent tutorial, I worked to perfection!
    I think I’ve seen about 8 tutorials before but none succeeded to the operation!
    Share it to the test within the public I think one index.html and I think the app/ with a test.jsp follows

    Hello World

    Hello World
    Today is:

    I want to ask that when I tried http://“” gave me a Forbidden error (without running the ErrorDocument) thus proceeded to create the directory / error and files: 403.html and 404.html. But I still shows the default error and not my custom html. Obviously, when I create my index.html it does not happen because the file Apache is running normally. How I can make my 40x.html custom work?

  2. ahmeddirie

    Hi Farid,

    Thanks. Regarding the error pages, you’ve got to first define the error page in Tomcat. The web.xml file in /WEB-INF directory, add the directives pointing to that page.


    Once you’ve got all the error pages you need defined, you should then add it to the Apache configuration.

    ErrorDocument 400 /error/400.jsp
    ErrorDocument 401 /error/401.jsp
    ErrorDocument 404 /error/404.jsp

    Hope that helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>