Step 1 - Java installation

Describes how to integrate Oracle Java into an Ubuntu installation.
You should consider installing just the JRE on a production system, except when you need to compile Java code on that machine.

Install and configure Java in your environment

Variant 1: Download binaries from Oracle and run update-alternatives

Perform steps below to install Java and it will be configured as the default Java in your ubuntu system.
The disadvantage of this variant is that you have to keep java up to date manually.

root@jiffybox # cd /usr/local/
root@jiffybox # # (I am not sure if this will work, you may have to download the file manually)
root@jiffybox # wget http://download.oracle.com/otn-pub/java/jdk/6u31-b04/jre-6u31-linux-x64.bin
root@jiffybox # /bin/sh jre-6u31-linux-x64.bin
root@jiffybox # ln -s jre1.6.0_31/ java # create link /usr/local/java -> jre1.6.0_31/
root@jiffybox # update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/bin/java" 1
root@jiffybox # update-alternatives --set java "/usr/local/java/bin/java"

Variant 2: Use the georgious updater tool by webupd8team

This method integrates the Java update process into Ubuntus apt-get update routine.
I personally experienced this as a good way to keep Java up to date, it's quick and easy.
The disadvantage of this variant is that you have to rely on a third party software source. (java6 and java8 is also available here.)

root@jiffybox # add-apt-repository ppa:webupd8team/java
root@jiffybox # apt-get update
root@jiffybox # apt-get install apt-get install oracle-java7-installer oracle-java7-set-default

For both variants: You may want to register java in your environment

To set JAVA_HOME environment variable globally, add this line to
/etc/profile
(Note that this will not affect running processes.)

JAVA_HOME=/usr/local/java

Test the installation

root@jiffybox # java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

See also: Install Oracle Java on Ubuntu Linux

Step 2 - Tomcat installation

I prefer to split the tomcat installation into a shared CATALINA_HOME directory and one CATALINA_BASE directory per tomcat instance.

HOME installation (Setup shared installation)

In this example, /opt/tomcat is used as home.

root@jiffybox # cd /opt/
root@jiffybox # wget http://mirror.sti2.at/apache/tomcat/tomcat-7/v7.0.26/bin/apache-tomcat-7.0.26.zip
root@jiffybox # unzip apache-tomcat-7.0.26.zip
root@jiffybox # chown root:root -R apache-tomcat-7.0.26
root@jiffybox # chmod o-w -R apache-tomcat-7.0.26
root@jiffybox # ln -s apache-tomcat-7.0.26 tomcat

BASE installation (Setup an instance)

In this example, /home/testuser/tomcat_base is used as base.
If you want to create multiple instances, you might create a template first.

testuser@jiffybox $ mkdir /home/testuser/tomcat_base
testuser@jiffybox $ cd /home/testuser/tomcat_base
testuser@jiffybox $ mkdir {bin,conf,logs,temp,webapps,work}
testuser@jiffybox $ echo '# do whatever you want change in catalina.sh in this file.' >> bin/setenv.sh
testuser@jiffybox $ cp /opt/tomcat/conf/{server,web}.xml conf/

Create a startup skript for the instance like this snippet below.
I placed it at /etc/init.d/testuser-tomcat-1.sh :

#!/bin/sh
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/home/testuser/tomcat_base
/bin/sh $CATALINA_HOME/bin/catalina.sh $@

Don't forget to allow the tomcat owner to execute the script:

root@jiffybox # chgrp andre /etc/init.d/tomcat-andre.sh
root@jiffybox # chmod 754 /etc/init.d/tomcat-andre.sh

Finally, we want to test the installation with these statements:

testuser@jiffybox $ cp /data/shared/helloworld.war /home/testuser/tomcat_base/webapps/helloworld.war
testuser@jiffybox $ /etc/init.d/tomcat-andre.sh start
testuser@jiffybox $ wget http://localhost:8080/helloworld -O -

Step 3 - Use Tomcat Apps through Apache HTTP Server

We want to reach the Tomcat apps on the standard Web port (80 for HTTP, 443 for HTTPS). To redirect the traffic through the HTTP server to the servlet container, we use ModJK.
This allows an installed app (more precisely, a context root) respond via AJP protocol. The HTTP server may act as a load balancer in this case, but in this example, I use only a single worker.
For further information, try this article.

Relevant config files:

  • /etc/apache2/workers.properties
  • /etc/apache2/apache2.conf
  • /etc/apache2/sites-available/default
  • <TOMCAT_INSTANCE>/conf/server.xml
For example, you want the webapp http:// mydomain.ws:8080 /testapp to be available at http:// mydomain.ws /testapp.

Install and configure mod_jk

First, install the module:

root@jiffybox # apt-get install libapache2-mod-jk

Then, register and configure the module in apache configuration.
Add the following snippet to file /etc/apache2/apache2.conf :

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

After this, we will define a worker as mentioned above.
Create/Edit file /etc/apache2/workers.properties and insert following entries:

# define one single worker in this example:
worker.list=myfirstworker
# configure the worker:
worker.myfirstworker.type=ajp13
worker.myfirstworker.host=localhost
worker.myfirstworker.port=8009

Now it's time to tell the apache config to redirect the requests incoming to the /helloworld contextroot to our ajp worker.
Edit your /etc/apache2/sites-available/default file:

# (this is the host section we want to use for the mapping)
<VirtualHost *:80>
    ServerName mydomain.ws
    # [... blah blah ...]
    # define the mapping:
    JkMount /helloworld* myfirstworker
</VirtualHost>

Configure Tomcat

Finally, we need to configure the Tomcat instance to enable the AJP connector. Make sure the following element is set in /home/testuser/tomcat_base/conf/server.xml :

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

If you do not want tomcat to response any HTTP requests incoming to (default) port 8080, you can comment out the HTTP connector in the same file:

<!--
    <Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" />
-->

Congratulations, you should now be able to browse your testapplication through http://mydomain.ws/helloworld after restarting the services with:

root@jiffybox # /etc/init.d/apache2 restart
root@jiffybox # /etc/init.d/testuser-tomcat-1.sh stop
root@jiffybox # /etc/init.d/testuser-tomcat-1.sh start