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:
http://localhost:8080/

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

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 mod_jk.so
I downloaded mod_jk.so from here and add it in "C:\Program Files\Apache Software Foundation\Apache2.2\modules" directory.

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

worker.list=locworker
worker.locworker.port=8009
worker.locworker.host=localhost
worker.locworker.type=ajp13


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/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf/workers.properties
# 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).

test.jsp
<html>
<head>
<title>Test Page</title>
</head>
<body>
<%
out.println("Hello World from JSP!");
%>
</body>
</html>


test.html
<html>
<head>
<title>Test Page</title>
</head>
<body>
Hello World from HTML!
</body>
</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.

0 comments:

Recent Post