Apache and Tomcat

Install mod-jk

apt-get install libapache2-mod-jk

Enable Apache module

a2enmod jk

Create /etc/apache2/workers.properties and add the following into it

# List workers
worker.list=worker1,worker2

# Define worker1
worker.worker1.port=8009
worker.worker1.host=domain1.tld
worker.worker1.type=ajp13

# Define worker2
worker.worker2.port=8010
worker.worker2.host=domain2.tld
worker.worker2.type=ajp13

Edit your apache2 config in /etc/apache2/apache2.conf and add this to the end Or check jk.conf and jk.load files under /etc/apache2/mods-available. If there is no jk.load create and add the following into it.

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

If there is no jk.conf create and add the following into it.

# tomcat connector stuff:
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk shared memory
JkShmFile     /var/log/apache2/mod_jk.shm
# Where to put jk logs
JkLogFile     /var/log/apache2/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel    info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]

In your /etc/apache2/sites-enabled/ directory find the vhosts you want to use tomcat and edit them all, at the end of the vhost declaration put:

<IfModule mod_jk.c>
#Everything under root goes to tomcat
JkMount  /* worker1
#html files should be served by apache
JkUnMount /*.html worker1
</IfModule>

Edit /etc/tomcat6/server.xml and make sure that the ajp connector is uncommented and add other ports:

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

To set domains to different webapps directory, add below lines under the last Host directive (You may specify an absolute pathname for this directory, or a pathname that is relative to the $CATALINA_HOME directory.)

<Host appBase="webapps-domain1" autodeploy="true" name="domain1.tld" unpackwars="true" xmlnamespaceaware="false" xmlvalidation="false">
  <Alias>www.domain1.tld</Alias>
</Host>

<Host appBase="/var/www/vhosts/domain2.tld/httpdocs" autodeploy="true" name="domain2.tld" unpackwars="true" xmlnamespaceaware="false" xmlvalidation="false">
  <Alias>www.domain2.tld</Alias>
</Host>

If you want the manager app to work on the domain you need to copy manager.xml. Do the same for host-manager, etc:

mkdir /etc/tomcat6/Catalina/domain1.tld
cp /etc/tomcat6/Catalina/localhost/* /etc/tomcat6/Catalina/domain1.tld
Export WAR file for deployment first, then remove ROOT directory and copy war file for auto deployment
rm -r /var/lib/tomcat6/webapps/ROOT*
cp ~/my-app-0.1.war /var/lib/tomca6/adminapps/ROOT.war
In /var/lib/tomcat6 create a webapps-domain1 directory and give it 0775 perms and chown it to tomcat6:tomcat6
mkdir /var/lib/tomcat6/webapps-domain1
mkdir /var/lib/tomcat6/webapps-domain2
chown -R 0775 /var/lib/tomcat6/webapps-domain1
chown -R 0775 /var/lib/tomcat6/webapps-domain2

Restart tomcat:

/etc/init.d/tomcat6 restart

Restart apache:

/etc/init.d/apache2 restart

Navigate to domain1.tld and you should be all tomcatted