January 23, 2009

at Friday, January 23, 2009 Labels: , , , Posted by Billy

This article describes how to install Tomcat 6 behind the Apache web server. This allows Apache to serve the static content and Tomcat to handle the dynamic content of your site. It assumes you have already installed Apache and that the operating system is Ubuntu 8.10 Intrepid.

First we need to install Java on our system. We can use aptitude if we set our software sources correctly. To check, open the software sources list:

sudo nano /etc/apt/sources.list

Add the following two lines:
deb http://za.archive.ubuntu.com/ubuntu/ intrepid main restricted
deb http://za.archive.ubuntu.com/ubuntu/ intrepid multiverse

Update the repository:
sudo aptitude update

Now we can install Java:
sudo aptitude install sun-java6-jre

Confirm the Java installation by entering the following:
java -version

You should see output similar to "java version 1.6.0_10"

Installing Tomcat 6

The following command installs Tomcat 6 with the admin and example applications:
sudo aptitude install tomcat6 tomcat6-admin tomcat6-examples

The admin and example applications are optional. The admin applications provides quick and easy monitoring of the Tomcat server via a browser. The example applications help new users learn Tomcat and Java servlet programming.

To test the installation, point your web browser to http://server-name:8080, replacing server-name with the name or ip-address of your server. Be sure to add port 8080 to the end of the address. If you are installing Tomcat on your local machine, use localhost as the server name. Your browser should display the tomcat welcome page.

Previous versions of Tomcat required extra steps to set the java home variable and to automatically start tomcat when booting, but with Tomcat 6 this should be taken care of for you. You can view the Tomcat startup script in /etc/init.d/tomcat6.

Running Tomcat behind Apache

For this section, I assume you already have Apache installed. To connect Apache and Tomcat we need to install mod_jk:
sudo aptitude install libapache2-mod-jk

Installing mod_jk automatically adds it to Apache's list of enabled modules. If you already have it and need to enable it, issue the command:
sudo a2enmod jk

Next we need to modify Apache's configuration files to point specified paths of your site to Tomcat. First create a workers file that contains connection properties to Tomcat.
sudo nano workers.properties

Add the following lines to the file:

# This file provides minimal jk configuration properties needed to
# connect to Tomcat.
# We define a worked named 'default'



This creates a worker named "default" which connects to the Tomcat ajp port 8009. Now we make Apache aware of this file and tell it how to log jk information. Open apache2.conf:
sudo nano /etc/apache2/apache2.conf 

Add the following lines to the bottom of the file:

# Where to find workers.properties
JkWorkersFile /etc/apache2/workers.properties

# Where to put jk logs
JkLogFile /var/log/apache2/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

Next we tell Apache which paths of your site to let Tomcat serve. Site configuration files reside in /etc/apache2/sites-available/. If Apache serves one site you can edit the default site. Open the site configuration file and add the following between the VirtualHosts tags:

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Tomcat serves everything by default
JkMount / default
JkMount /* default

# Apache serves the following URLs
JkUnMount /static default
JkUnMount /static/* default
JkUnMount /photos default
JkUnMount /photos/* default

Notice the JKMount and JKUnMount lines. These settings enable Tomcat to serve all paths by default, and Apache to handle the /static and /photos paths. Adjust accordingly to your site. Next restart Apache so the changes take effect:
sudo /etc/init.d/apache2 restart

Point your web browser to you site, leaving off the trailing port number. You should see the Tomcat welcome page. Congratulations! You now have a Tomcat server running behind Apache!


  1. Thanks for your tutorials. This takes me haft a day until I read this.

  1. You're welcome!

  1. Thanks for the tutorials it is really easy to read through.

    I got a specific problem though.related to the ubuntu 8.10 libapache2-mod-jk installation, But lets get things in the chronological order.

    1. install Apache2 -> works fine
    2. install Tomcat6 -> works fine (java preinstalled)
    3. install libapache2-mod-jk ->tomcat crashed (localhost:8080 gives me "Failed to Connect"), no matter how many restarts
    4. after that I went through your tutorial step by step. Still tomcat crashes
    5. deleted from my system all the changes found in your tutorial and unistalled libapache2-mod-jk -> Still tomcat crashes

    Any ideas?

  1. Worked Just Brilliantly.
    Done this on 8.04 Hardy server.
    added the apt repository for intrepid and used apt-get install.
    Already running.

  1. Worked like a charm!

  1. Thanks for your tutorial.

    Followed your instructions and tomcat is running on port 8080.

    Modul jk is enabled and i created worker.properties file, changed the conf etc. but apache still does not forward http requests to tomcat.

    Does anybody else have this problem and has a solution?

    Thanks for any hint.


  1. This comment has been removed by the author.
  1. Great stuff up & running in 15', works on ubuntu 8.04,
    to make it more complete you may add the part relative to users since you need to create them for being able to administer by the web

  1. Since the default tomcat6 installation on Ubuntu doesn't allow mod_jk to communicate with it directly, we should edit server.xml file first.

    vim /etc/tomcat6/server.xml

    and uncomment the line

    to avoid to "connecting to tomcat failed" problem.

  1. Great article, saved me some hours :-)

    I was doing this on Ubuntu Lucid server (10.04)