Quick Setup of Tomcat with mod_jk under RedHat 9.0
RedHat Linux comes, like most distributions, shipped with an Apache
HTTP
Server. When starting on school with classes on Java Server Pages, most
people assumed this was going to be done under Windows (a poor man's
operating system). Of course I
didn't want to give up my free-software position, so I started to look
what I needed to do to get JSP pages running under Linux. I browsed to
the Jakarta Project (
http://jakarta.apache.org/) and looked into installing a Tomcat webserver on my RedHat Linux 9 PC.
Though the first steps where pretty simple, running JSP under my
already installed Apache HTTP Server caused me some headaches. At the
end I have to say that these headaches were not caused by difficuly
installation routines, but more by large amounts of documentation which
just didn't get to the point. Below are the steps I took to get it all
running. I hope it will turn out easier for you, if you do the same
thing.
Note that you can run both Tomcat and Apache HTTP Server on one and the
same machine, but you'll need a mod_jk if you want to have
communication between both(!) servers and the outside world. Actually
only Apache HTTP Server listens to the outside world, while Tomcat
listens to Apache.
Setup of Tomcat
-
First I made sure I had a Java 2 Software Development Kit installed. Before, I had downloaded one of the prebuild RPMS of Sun (http://java.sun.com/j2se/downloads.html). The RPM I took, installed J2SDK into /usr/java/j2sdk-{versionnumber}.
-
Next I set a system variable JAVA_HOME in /etc/profile:
# echo JAVA_HOME=/usr/java/j2sdk-{versionnumber} >> /etc/profile
-
I then downloaded a binary version of Tomcat from http://jakarta.apache.org/ (a TAR.GZ file) and unpacked it in /opt. It doesn't really matter where you put it, as long as you remember where :).
-
I set a system variable CATALINA_HOME in /etc/profile (Catalina is the servlet container of Tomcat, basically the core):
# echo CATALINA_HOME=/opt/jakarta-tomcat-{versionnumber} >> /etc/profile
-
I ran the startup.sh script located in the Tomcat directory
# $CATALINA_HOME/bin/startup.sh
and pointed my browser to the address http://localhost:8080 and it worked!
Any JSP file I put now in the $CATALINA_HOME/webapps directory, would appear on the page.
Now the mod_jk module
So far so good, and easy. But I had now two webservers running:
Apache listening to port 80 and Tomcat listening to port 8080. It
didn't seem to be a good idea to me. So the next thing to do was
getting a mod_jk Apache module to load into the Apache server,
which passes JSP-requests through to the Tomcat server, which can
only be reached internally (localhost).
The Apache server shipped with RedHat 9 is version 2.0.40. I
looked around on the Internet, but nowhere I could find a
precompiled mod_jk module (mod_jk.so) for this Apache version.
Using modules precompiled for other versions, didn't work out
either - Apache complained about a wrong version. So I had to
compile my own. I had to experience again how wonderfull life with
open source software can be.
Many
sites (to my own impression even the Jakarta Project itself) make you
believe you need Ant to compile the module, or the complete source code
of the Apache server (which isn't shipped with RedHat), or a large list
of JAR files from both the Jakarta Project. I wanted to keep it simple.
At the end, I did.
-
I checked if "apxs" (Apache eXtension Tool) was on my system. (apxs is
part of the Apache HTTP Server distribution, but is excluded on some
Linux distributions. If apxs is missing, you'll probably have to
download an Apache HTTP Server distribution from their site.)
# whereis apxs
Lucky for me, it was right there on my system.
-
I downloaded the Tomcat Connector source package from http://jakarta.apache.org/
(from the same location where we downloaded the Tomcat server), and
unpacked it again in /opt. (NOTE: I used the Tomcat BINARY package but
the Tomcat Connector SOURCE package.)
-
I went into /opt/jakarta-tomcat-connectors-{versionnumber}/jk/native directory and ran
# ./buildconf.sh
-
Next I ran
# ./configure --with-apxs=/usr/sbin/apxs
Don't use the --with-apache option. (Later I forgot why. Maybe
cause I didn't had the original Apache source on my system, but
just the RedHat version.)
-
The configure went fine, so I ran
# make
-
Make built a shared library called "mod_jk.so" for me which I then
copied to the modules directory of Apache "/etc/httpd/modules/". I made
sure the permissions of the shared library where the same as the other
modules in this directory (defaults to 755).
-
Now the module itself is in place, I needed to tell Apache what to do
with it. The Jakarta Project offers a default configuration file, in
which I modified the LoadModule statement to point to the
"/etc/httpd/modules/" directory. I put the mod_jk.conf file in the "/etc/httpd/conf.d/" directory so it is automatically read by Apache.
In this configuration file the module (mod_jk) is loaded by Apache. If
this goes wrong, it probably means that the module has been compiled in
a wrong way.
Important for the lot to work is the JkMount statement: A worker called
"ajp13" is used to forward URI requests stated here, to forward the
request to the Tomcat server. Here the magic comes alive. "Ajp13" is a
connection handler of Tomcat listening to a default port 8009,
connecting Apache to Tomcat.
-
The mod_jk.conf file calls another file called workers.properties which I placed as well in the same directory (/etc/httpd/conf.d/).
Make sure you change the "workers.tomcat_home" and "workers.java_home" lines in "workers.properties" as well.
Finishing touch
-
Restart the Apache daemon:
# /etc/init.d/httpd restart
If this fails, it's probably your module's fault.
-
In the case of the configuration found in my files, if you request the
URL "http://localhost/jsp", Apache connects you through to Tomcat. If
this happens, Tomcat should confront you with a PAGENOTFOUND. Now
create a directory $CATALINA_HOME/webapps/ROOT/jsp, and you should get
an Index listing of your new built directory.
I just created here a JSP page "index.jsp" (with read permissions for Tomcat) with the following line
<% out.println("Hello World"); %>
See if it prints "Hello World".
-
It really works!