Configuring Tomcat5 and Apache2 with Virtual Hosts using mod_jk - Page 4

Want to support HowtoForge? Become a subscriber!
 
Submitted by ctroyp (Contact Author) (Forums) on Sun, 2006-02-12 23:32. ::

Configure Apache

Important note: Be sure to make a copy of your config files before modifying.

  1. Open your Apache2 configuration file and add the following just below the line Include /usr/lib/apache-tomcat/conf/auto/mod_jk.conf.

# Where to find workers.properties
JkWorkersFile /etc/apache2/workers.properties

# Where to put jk logs
JkLogFile /var/log/apache2/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Globally deny access to the WEB-INF directory
<LocationMatch '.*WEB-INF.*'>
AllowOverride None
deny from all
</LocationMatch>

  1. This next section assumes you are using virtual hosts. In this system I am running ISPConfig where all of my Vhosts are defined in a separate file.

Otherwise, if you are not using ISPConfig and your Vhosts are defined within your main Apache2 config file, you just need to add this code there within the desired Vhosts that you want to utilize Tomcat (JSP/Servlets).

If you are not running Vhosts, simply add this code at the bottom of your main Apache2 config file.

For those running ISPConfig as I am, you will need to either manually place this code in the master Vhost (localhost) in the /etc/apache2/vhosts/Vhosts_ispconfig.conf or the selected Vhosts that you want to utilize Tomcat (JSP/Servlets) using the ISPConfig control panel—in the Apache Directives window for each website.

This is up to you.

For this server, I have the ISPConfig Vhosts defined in /etc/apache2/vhosts/Vhosts_ispconfig.conf.

You can either place this code after the <VirtualHost ipaddress:port> tag or just before the </VirtualHost> end tag. ISPConfig documentation recommends that you use the control panel to include any additional virtual host directives.

# Send servlet for context /servlets-examples to worker named worker1
JkMount /*/servlet/* worker1
# Send JSPs for context /jsp-examples to worker named worker1
JkMount /*.jsp worker1

Conclusion

To finalize and test this configuration you will need to copy the Tomcat example JSP files to the virtual host web directory that was defined in the server.xml, apache2.conf, and Vhosts_ispconfig.conf files. In the server.xml file you will notice that I am referencing the jsp-examples directory where jsp-examples is the name of the docbase. This will give you some idea of how the web applications will be setup for your website. You can find more details on the Tomcat website on how to generate your original web applications. In order to define another web application, you would need to define another worker (i.e. worker2) in the workers.properties file, add another host in the server.xml file, and add the same directives (using worker2 of course) to the respective VHost section.

      <!-- www.domain1.org -->
<Host name="www.domain1.org" appBase="/home/www/web2/web"
unpackWARs="true" autoDeploy="true">

<Context path="" docBase="jsp-examples" debug="0" reloadable="true"/>

<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="web1_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>
</Host>

  1. First copy just the jsp-examples directory to web1.

cp -R /usr/lib/apache-tomcat/webapps/jsp-examples /home/www/web1/web

  1. Next you need to restart Tomcat as described earlier and then restart Apache. Remember to restart Tomcat first so you will regenerate the mod_jk.conf file.

cd /usr/lib/apache-tomcat/bin

sh shutdown.sh

sh startup.sh

/etc/init.d/apache2 restart

  1. Finally test the websites by entering the respective url into your browser:

http://www.domain1.org/jsp-examples/

You should see the same jsp-examples html as you did in the default Tomcat page from earlier.

Congratulations! Your server should now be ready to support JSP/Servlets. Again, if you find any inconsistencies within this tutorial, please contact me so I can make the appropriate corrections.

Resources/Links

This tutorial was compiled using several resources that I found on the internet. Due to the trouble I had finding all of this information it only made sense to put it all together.

http://blog.gilluminate.com/?s=10

Integrating Tomcat and Apache on Red Hat Linux

Apache website

Tomcat website

Workers HowTo

http://tomcat.apache.org/connectors-doc-archive/jk2/jk2/vhosthowto.html

http://tomcat.apache.org/connectors-doc/howto/apache.html

The Perfect Setup - Debian Sarge (3.1)

http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/

pdo.debian.net


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Dan Kelleher (not registered) on Tue, 2011-11-01 21:02.
The clearest and easiest to follow guide for mod_jk I found. Thanks very much.
Submitted by drid (registered user) on Sun, 2007-06-10 21:10.

I get a "Useless use of AllowOverride" message when I start Apache2. I guess thats because the <LocationMatch> does now support AllowOverride option. Only <Directory> support it according to http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride

Submitted by Anonymous (not registered) on Fri, 2006-04-07 09:15.
It is not recommeded to place <Context> elements directly in the server.xml file. See http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
Submitted by Anonymous (not registered) on Tue, 2006-03-07 08:06.
If you setup tomcat this way 1 tomcat to rule them all, every user can read another users home dir, isn't that a major security issue?