Gentoo Wiki


Split-arrows.gifIt has been suggested that this article be split into multiple articles accessible from an index page.   (Discuss).
An Index page has been created that this article's contents should be split into: Apache2 please use it to make the article more atomic.


Getting Apache2 and Tomcat5 to work


First of all we need to install apache and tomcat. For more information about installing tomcat see [1].

$emerge apache
$emerge tomcat

I have installed apache with the berkdb doc gdbm threads options turned on.
For tomcat only the doc option was turned on. You need the java5 flag to be turned on, otherwise you will experience compilation errors


$/etc/init.d/apache2 start
$/etc/init.d/tomcat5 start

You can test to see if your servers are started correctly by surfing to http://localhost for apache and to http://localhost:8080 for tomcat.

If you can see a notification of both servers they are started successfully (for Tomcat you might see the very blank page without any errors, as for ebuild 5.0.28-r[6-9], installed without "examples" in the correspondent USE variable).

JK Connector


First check if you have adjusted the USE flags in /etc/make.conf appropriately, to make sure emerge assumes Apache 2 instead of 1.3.x:


Then go ahead and make Portage install mod_jk for you:

 emerge -av mod_jk

SPARC Users Note: If emerge complains that mod_jk has a "masked keyword" you may have to unmask mod_jk before emerging it with something like:

 echo "=www-apache/mod_jk-1.2.25 **" >> /etc/portage/package.keywords
 emerge -av mod_jk

Configuring Apache to Use the mod_jk Connector

Once mod_jk is installed, we need to tell apache to load the JK Connector when it starts. To do this , we will need to edit the APACHE2_OPTS variable in the apache2 conf.d file:

 nano -w /etc/conf.d/apache2

Look for the line APACHE2_OPTS= and add "-D JK" (do not delete any other options that you may have, e.g., -D PHP5).


Start Tomcat and Apache back up:

 /etc/init.d/apache2 restart && /etc/init.d/tomcat5 restart

Now point your web browser to http://localhost/index.jsp and you should see the tomcat start page, as you would see it if you browsed to http://localhost:8080. If you did, then you are done! Mod_jk will now take care of handling your .jsp's--but be aware that the location where mod_jk will look for a .jsp file is determined by your tomcat setup.

In tomcat-5.0.28 the tomcat files are spread under /usr/share/tomcat-5 and /var/lib/tomcat-5/default/

In tomcat-5.0.27 all files are under /opt/tomcat5/

In tomcat-5.0.28 e.g., /var/lib/tomcat5/default/webapps/[ROOT/index.jsp]<-- (This must be created by you) is the page you will see by default when browsing to http://localhost/index.jsp.

Tips for Virtual Hosting by Name and Tomcat

By default the /etc/apache2/modules.d/88_mod_jk.conf is configured to make Tomcat handle all .jsp pages. If you are running a virtual hosting environment, you may not want each host to be able to execute jsp pages.

To handle jsp's on a virtual host basis, edit /etc/apache2/modules.d/88_mod_jk.conf by commenting out jkMount /*.jsp.

 nano -w /etc/apache2/modules.d/88_mod_jk.conf
 #jkMount /*.jsp ajp13

Then to add jsp capabilities to a virtual host simply add the jkMount command inside the <VirtualHost *:80> tag. For example, if you site was you might:

 nano -w /etc/apache2/vhosts.d/01_www.gentoo.com_vhost.conf

