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'
#

workers.tomcat_home=/var/lib/tomcat6
workers.java_home=/usr/lib/jvm/java-6-sun
ps=/
worker.list=default

worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1

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!

12 comments:

  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.

    Chris.

  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)

  1. Hi

    I have a great problem

    I have installed java, apache, tomcat and mod-jk:

    APACHE:
    Server version: Apache/2.2.14 (Ubuntu)
    Server built: Nov 18 2010 21:20:56

    TOMCAT
    Server version: Apache Tomcat/6.0.24
    Server built: December 10 2010 1755
    Server number: ...0
    OS Name: Linux
    OS Version: 2.6.32-28-generic
    Architecture: amd64
    JVM Version: 1.6.0_20-b20
    JVM Vendor: Sun Microsystems Inc.

    JAVA:
    java version "1.6.0_20"
    OpenJDK Runtime Environment (IcedTea6 1.9.2) (6b20-1.9.2-0ubuntu1~10.04.1)
    OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)

    All my linux system use UTF-8
    root@alfredo-laptop:/usr/share/tomcat6/bin# locale
    LANG=es_MX.UTF-8
    LC_CTYPE="es_MX.UTF-8"
    LC_NUMERIC="es_MX.UTF-8"
    LC_TIME="es_MX.UTF-8"
    LC_COLLATE="es_MX.UTF-8"
    LC_MONETARY="es_MX.UTF-8"
    LC_MESSAGES="es_MX.UTF-8"
    LC_PAPER="es_MX.UTF-8"
    LC_NAME="es_MX.UTF-8"
    LC_ADDRESS="es_MX.UTF-8"
    LC_TELEPHONE="es_MX.UTF-8"
    LC_MEASUREMENT="es_MX.UTF-8"
    LC_IDENTIFICATION="es_MX.UTF-8"
    LC_ALL=

    running

    http://localhost:8080/lexcic/SearchController?searchWord=niño

    everything works fine, but with

    http://localhost/lexcic/SearchController?searchWord=niño

    the java servlet recives:

    searchWord: niño

    to use tomcat6 and apache I just 1) edit 000-default

    vim /etc/apache2/sites-enabled/000-default

    and add the line JkMount /lexcic/* ajp13_worker

    2) edit server.xml and descomment

  1. I have just installed Tomcat 6 on my Ubuntu, thank you guidance on this install.