Friday, October 27, 2006

Integrate Tomcat 6 to Apache 2 with mod_jk

Requirements

+ JDK 1.5 or later (http://java.sun.com/javase/downloads/index.jsp - Tomcat 6 cannot run with JDK before 1.5)
+ Apache 2 (http://ftp.apache-kr.org/httpd/binaries/win32/ - already setup and run)
+ mod_jk (http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/)
+ and of course, Tomcat 6 (http://tomcat.apache.org/download-60.cgi)


Our Configuration

+ Java JDK path : C:\jdk1.5.0_09
+ Apache path : C:\www\Apache2
+ Tomcat path : C:\www\Tomcat6
+ Apache's webroot : C:\www\webroot
+ JSP/Servlet webroot (via Apache2/mod_jk) : C:\www\Tomcat6\webapps
+ JSP/Servlet webroot (via native Tomcat server, port 8080) : C:\www\Tomcat6\webapps
+ Access JSP/Servlet pages via Apache2/mod_jk : http://localhost/[jkmount-mapped url of your webapp]
+ Access native Tomcat server : http://localhost:8080/


Install JDK

+ Run file jdk-1_5_0_09-windows-i586-p.exe to install under your %SystemDrive% (C:\), forming C:\jdk1.5.0_09


Unpack and place Tomcat6 and mod_jk

+ Unpack file apache-tomcat-6.0.0.zip under directory C:\www
+ Rename directory C:\www\apache-tomcat-6.0.0 to C:\www\Tomcat6
+ Rename file mod_jk-apache-2.x.xx.so to mod_jk.so, and place under directory C:\www\Apache2\modules


Configuration and Setup

+ Set Environmental Variables : JAVA_HOME=C:\jdk1.5.0_09 and CATALINA_HOME=C:\www\Tomcat6
+ Create empty file C:\www\Apache2\conf\workers.properties, insert code...

workers.tomcat_home=C:/www/Tomcat6
workers.java_home=C:/jdk1.5.0_09
ps=\

# Define worker 'example'
worker.list=example

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

worker.example.cachesize=10
worker.example.cache_timeout=600
worker.example.socket_keepalive=1
worker.example.reclycle_timeout=300

+ Edit file C:\www\Apache2\conf\httpd.conf, insert code...

LoadModule jk_module modules/mod_jk.so

<IfModule mod_jk.c>

JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"

Alias /examples "C:/www/Tomcat6/webapps/examples/"

<Directory "C:/www/Tomcat6/webapps/examples/">
Options Indexes +FollowSymLinks
AllowOverride None
Allow from all
</Directory>

<Location /*/WEB-INF/*>
AllowOverride None
deny from all
</Location>

JkMount /examples/jsp/*.jsp example
JkMount /examples/servlets/* example

</IfModule>

+ Edit file C:\www\Tomcat6\conf\tomcat-users.xml, insert lines...

<role rolename="manager"/>
<user username="manager" password="manager" roles="manager"/>


Install and Start Tomcat

Tomcat can be installed as a Service or started as a Standalone Console application. Note to make sure that Apache has been started at this point.

As a service

+ Open the command-line prompt (Start » Run... cmd.exe)
    C:\www\Tomcat6\bin> service.bat install
    ...> NET START Tomcat6
+ To shutdown the Tomcat Service : ...> NET STOP Tomcat6
+ To remove/uninstall the Tomcat Service : C:\www\Tomcat6\bin> service.bat remove

As a standalone console application

+ Open the Command-Shell (Start » Run: cmd.exe), change to dir C:\www\Tomcat6\bin...
    Run file startup.bat to start Tomcat
    Run file shutdown to shutdown Tomcat


Testing

+ Test the native Tomcat server...
    http://localhost:8080/
    http://localhost:8080/manager/status -- User:manager, Password:manager

+ Test Apache/mod_jk...
    http://localhost/examples/jsp/
    http://localhost/examples/servlets/

3 comments:

Huynh Lap said...

Hi anh Tuấn, em đang thao tác với Apache&tomcat và đọc được bài viết của anh.
Em làm theo hướng dẫn của anh đối với Apache2.2 và Tomcat6 bản rời thì chạy ok nhưng em có vấn đề này muốn hỏi anh, em cài wamp5.1 vì dùng cho 1 số chương trình khác nữa trong đó đã có sẳn apache2, em cũng cấu hình như anh hướng dẫn nhưng start tomcat thì được còn chạy example jsp thì không được (apache trong wamp không start được ).
Mong anh chỉ giúp em, cảm ơn anh nhiều
Văn Lập
(vanlap74@gmail.com)

t800t8 said...

Bạn chạy ví dụ JSP như thế nào? Việc không start được Apache không liên quan gì đến việc chạy ví dụ JSP cả vì bạn start được Tomcat rồi.

Hãy thử chạy bằng URL này xem http://localhost:8080/examples/jsp/

t800t8 said...

One more thing, configuration file for Apache is case-sensitive so if you use "C:/www/Tomcat6", you must enter "C:/www/Tomcat6", not "C:/www/tomcat6", or "c:/www/Tomcat6", etc...