If there is any topic I have researched till the point of further hair loss, it is getting an Apache web server to collaborate with Tomcat easily, on a Ubuntu system. In fact, I think collaborate may not be the right term, but a more proper description would be having Apache delegate certain tasks to Tomcat. Essentially, Tomcat would handle the access to the Java applications, while Apache would take care of everything else.
There are alot of articles, blog posts, and how-to’s that take this topic into depth. Most of them are either out-dated, or haven’t quite worked with my present scenario.
I’ve got a Ubuntu 10 system with the latest install of Apache. Apache is hosting multiple sites for multiple users. Of the multiple users, a couple have a requirement to run a java application, and this is where Tomcat comes into play. It would be easier if you just do a standard install of Tomcat and have the apps called through the browser using a port, but this is not ideal. Also, there is the possibility that each user might require the app to be accessed using SSL (btw, you’ll need multiple IPs for each one) and this just makes everything alot more tricky.
This is an introductory post on getting the ball rolling. I’ll jump into some detail in the next couple posts.
Step 1 – Apache 2
sudo aptitude install apache2
Step 2 – Open JDK
sudo aptitude install java-6-openjdk
Step 3 – Installing Tomcat 6 and Connectors
sudo aptitude install tomcat6 libapache-mod-jk
Step 4 – Activate Mod JK
sudo a2enmod jk
Step 5 – Create a workers file
sudo nano /etc/apache2/workers.properties
Copy the following code in it and save
workers.tomcat_home=/etc/tomcat6 workers.java_home=/usr/lib/jvm/java-6-openjdk ps=/ worker.list=tomcat6 worker.tomcat6.port=8009 worker.tomcat6.host=localhost worker.tomcat6.type=ajp13 worker.tomcat6.lbfactor=1
Step 6 – Modify the Apache Config
We’ll have to let Apache know about the workers.properties file you just created. Copy the following code to the end of the apache2.conf file.
JkWorkersFile /etc/apache2/workers.properties JkShmFile /var/log/apache2/mod_jk.shm JkLogFile /var/log/apache2/mod_jk.log JkLogLevel info JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
Step 7 – Create a site in Apache
sudo nano /etc/apache2/sites-available/example.com.vhost
Our current site resides in a the directory (/home/admin/users/username/example.com/public) and the app is located in the app directory. The code below is for my virtual host as well as the app.
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com ServerAdmin email@example.com DirectoryIndex index.php index.html index.htm index.shtml index.cgi index.pl index.xhtml DocumentRoot /home/admin/users/username/example.com/public LogLevel warn ErrorLog /home/admin/users/username/example.com/log/error.log CustomLog /home/admin/users/username/example.com/log/access.log combined ErrorDocument 400 /error/400.html ErrorDocument 401 /error/401.html ErrorDocument 403 /error/403.html ErrorDocument 404 /error/404.html ErrorDocument 405 /error/405.html ErrorDocument 500 /error/500.html ErrorDocument 503 /error/503.html ScriptAlias /cgi-bin/ /home/admin/users/username/example.com/cgi-bin/ AddHandler cgi-script .cgi AddHandler cgi-script .pl <Location /cgi-bin> Options +ExecCGI </Location> <Directory /home/admin/users/username/example.com/public> Options -Indexes AllowOverride All Order allow,deny Allow from all </Directory> <IfModule mod_jk.c> JkMount /app tomcat6 JkMount /app/* tomcat6 </IfModule> </VirtualHost>
Now that this is done, you’ve got to enable the site for Apache to know about it.
sudo a2ensite example.com.vhost
Step 8 – Modify Tomcat Configuration
Remove the comment that defines the port just below the AJP Connector
<!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Create the link to your site in the host name section towards the bottom of the configurations
<!-- example.com --> <Host name="example.com" appBase="/home/admin/users/username/example.com/public/" unpackWARs="true" autoDeploy="true"> <context path="" docBase="app" debug="0" reloadable="true"/> <valve className="org.apache.catalina.valves.AccessLogValve" directory="/home/admin/users/username/example.com/log" prefix="tomcat_access_" suffix=".log" pattern="common" resolveHosts="false"/> </Host>
Step 9 – Restart Apache and Tomcat
This is grand finale of it all. You’ll have to stop Apache, restart Tomcat, startup Apache, and basically hit the ground running.
sudo /etc/init.d/apache2 stop sudo /etc/init.d/tomcat6 restart sudo /etc/init.d/apache2 start
Visit your page and test out your application. More than likely, your setup and mine would probably not be the same. Let me know if you’ve got any problems.