How to setup Apache, Java, and Tomcat on a Windows XP system.

Recently I have been tasked with setting up an application based almost entirely on Java Server Pages. The application can run under either Websphere , BEA , or Apache/Tomcat. Being as frugal as possible, I thought it would be a good idea to test the application under Apache/Tomcat.

I’ve setup Apache several times (I run it at home under SuSE Linux.) and can even get into the mod_rewrite section if needed. 

Being that my workstation at work is XP, I gave it a go. Little did I know that this would be a painstaking adventure.

First, the documentation for installing and configuring Apache under Windows is weak. Second, the documentation for installing and configuring Tomcat under Windows is weaker, and wrong in some cases.

Here’s what I installed:

Apache Web Server 2.0.52
Java Development Kit (J2SE) 5.0 (also known as 1.5 – go figure).
Tomcat 5.5.4


These versions will most certainly be different after a short time. However, the instructions should be good for awhile. I found some documentation out there pointing to some older versions and was able to make my way from there. In order to successfully install the applications, you’ll need to follow the steps in the order below. I’m going to assume you have not installed any of the applications. 

How to do it

First install Apache. You can get it at: http://httpd.apache.org/download.cgi – the most recent version as of this article is 2.0.52. You will want the Win32 binary, which is a MSI file. Instructions are fairly easy, and the apache installer is straightforward. It is for that reason, I won’t go into more detail. Test your installation by opening up your favorite browser (Firefox?) and typing in: http://localhost. You should see the default Apache web page.



Next install Java Development Kit (JDK) 5.0. 

http://java.sun.com/j2se/1.5.0/download.jsp

I went ahead and installed the JDK with NetBeans. Again, instructions are given with the install, so they won’t be provided here. As in Apache, it is an installer application and should give you no problems.

Now for the fun part. Before you do anything else in this document you need to go in and setup an Environment variable. Huh? I hear you saying, “I thought this was Windows XP and not DOS. I shouldn’t be setting up Environment variables… “ 

Tomcat will need to find an environment variable JAVA_HOME to determine where to run the Java engine. I think this is a carryover from the Linux/Unix versions. There you setup the same variable.

In Windows XP, setting an environment variable is easy, if you’re an Admin. First thing I would do is find out where Java is installed. You can do this using explorer. You need the root to the directory. On my system, Java is installed at C:\Program Files\Java\jdk1.5.0_01. You can copy the location from the top of the explorer bar, if you navigate to that directory. 


Right-click on the My Computer icon on your desktop.
Select the Properties menu.
Click the Advanced Tab.
Click the Environment Variables button.
Click on New.
In the Variable Name box, type: JAVA_HOME
In the Variable Value box, type: C:\Program Files\Java\jdk1.5.0_01 (or your directory)
Click OK.

You’re environment box should look like:



If all is good, click OK then OK again. You will be ready to install Tomcat…

Tomcat, which is part of the Apache Jakarta project, can be found here: http://jakarta.apache.org/tomcat/index.html The instructions for setting up on Windows are fairly simple – if you done the previous steps. 

From the installation instructions, Tomcat will be installed as a Windows NT/2K/XP service, no matter what setting is selected. 

You will want to setup the service to run automatically. There will be no tray icon, unless you select it from the shortcut folder created. 

I installed Tomcat 5.5.4 from this location: http://jakarta.apache.org/site/binindex.cgi#tomcat

The actual installer is hard-to find among the list of .tar, .gz, .zip files, so here’s the direct link: http://apache.mirrors.hoobly.com/jakarta/tomcat-5/v5.5.4/bin/jakarta-tomcat-5.5.4.exe

If this mirror is not functional, there will be others. Do install the examples when asked. You can use them to test your installation later.

Assuming you install okay (and select port 8080 by default), typing in http://localhost:8080 will get you the following screen..



You’re almost there!

The final thing – and what almost no one tells you – is how to hook Apache, Tomcat, and Java together. Apparently a future version of Apache will have this functionality built-in, but for now, we have to do it manually. There are only a few steps here.

First, download the JK2 connector. This is the “glue” which allows Tomcat and Java to connect. It can be found at: http://ftp.wayne.edu/apache/jakarta/tomcat-connectors/jk2/binaries/win32/

You want the 2.0.49 file for Apache. Get it here.

It is a zip file. Go ahead and download then open it. Extract the contents to the root of your Apache folder (C:\Program Files\Apache Group\Apache2) and ensure that the “use document folders” option is checked. The important file is one called mod_jk2.so. This is an Apache loadable module. Note that it is placed in the \modules directory. 

As in the instructions which pop up when you open the zip file, create a file called: workers2.properties in the \conf directory. Here’s mine – change your apache directory location if it is different. The rest of the file should be identical:


# shm file location
[shm]
file="C:\Program Files\Apache Group\Apache2\logs"
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:/examples/*]
worker=ajp13:localhost:8009





Finally, you get to edit the apache configuration file. Unlike all other windows apps, apache doesn’t use either an .ini file nor does it use the registry. Dunno why – that’s just the way it is. There is a file located in your Apache directory called httpd.conf. You would be best served if you set .conf files to open with your favorite text editor. (I use html-kit for almost all text editing.) You should be able to get to the httpd.conf file either by browsing in your apache2 directory under conf or by selecting a shortcut in your start menu under Start/Apache HTTP Server 2.0.52/Configure Apache Server/Edit the httpd.conf configuration file. 

Here’s what I added at the end of the file. You can change the path to match your own Apache installation. It simply tells apache where to look for that loadable module you downloaded and extracted to the modules directory. 

[# LoadModule jk2_module “c:\modules/mod_jk2-2.0.43.dll”
LoadModule jk2_module “C:\Program Files\Apache Group\Apache2\modules\mod_jk2.so”


JkUriSet worker ajp13:localhost:8009



JkUriSet worker ajp13:localhost:8009


Save the file and restart both Apache and Tomcat. If you get no errors (and you shouldn’t), you’re done!

Again, enter the tomcat url in your browser: http://localhost:8080. There should be some examples if you installed them. Click on one. I clicked on the cart example at: http://localhost:8080/jsp-examples/sessions/carts.html





Congratulations! You have now installed Apache Tomcat and Java on your Windows machine. You’re now ready to do some serious Java coding. 



Go to it!