星期五, 十一月 04, 2005

apache+tomcat+jk2_jni

今天忙活和一天,终于搞出来apache和tomcat使用jni整合配置,用的jni方式通信,不用让tomcat启动,用的时候apache会自动启动tomcat的。把配置过程共享出来,希望对其他人能有帮助。我是在windowsxp下试成功的,linux下没成功,可能是apache版本太低,如果有人作出来了希望能不吝赐教!
1) (八股?!)下载软件安装
apache2.0.49
tomcat5.19
jk2.0.2
在那下载自己找,我就不提供了吧?
装好后启动服务器,先测试一把
http://localhost           以该能看到apache的默认页
http://localhost:8080      应该能看到tomcat的默认页面
测试成功则关闭apache和tomcat并把tomcat的启动方式改为手动再继续

2)安装jk2
把下载到的jk解压,把那个dll文件改名为mod_jk2.dll(你也可以改成其他名字或不改,以下的过程也跟着变就是了)放到[apache安装目录]\modules下
编辑httpd.conf加入
    LoadModule jk2_module modules/mod_jk2.dll
重启apache,成功则关闭apache继续(不成功能不能继续我没试过:-)

3)编辑配置文件
a)workers2.properties
在[apache安装目录]\conf下新建一个文件,命名为workers2.properties
内容如下,注意,根据你服务器的安装路径可能要作修改(下面有提示的地方)!
[config:]
file=${serverRoot}/conf/workers2.properties
debug=0
debugEnv=0

[shm:]
disabled=1

#在tomcat4中从这里开始到下一个标记处不需要
# Example socket channel, override port and host.
[channel.socket:localhost:8009]                    
port=8009
host=127.0.0.1

# define the worker
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
#在tomcat4中从这里到上一个标记之间的不需要

[channel.jni:jni]
info=The jni channel, used if tomcat is started inprocess

[vm:]
info=Parameters used to load a JVM in the server process
classpath=D:/server/Tomcat5/lib                 #可能需要修改
OPT=-Dtomcat.home=D:/server/Tomcat5             #可能要修改
OPT=-Dcatalina.home=D:/server/Tomcat5           #可能要修改
OPT=-Xmx128M
disabled=0

[worker.jni:onStartup]
info=Command to be executed by the VM on startup. This one will start tomcat.
class=org/apache/jk/apr/TomcatStarter
ARG=start
disabled=0
stdout=${serverRoot}/logs/stdout.log
stderr=${serverRoot}/logs/stderr.log

[worker.jni:onShutdown]
info=Command to be executed by the VM on shutdown. This one will stop tomcat.
class=org/apache/jk/apr/TomcatStarter
ARG=stop
disabled=0

[status:]
info=Status worker, displays runtime informations

[uri:/jkstatus/*]
info=Display status information and checks the config file for changes.
group=status:

[uri:/jsp-examples/*]
info=Map the whole webapp (for tomcat5)

[uri:j.e16.dhs.org/*.jsp]                  #虚拟主机用,根据你的域名修改
info=e16 test site

[uri:j.e16.dhs.org/servlet/*]              #也是虚拟主机映射
info=servlets

然后在[tomcat安装目录]下新建个目录,命名为lib,把tomcat带的所有jar文件解压到这个目录下(有点劲大,但这样最省事,好像)。

b)jk2.properties
好像[tomcat安装目录]\conf下本来就有,编辑它,内容如下:
## THIS FILE MAY BE OVERRIDEN AT RUNTIME. MAKE SURE TOMCAT IS STOPED
## WHEN YOU EDIT THE FILE.

## COMMENTS WILL BE _LOST_

## DOCUMENTATION OF THE FORMAT IN JkMain javadoc.

# Set the desired handler list
handler.list=apr,request,channelJni        #这行原来是被注释掉的
#
# Override the default port for the socketChannel
# channelSocket.port=8019
# Default:
# channelUnix.file=${jkHome}/work/jk2.socket
# Just to check if the the config  is working
# shm.file=${jkHome}/work/jk2.shm

# In order to enable jni use any channelJni directive
# channelJni.disabled = 0
# And one of the following directives:

# apr.jniModeSo=/opt/apache2/modules/mod_jk2.so

# If set to inprocess the mod_jk2 will Register natives itself
# This will enable the starting of the Tomcat from mod_jk2
apr.jniModeSo=inprocess                   #这行也是
下面,激动人心……
启动apache,打开浏览器,输入
http://localhost/jsp-examples/
看到JSP Samples页面就成功了(刚启动后马上可能看不到,稍微等一会,要十分钟后还看不到则肯定是挂了。

4)虚拟主机
上面配置的是让tomcat替apache解析jsp-examples目录下的所有文件,也可以把整个虚拟主机交给tomcat(假设域名是j.e16.dhs.org)
a)编辑httpd.conf,在最后加入
NameVirtualHost *:80
<VirtualHost *:80>
     DocumentRoot f:/jwebapp      #目录根据你的需要改
     serverName j.e16.dhs.org
</VirtualHost>

<Directory f:/jwebapp>               #和上面保持一致
     AllowOverride All            #这里为了一会设定web-inf权限
     Options +Indexes             #这里可以不要,为了调试程序方便
</Directory>
b)编辑workers2.properties,加入
[uri:j.e16.dhs.org/*.jsp]
info=e16 test site

[uri:j.e16.dhs.org/servlet/*]
info=servlets
#这样你就把所有虚拟主机下的jsp和servlet交给tomcat了,但还要在tomcat下作相应的配置
c)编辑server.xml,加入
     <Host name="j.e16.dhs.org" debug="0" appBase="f:/jwebapp" unpackWARs="true" autoDeploy="true">

          <Context path="" docBase="" debug="1"/>

          <Valve className="org.apache.catalina.valves.AccessLogValve"
                 directory="logs"  prefix="e16j." suffix=".txt"
                 pattern="common" resolveHosts="false"/>
     </Host>
<!--地方自己找,原来应该有个host标记,和它放在一起-->
d)对web用户隐藏web-inf目录
这一步是为安全考虑
新建一个文件.htaccess,内容如下
Options None
Order deny,allow
Deny from all
在每个web-inf下放一个
启动apache,试试http://j.e16.dhs.org/        把域名改成你的域名,应该会有个惊喜

补充:
上面的虚拟主机(j.e16.dhs.org)不会影响php和perl的执行。用tomcat4(我试了4.1.30)以上配置不用改变,而且可以按照提示去掉一些东西(我不知道为什么)。这里我在winxp下虽然配置成功了,但原理仍然不是太懂,如果谁很清楚的话望能赐教。附带的附件是我的配置文件(正在运行的)。欢迎指正以上可能存在的错误,我的BMYid是neilzheng。

没有评论: