Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Application Server » Tomcat  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [转载]How to connect Tomcat 5 and Apache2
davidself

猫哥

CJSDN高级会员


发贴: 1506
积分: 333
于 2004-08-30 13:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
原文见
http://jroller.com/page/mikechow/20040829#how_to_connect_tomcat_5

How to connect Tomcat 5 and Apache2

"I struggle at everything in life."

William Hung (American Idol contestant)

One of the reasons I created this blog was to provide a place to document all the struggles I had with working on my server. Hopefully, documenting my approach will help others. In addition, it would be nice to receive feedback on things I might have done wrong or even on alternative approaches. The first question is why bother connecting Tomcat with Apache? I guess my only reason, other than curiosity, is I didn't want users to have to specify a port in the url just to access webapps running on my server. I'm sure there are other more important reasons but I'm too lazy to think about them at this time. Feel free to let me know. :)

One of the main problems I had was finding documentation on how to do this using Apache2, Tomcat5, and mod_jk2. There's quite a bit of information on connecting Apache with Tomcat4 and mod_jk. I tried following the approaches for Tomcat4 and mod_jk but I guess there were enough significant changes in mod_jk2 and Tomcat5 that I could never get it to work. I ended up looking in various newsgroups and articles and I began piecing together an approach which finally worked.

Now, some details regarding the software used. This server is currently running:

* Apache 2.0.50
* Tomcat 5.0.27
* mod_jk2 2.0.4

For those who don't know, I'm currently running Gentoo Linux. Consequently, some of the paths I use in this example won't necessarily match the paths from other flavors of Linux such as Fedora, SUSE, Mandrake, etc.



Apache and Tomcat

I'll assume you've already downloaded and installed Apache2 and Tomcat5 and have both up and running. On Gentoo, all I have to do is type emerge apache and emerge tomcat to automatically install/update the latest version from the Portage tree. I've also installed both on RedHat before and it was pretty simple.

"If Yan can cook, so can you"

Martin Yan (Chef)



Building mod_jk2

Depending on your system setup, you'll probably have to change to the root user before doing any of this. The first thing is to grab mod_jk2 from the Jakarta Site. If you're using Fedora or SUSE, binaries have already been built for you. Unfortunate bastards like me using less popular versions of Linux have to compile our own. If you can get the binaries, then skip the rest of this section. To grab the source code, type:

wget http://apache.mirrors.pair.com/jakarta/tomcat-connectors/jk2/jakarta-tomcat-connectors-jk2-src-current.tar.gz

Otherwise, just download the file from one of the many mirrors available at the Jakarta Site. Now, unpack the archive into a directory. I chose /usr/src, but you can put it anywhere you like. You can even delete the whole directory after you're done.

mv jakarta-tomcat-connectors-jk2-src-current.tar.gz /usr/src/
tar -xvzf jakarta-tomcat-connectors-jk2-src-current.tar.gz

The next step is to setup a directory for building the binaries. Again, the directory is totally arbitrary. I'm just showing what I did.

mkdir /usr/build

Now you need to locate file config_vars.mk. On my system, it's available at /usr/lib/apache2/build/. If you're not using Gentoo, simply find it by entering:

updatedb
locate config_vars.mk

Or use find.

Once you have found the location of config_vars.mk, you'll have to copy it into the /usr/build directory.

cp /usr/lib/apache2/build/config_vars.mk /usr/build/

Now go to the directory where you untarred the mod_jk2 source code, and go to the /jk/native2/ directory. Once there, change the permissions on buildconf.sh. Run the script, and then run the configure script. You will need to supply the path to your apxs2 command which should be in /usr/sbin.

cd /usr/src/jakarta-tomcat-connectors-jk2-2.0.4-src/jk/native2/
chmod 0777 buildconf.sh
./buildconf.sh
./configure --with-apxs2=/usr/sbin/apxs2 --with-jni

