본문 바로가기

StoreHouse/Server

Gentoo Integrate Tomcat With Apache Using mod_jk

Integrate Tomcat With Apache Using mod_jk

What will be done?
By default Tomcat is setup to use port 8080. In order to let users view your JSP or Servlet pages, they would have to navigate outside of your main Apache site. This also made using SSL in Java pages an impossibility. When linking your Tomcat server with your Apache server, Apache forwards its *.jsp and */servlet/* requests to Tomcat using an intermediate module, mod_jk. There is a more advanced module called mod_jk2 available, but due to its lack of any documentation, very few servers use it. mod_jk is preferred.

Setup Apache
If you already have Apache setup, great, skip this section. Otherwise just
Code:
# emerge sync
# emerge apache

# rc-update add apache2 default
# /etc/init.d/apache2 start


Goto http://localhost to see if Apache works. You can also use Apache 1.3, however I have not tested it and I'll leave it up to you to figure out the best way to include the configuration information.

Setup Tomcat
In the following section you will setup the JSP and Servlet server, Tomcat 4.1.
Code:
# emerge tomcat

There is a bug in the default permissions for a configuation file that does not allow Tomcat to start.
Code:
# chmod 755 /etc/conf.d/tomcat

Now set Tomcat to load at startup and start the server.
Code:
# rc-update add tomcat default
# /etc/init.d/tomcat start

Goto http://localhost:8080 to see if Tomcat works.

mod_jk will only work with Tomcat >= 4.0. If you are not using a JDK from Portage, you may have to specify your JDK's path in /etc/conf.d/tomcat.

Compile mod_jk
Download the latest source to mod_jk and unpack:
Code:
$ wget http://jakarta.apache.org/builds/jakarta-tomcat-connectors/jk/release/v1.2.4/src/jakarta-tomcat-connectors-jk-1.2.4-src.tar.gz
$ tar -xvzf jakarta-tomcat-connectors-jk-1.2.4-src.tar.gz

Now you'll have to prepare your system for the build.
Code:
# mkdir /usr/build
# updatedb
# locate config_vars.mk

The config_vars.mk file should be in something like /usr/lib/apache2/build/ but it may be in a different location if you use Apache 1.3. Copy that file to /usr/build/
Code:
# cp /usr/lib/apache2/build/config_vars.mk /usr/build/

Now we'll compile.
Code:
$ cd jakarta-tomcat-connectors-jk-1.2.4-src/jk/native/
$ ./buildconf.sh
$ ./configure --with-apxs=/usr/sbin/apxs2
$ make

If you are using Apache 1.3 you need to use the configure flag '--with-apxs=/usr/sbin/apxs'.

Now copy the module to a place safe place:
Code:
# cp apache-2.0/mod_jk.so /usr/lib/apache2-extramodules/


Now the fun begins...link 'em up!
Open up /opt/tomcat/conf/server.xml in your favorite text editor. After the line
Quote:

add
Quote:


and after the line
Quote:


add
Quote:


Save the file and restart Tomcat.
Code:
# /etc/init.d/tomcat restart

Wait about a minute (I'm serious...I don't care how fast your computer is) for Tomcat to parse the configuration. You should now have directories called 'auto' and 'jk' in /opt/tomcat/conf/. Copy /opt/tomcat/conf/auto/mod_jk.conf to /etc/apache2/conf/modules.d/. Delete every thing in your newly copied mod_jk.conf except for the JkMount lines. Edit that file to look something like:
Quote:


LoadModule jk_module extramodules/mod_jk.so




JkWorkersFile /opt/tomcat/conf/jk/workers.properties
JkLogFile /opt/tomcat/logs/mod_jk.log
JkLogLevel emerg

JkMount /admin ajp13
JkMount /admin/* ajp13

JkMount /webdav ajp13
JkMount /webdav/* ajp13

JkMount /examples ajp13
JkMount /examples/* ajp13

JkMount /tomcat-docs ajp13
JkMount /tomcat-docs/* ajp13

JkMount /manager ajp13
JkMount /manager/* ajp13


As you see in the new mod_jk.conf there a line defining where the workers.properties file is. The workers.properties is a file that specifies mod_jk's linking type, ports, and loadbalancers if you want them. I have created the most simple workers.properties as possible. You should create this file in /opt/tomcat/conf/jk/.
Quote:
workers.tomcat_home=/opt/tomcat
workers.java_home=/opt/blackdown-jdk-1.4
ps=/

worker.list=ajp13

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

Change the paths as necessary. To enable mod_jk in Apache, edit /etc/conf.d/apache2 to have
Quote:
APACHE2_OPTS="-D JK"

Make sure APACHE2_OPTS is uncommented! Now restart Apache and navigate to http://localhost/tomcat-docs/. Since tomcat-docs has been "JkMount"ed, Apache will forward requests to Tomcat. If you see the Tomcat documentation, then everything has been set up correctly.

Well so much for my first post to the documentation forum. Please give me comments or ask me questions.



출처 : http://forums.gentoo.org/viewtopic.php?t=95184&highlight=modjk