Apache 2 and Tomcat in Co-existence
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.
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.
Step by step, I did:
$ apt-get update $ apt-get install libapache2-mod-jk $ a2enmod jk # (didn't need to do this)
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:
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>
<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 . . .
<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>
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
All I had to do was:
[email protected]:/etc/apache2# service apache2 restart * Restarting web server apache2 ... waiting . [ OK ] [email protected]:/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.
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.