Gentoo Wiki ArchivesGentoo Wiki

HOWTO_Apache2_and_Tomcat5

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.

Contents

Getting Apache2 and Tomcat5 to work

Installing

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

Starting

$/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

Installation

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:

 USE="apache2"

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).

APACHE2_OPTS="-D JK"

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 gentoo.com 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

Getting+Installing

Installing connector via portage:

emerge mod_jk2

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

wget http://www.apache.org/dist/jakarta/tomcat-connectors/jk2/jakarta-tomcat-connectors-jk2-src-current.tar.gz
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/config_vars.mk /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
make
libtool --finish /usr/lib/apache2
cp ../build/jk2/apache2/*.so /usr/lib/apache2

What you just built and copied were the mod_jk2.so and libjkjni.so 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 forums.gentoo.org 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

/etc/tomcat5/jk2.properties

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

/etc/apache2/conf/workers2.properties

Mine looks like this:

File: workers2.properties
[shm]
file=/opt/tomcat5/logs/shm.file
 size=1048576
 
 # Example socket channel, override port and host.
 [channel.socket:localhost:8009]
 port=8009
 host=127.0.0.1
 
 # define the worker
 [ajp13:localhost:8009]
 channel=channel.socket:localhost:8009
 
 # Uri mapping
 [uri:/*.jsp]
 [uri:/~*/*.jsp]
 [uri:/jsp-examples]
 worker=ajp13:localhost:8009

/etc/apache2/conf/apache2.conf

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/mod_jk2.so
### Set the config file for jk2
JkSet config.file /etc/apache2/conf/workers2.properties
### Forward all jsp files to the worker
<Location "/*.jsp"> 
JkUriSet worker ajp13:localhost:8009 
</Location>
### Every user a website
UserDir public_html
UserDir disabled root
### Automaticly load index
DirectoryIndex index.html index.htm index.jsp index.txt

/etc/tomcat5/server.xml

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>

To

<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>
<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"/>  
    ...
</Host>

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

Testing

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

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

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/workers2.properties. This is a workaround to use TCP instead of shared memory (faster on localhost) for communication.

Thanks

Special thanks to barcct and d2r

See Also

HOWTO

Retrieved from "http://www.gentoo-wiki.info/HOWTO_Apache2_and_Tomcat5"

Last modified: Sun, 28 Sep 2008 15:13:00 +1000 Hits: 67,657

Created by NickStallman.net, Luxury Homes Australia
Real estate agents should list their apartments, townhouses and units in Australia.