Apache和Tomcat整合之道
2024-08-27 18:29:09
供稿:网友
菜鸟学堂:
准备工作
1. 安装java, apache, tomcat并设置好环境变量
2. 这里假设apache的安装目录为c:/apache group/apache2,tomcat的安装目录为c:/apache group/tomcat 5.0
3. 下载mod_jk,放在任意目录下,这里我放在c:/apache/connapatom下
做完准备工作后就开始二者的整合
1. 在apache的httpd.conf中加入以下内容
# load mod_jk moduleloadmodule jk_module connapatom/mod_jk-1.2.8-apache-2.0.52.so# declare the module for <ifmodule directive>#addmodule mod_jk.c# where to find workers.propertiesjkworkersfile "c:/apache group/apache2/connapatom/workers.properties"# where to put jk logsjklogfile "c:/apache group/apache2/connapatom/mod_jk.log"# set the jk log level [debug/error/info]jkloglevel info# select the log formatjklogstampformat "[%a %b %d %h:%m:%s %y] "# jkoptions indicate to send ssl key size, jkoptions +forwardkeysize +forwarduricompat -forwarddirectories# jkrequestlogformat set the request format jkrequestlogformat "%w %v %t"# send servlet for context /examples to worker named worker1jkmount /*/servlet/ worker1 #(1)# send jsps for context /examples to worker named worker1jkmount /*.jsp worker1 #(2)jkunmount /*.gif worker1jkunmount /*.jpg worker1
2. 注意上面的(1),(2)句,后面再说。这里先在c:/apache group/apache2/connapatom下建立一个文件workers.properties,内容如下
workers.tomcat_home="c:/apache group/tomcat 5.0" #让mod_jk模块知道tomcatworkers.java_home="c:/j2sdk1.4.2_08" #让mod_jk模块知道j2sdk#worker.list=worker1 #list of workers, more workers can be sperated by ','.when starting up, the web server plugin will instantiate the workers whose name appears in the worker.list property, these are also the workers to whom you can map requests.# entries for worker1worker.worker1.type=ajp13 #类型worker.worker1.host=localhost #本机,若上面的apache主机不为localhost,作相应修改worker.worker1.port=8009 #工作端口,若没占用则不用修改worker.worker1.lbfactor=1 #代理数,不用修改
3. 这里我的文件的根目录是d:/www,下面就通过修改apahe和tomcat的配置来实现在此目录下静态网页由apache来处理,动态网页由tomcat来处理:
为此首先要解决的是改变tomcat的根目录,在tomcat5.0以上的做法与以前的版本不同,写一个context片断,放在$catalina_home/conf/[enginename]/[hostname]/ 下,这里写在下文件www.xml,其内容为<context path="" docbase="d:/www"></context>,这里就把tomcat的根目录改到d:/www。对apache,其documentroot也设为d:/www,(1)句jkmount /*.jsp worker1实现了将d:/www里的*.jsp就交由tomcat来处理此时tomcat还不能对servlet进行处理,解决这个问题需要做三个方面的工作:a. 在apache的http.conf里加入alias语句,如在d:/www下建一个文件夹,servletprg专门用来放servlet程序,#alias, so the servlets can be send to tomcatalias /servletprog/ "d:/www/servletprog/"<directory "d:/www/servletpro"> allowoverride none options includesnoexec addoutputfilter includes html addhandler type-map var order allow,deny allow from all </directory>
b. 在http.conf里加入:jkmount /*/servlet/* worker1,也就第(2)句
c. 在tomcat里再写一个context片断,<context path="/servletprog" docbase="d:/www ervletprog" reloadable="true" debug="0"></context>
这样,apache就可以把servletprg里的servlet传给tomcat了。