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了。