And between the <VirtualHost *:80> and </VirtualHost> add:

 jkMount /*.jsp
 jkMount /servlet/*

Warning About mod_jk2

The rest of this wiki explains how to use mod_jk2, but mod_jk2 is deprecated. Please use the above tutorial on installing mod_jk. DO NOT USE THE MOD_JK2 METHOD!!!

JK2 Connector


Installing connector via portage:

emerge mod_jk2

Or you can download the JK2 connector from apache's website.

tar -zxvf jakarta-tomcat-connectors-jk2-src-current.tar.gz

Before we config and compile, lets get the config vars in an accepable place.

mkdir /usr/build
cp /usr/lib/apache2/build/ /usr/build/
ln -s /usr/bin/libtool /usr/build/libtool
cd jakarta-tomcat-connectors-jk2-2.0.4-src/jk/native2/   #Check the version!
./configure --with-apxs2=/usr/sbin/apxs2 --with-jni
libtool --finish /usr/lib/apache2
cp ../build/jk2/apache2/*.so /usr/lib/apache2

What you just built and copied were the and files.

Setting up the jk2 connector

This section is deprecated with Apache2! Use the mod_jk connector instead of the mod_jk2 connector with Apache2!

See this tutorial for setting up mod_jk on Gentoo. Additional info can be found on Apache's website.

Yes it's true, see this post.

First of all lets stop apache and tomcat, so that nothing crashes.

/etc/init.d/apache2 stop
/etc/init.d/tomcat5 stop

Lets touch the socket

touch /opt/tomcat5/work/jk2.socket
chown tomcat:root /opt/tomcat5/work/jk2.socket
touch /var/log/tomcat5/shm.file
chown tomcat:root /var/log/tomcat5/shm.file
usermod -G apache,tomcat apache

Configuring everything to work together


Make sure everything in this file is commented out. Default values are good enough.


Mine looks like this:

 # Example socket channel, override port and host.
 # define the worker
 # Uri mapping


Just add these lines to get jsp working in every user directory + documentroot:

File: apache2.conf
### Load the jk2 module
LoadModule jk2_module /usr/lib/apache2-extramodules/
### Set the config file for jk2
JkSet config.file /etc/apache2/conf/
### Forward all jsp files to the worker
<Location "/*.jsp"> 
JkUriSet worker ajp13:localhost:8009 
### Every user a website
UserDir public_html
UserDir disabled root
### Automaticly load index
DirectoryIndex index.html index.htm index.jsp index.txt


Search for the host specification where <host name=localhost ... is located and apply the modifiations as shown below.

NOTE: Remember to remove the matching </Host> tag located towards the end of the file.

File: server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">


<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true"  xmlValidation="false" xmlNamespaceAware="false">
    <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="localhost_log."      suffix=".txt" timestamp="true"/>
<Host name="YOUR SERVERNAME" debug="0" appBase="YOUR DOCUMENTROOT" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="YOUR SERVERNAME"   suffix=".log"  timestamp="true"/>
    <Context path="" docBase="" debug="0" allowLinking="true"/>
    <Listener className="org.apache.catalina.startup.UserConfig" directoryName="public_html"      userClass="org.apache.catalina.startup.PasswdUserDatabase"/>  

Take care that all host tags are closed and none are left open.
I gave 2 hostnames as you can see. The first is only accessible from localhost:8080 and the second is accessible from the internet by surfing to your servername.
The first is the default start screen of tomcat but it can be very handy to manage or test some files.

Restarting servers

/etc/init.d/apache2 start
/etc/init.d/tomcat5 start


I wrote a small file called test.jsp to see if your apache forwards jsp files to tomcat.

File: test.jsp
Hello!  The time is now <%= new java.util.Date() %>

Save it to $CATALINA_HOME/webapps/ROOT/test.jsp

Test with http://YOUR_SERVERNAME/test.jsp.

If your /var/log/apache2/error_log log says

[Wed Nov 17 15:10:54 2004] [error] shm.create(): error creating shm 70014 End of file found
[Wed Nov 17 15:10:54 2004] [error] shm.create(): error creating shm /opt/tomcat5/logs/shm.file

then remove the [shm] section from /etc/apache2/conf/ This is a workaround to use TCP instead of shared memory (faster on localhost) for communication.


Special thanks to barcct and d2r

See Also


Retrieved from ""

Last modified: Sun, 28 Sep 2008 05:13:00 +0000 Hits: 62,222