Apache 2 and Tomcat in Co-existence

Russell Bateman
November 2013
last update:

This will become a how-to of serving up static HTML pages from one or more domains plus Tomcat-based web applications from at least one other domain. My web server hosts some 20 domains consisting almost completely of simple, static content, but I needed to begin supporting applications too: Tomcat and Apache coexisting.

Preface

The Apache JServ Protocol (AJP) proxies in-bound requests from a web server through to an application server, like Tomcat, sitting behind it. This is accomplished using Apache's mod_jk plug-in. Also implicated is the Apache module, mod_proxy.

Installation of libapache2-mod-jk

Step by step, I did:

    $ apt-get update
    $ apt-get install libapache2-mod-jk
    $ a2enmod jk   # (didn't need to do this)

The Duffin lifecycle...

After a lot of Googling, reading, trial and error, I found that pretty much everyone who's written about this is either wrong or illiterate. The lone exception appeared to me to be Joel Duffin whose article, Configuring Apache and Tomcat to serve my Java web application through port 80, proved most useful—possibly because I had already read everybody else, but still, his was the only one that didn't leave me perplexed.

Ultimately, by the time I got this working, I essentially followed Duffin, erasing detours I had taken. I deviated from Duffin where trying my best to keep everything as it comes installed in order to have to modify the very least.

So, since it was successful, here are my steps with their corresponding Duffin lifecycle numbers (from the article). In the end, I only had to do three things:

Steps

  1. /etc/tomcat6/server.xml:

    Basically, this amounted to uncommenting an already existing line.

    <Server port="8005" shutdown="SHUTDOWN">
        .
        .
        .
        <!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
        .
        .
        .
    </Server>
    
  2. Presence ensured: /etc/apache2/mod-available/jk.load, unmodified.
  3.  
  4. Presence ensured: /etc/apache2/mod-available/jk.conf, unmodifed, however, the bolded contents are noteworthy.
    /etc/apache2/mod-available/jk.conf:
    <IfModule jk_module>
    
        # We need a workers file exactly once
        # and in the global server
        JkWorkersFile /etc/libapache2-mod-jk/workers.properties
    
        # Our JK error log
        # You can (and should) use rotatelogs here
        JkLogFile /var/log/apache2/mod_jk.log
    
        # Our JK log level (trace,debug,info,warn,error)
        JkLogLevel info
    
        # Our JK shared memory file
        JkShmFile /var/log/apache2/jk-runtime-status
        .
        .
        .
    
  5. No comment.
  6.  
  7. Presence ensured: /etc/apache2/sites-available/aintnoclouds instead of modifying the default vhosts file.
    /etc/apache2/sites-available/aintnoclouds:
    <VirtualHost *:80>
    	ServerName www.aintnoclouds.com
    	ServerAlias aintnoclouds.com *.aintnoclouds.com
    	DocumentRoot /home/websites/aintnoclouds.com/
    	<IfModule mod_jk.c>
    		JkMount /* ajp13_worker
    	</IfModule>
    </VirtualHost>
    
  8. Presence ensured: /usr/lib/apache2/modules/mod_jk.so.
  9.  
  10. Presence ensured: /etc/libapache2-mod-jk/workers.properties, unmodified, and where our stock jk.conf already had it.
    /etc/libapache2-mod-jk/workers.properties:
    workers.tomcat_home=/usr/share/tomcat6
    workers.java_home=/usr/lib/jvm/default-java
    worker.list=ajp13_worker
    worker.ajp13_worker.port=8009
    worker.ajp13_worker.host=localhost
    worker.ajp13_worker.type=ajp13
    worker.ajp13_worker.lbfactor=1
    worker.loadbalancer.type=lb
    worker.loadbalancer.balance_workers=ajp13_worker
    
  11. No comment.

Ladies and gentlemen, that did it!

All I had to do was:

    root@taliesin:/etc/apache2# service apache2 restart
     * Restarting web server apache2                   ... waiting .     [ OK ]
    root@taliesin:/etc/apache2# service tomcat6 restart
     * Stopping Tomcat servlet engine tomcat6                            [ OK ]
     * Starting Tomcat servlet engine tomcat6                            [ OK ]

Then, I launched a browser and typed in http://www.aintnoclouds.com/dpl/ only to find my application's splash page, i.e.: working perfectly.

Links

I looked at and read through pretty much all of these. Some were especially frustrating and I gave up going back to evaluate or comment on them once I found Duffin, promoted him to the top, then promptly ignored the rest. I've left them here for completeness, however.