If configure fails, with a message such as "... Cannot find jni_md.h ...", it's because some of the java header files are platform specific and the build files do not guess right. One way to correct this mistake is to change the /jk/native2/build.xml and modify the build.xml by deleting any references to if="linux". It's important that you don't delete the whole tag, just from if until the end of the tag.

The next step is to build mod_jk2.

make clean build
libtool --finish /usr/lib/apache2

If everything worked, you should have a few files in the /usr/src/jakarta-tomcat-connectors-jk2-2.0.4-src/jk/build/jk2/apache2 directory. You'll need mod_jk2.so and libjkjni.so. You'll have to copy these two files into Apache's extramodules configuration directory. For Gentoo users, this is located at /etc/apache2/extramodules/. If those folder doesn't exist, you'll have to Google for further information since I've only configured mod_jk2 once on this system only.

cd ../build/jk2/apache2
cp mod_jk2.so /etc/apache2/extramodules/
cp libjkjni.so /etc/apache2/extramodules/

You can now delete the source directory. I wouldn't bother unless you need the space on the server and if you're that tight on space, configuring mod_jk2 should be the last thing on your mind.





Configuring Tomcat and Apache

I found this part really confusing because people gave examples from their own custom setup or they were implementing this for an intranet. It took sometime but I finally go it to work. My focus was not on maintaining an intranet so I don't bother with configuring Apache with virtual hosts. If you need to include virtual hosting, there are quite a few articles that you can find through Google to assist you.

First, you have to stop both Apache2 and Tomcat. On Gentoo, I would enter:

/etc/init.d/apache2 stop
/etc/init.d/tomcat5 stop

Since we want Apache to able to use Tomcat, the apache user should be part of the Tomcat group. Simply enter:

/usr/sbin/usermod -Gapache,tomcat apache

Now, you'll have to edit apache2.conf. On Gentoo, this is available at /etc/apache2/conf/apache2.conf (I would advise that you make a copy of apache2.conf before making any changes). After the last "LoadModule ..." line, add:

LoadModule jk2_module /etc/apache2/extramodules/mod_jk2.so

The next step is to edit /opt/tomcat5/conf/jk2.properties. You may need to create this file if it does not already exists. Again, /opt/tomcat5/conf is simply the path on my system. File jk2.properties should be:

# jk2.properties file created 20040805

# Set the handler list
handler.list=channelSock,request

# Linux domain socket location
channelSocket.port=8009

# Dynamic library
serverRoot=/etc/apache2 # Look in the apache2.conf file.
apr.NativeSo=/etc/apache2/extramodules/libjkjni.so

Finally, you'll need to specify a worker. A worker is a Tomcat instance that is waiting to execute servlets on behalf of some web server. In our case, Apache would be forwarding servlet requests to a Tomcat process (the worker) running behind it. You'll need to edit /etc/apache2/conf/workers2.properties. If this file doesn't exist, you'll have to create it.

# workers2.properties file created 20040805

# Shared memory handling. Needs to be set.
[shm]
file=anonymous
size=1048576
debug=0
disabled=0

[channel.socket:localhost:8009]
port=8009
host=127.0.0.1

# Define the worker
[ajp13:localhost:8080]
channel=channel.socket:localhost:8009

# Uri Mapping. Basically specify the webapps you would like accessed through Apache
[uri:/jsp-examples/*] # This should be available with any Tomcat install.
worker=ajp13:localhost:8009

[uri:/roller/*]
worker=ajp13:localhost:8009 # This is an app that I added to Tomcat's webapps/ directory.



Testing it all together

"Dear Red. If you're reading this, you've gotten out. And if you've
come this far, maybe you're willing to come a little further. ..."

Andy Dufresne (From "The Shawshank Redemption)

Now restart Apache and Tomcat

/etc/init.d/apache2 start
/etc/init.d/tomcat5 start

Access the webapp without specify port 8080.

http://www.whateveryoururlis.com/jsp-examples

Well that's it. I've documented everything I did. If you have any questions, please leave a comment. As I said, I've only done this once but if I can help, I will.



--108的上铺--
davidself@twitter

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923