Practical Web Programming

Friday, July 27, 2007

Installing Tomcat and Apache Web Server in the Same PC

Since the beginning of this week, I tried to install Tomcat and Apache on the same machine and have them share the same default port, the port 80. After three days of "nose bleed", with the help of a co-worker, I succeeded.

For your information, when your install Apache web server, it uses the default port which is port 80, and Tomcat web server on the other hand uses port 8080. If you install them on the same machine, you have to put your JSP/Sevlets files in the Tomcat server htdocs, while your other websites, written in HTML or PHP for example, in the Apache server htdocs. And they have different URLs when you access them. Here's an example:

For websites hosted on Tomcat, such as JPS and servlets, you access your local web directory or htdocs like this:

For websites hosted on Apache, such as PHP, you access your local web directory or htdocs like this:

What I want is to access both by using only one URL, which is http://localhost/

If you are in a testing server or if it doesn't bother you typing different URLs for JSP/Servlets and PHP, this is just fine. But if you are in a production server, surely your users will be very much annoyed typing the port number (8080) when they access a website written in JSP or servlets. Fortunately, there's a solution, here's a step-by-step of what I did.

1) Install Tomcat and Apache
I installed Apache version 2.2.4 for Windows which I downloaded here and Tomcat version 5.5.20 also for Windows which I downloaded from here in "C:\Program Files\Apache Software Foundation" directory of my PC.

2) Add
I downloaded from here and add it in "C:\Program Files\Apache Software Foundation\Apache2.2\modules" directory.

3) Add
I created a file and put in "C:\Program Files\Apache Software Foundation\Apache2.2\conf" directory. Inside the file I added the text below:


4) Add mod_jk.conf
I also created a mod_jk.conf file and put in "C:\Program Files\Apache Software Foundation\Tomcat 5.5\conf" directory. Inside the mod_jk.conf file I added the text below:

# Load mod_jk module
LoadModule jk_module modules/
# Where to find
JkWorkersFile conf/
# Where to put jk shared memory
JkShmFile logs/mod_jk.shm
# Where to put jk logs
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# Send servlet for context to worker named locworker
#JkMount /* locworker
# Send JSPs for context /examples to worker named locworker
JkMount /*.jsp locworker
JkMount /*.war locworker

5) How to Test
Create a JSP and HTML files with the source codes below and put it in your Apache web directory or htdocs (no need to add those files in your Tomcat web directory).

<title>Test Page</title>
out.println("Hello World from JSP!");

<title>Test Page</title>
Hello World from HTML!

Now test if it by accessing it through this URL: http://localhost/test.html or http://localhost/test.jsp. You should see the both web pages without error.


Recent Post