Sunday, January 22, 2012

Integrate Tomcat with Apache HTTP Server: Forward JSP/Servlet Content to Tomcat

Usually we want serve static content or php by Apache, only serve JSP/Servlet content by Tomcat.
We can do this by several steps, described as follows:

Steps:

Step 1.
Get Apache HTTP Server:
go to http://httpd.apache.org/,
click from a mirror then download (I choose 2.2.21) and install


The document root of Apache HTTP Server is
APACHE_HOME/htdocs
(APACHE_HOME = [Install path]/Apache2.2)
You can change it by modify  APACHE_HOME /conf/httpd.conf

Create an icon named favicon.ico,
put it into APACHE_HOME/htdocs
You may need the png2ico to help this


Step 2.
Get Tomcat:
go to http://tomcat.apache.org/
click Tomcat 7.0 then download and install
(I just extract it)
then set TOMCAT_HOME Environment Variable

Step 3.
Copy htdocs/index.html and rename it to index.jsp,
put it at APACHE_HOME/htdocs/JEE/Test
(or put Test under TOMCAT_HOME/webapps if you skip Step 4.4)

currently it will show all html if you visit http://localhost/Test/index.jsp

Step 4.
Connect Apache HTTP Server and tomcat as follows:

Step 4.1.
Get mod_jk from http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/
I choose tomcat-connectors-1.2.32-windows-i386-httpd-2.2.x.zip
Extract it and find mod_jk.so in it
Put mod_jk.so into  APACHE_HOME /modules

Step 4.2.
Create workers.properties,
Put it into APACHE_HOME /conf
Edit it as below

# Define worker 'worker1'
worker.list=worker1

# Set properties for worker 'worker1' (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

worker.worker1.cachesize=10
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.recycle_timeout=300

For more information of workers.properties, please refer to
http://tomcat.apache.org/connectors-doc/reference/workers.html

Step 4.3.
Create folder C:/var/log/httpd
Edit APACHE_HOME /conf/http.conf, append

# 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     /var/log/httpd/mod_jk.shm
# Where to put jk logs
JkLogFile     /var/log/httpd/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 /Test to worker named worker1
JkMount  /Test/* worker1

This line JkMount /Test/* worker1 denotes forward the context path match /Test/* to Tomcat,
you can add multiple JkMount to define multiple path

Step 4.4.
This step should be skipped if you put Test under TOMCAT_HOME/webapps at Step 3
Edit TOMCAT_HOME/conf/server.xml

Change

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

to

<Host name="localhost"  appBase="APACHE_HOME\htdocs\JEE"
            unpackWARs="true" autoDeploy="true">

The Apache HTTP Server is connected to Tomcat now,
the only thing left to do is restart Apache HTTP Server and start Tomcat

Step 5.
Restart Apache HTTP Server:

Click the icon at System Tray


then click Apache2.2 -> Restart

Step 6.
Start Tomcat as a service,

At this point, make sure you have set the environment variables correctly,
for example:



Open cmd.exe as Administrator (Right click on cmd.exe -> Run as Administrator)
cd to TOMCAT_HOME/bin
Execute

service.bat install

NET START Tomcat7


Now you can visit http://localhost/Test/index.jsp and see your JSP page displayed correctly.

Download:
The resources can be downloaded from github
https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Environment

References:
http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html
http://tomcat.apache.org/connectors-doc/reference/workers.html
http://tomcat.apache.org/tomcat-4.1-doc/config/host.html
http://www.devside.net/guides/windows/tomcat

No comments:

Post a Comment