目的:
整合Apache和Tomcat,使得Java工程和PHP工程都能共用80連接埠,訪問網站時,無需在地址欄中加連接埠號碼。
環境說明:Linux CentOS 32位Apache 2.2.2Tomcat 7.0.37
準備工作:下載mod_jk.sohttp://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/開啟連結找到合適的檔案下載,下載後改名為mod_jk.so
修改Apache相關檔案1、將mod_jk.so上傳至/modules目錄下2、在/apache/conf目錄下建立檔案mod_jk.conf
#Load mod_jk moduleLoadModule jk_module modules/mod_jk.so#Where to find workers.propertiesJkWorkersFile conf/workers.properties#Where to put jk logsJklogFile logs/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 formatJkRequestLogFormat "%w %V %T"#Send servlet for context /examples to worker named ajp13JkMount /servlet/* ajp13#Send all files under MyProject for to worker named ajp13JKMount /MyProject ajp13
3、在/apache/conf目錄下建立檔案workers.properties
worker.list=ajp13worker.ajp13.port=8009worker.ajp13.host=localhostworker.ajp13.type=ajp13worker.ajp13.lbfactor=1
4、修改/conf/httpd.conf
Listen 80改為Listen 你的IP:80 ServerName localhost:80 最後一行添加 Include conf/mod_jk.conf 找到DocumentRoot,記住它的值,後面有用。我的是/www/apache/htdocs5、添加<VirtualHost>節點
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName www.sample.cn/m
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /MyProject http://www.sample.cn/MyProject/pages/login.jsp
ProxyPassReverse /MyProject http://www.sample.cn/MyProject/pages/login.jsp
</VirtualHost>【注意:】紅字部分的名稱必須一致,也就是說必須和tomcat下的工程名一致
修改Tomcat相關檔案1、在tomcat下添加ROOT.xml 我的目錄是這樣的:/opt/tomcat/conf/Catalina/localhost 建立ROOT.xml <?xml version='1.0' encoding='utf-8'?> <Context displayName="Welcome to Tomcat" docBase="" path="" debug="0"></Context>2、修改/conf/server.xml I.修改預設連接埠號碼(也可以不改,預設一般為8080) <Connector port="9090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> II.檢查AJP連接埠號碼(8009不可修改,若這一句被注釋掉了,取消注釋) <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> III.檢查<HOST>節點 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
-----------------------友情提示-------------------------------------------------------------
至此,就可以通過“網域名稱/工程名/”訪問java工程了,但是請注意,最後的"/"不能少!
如果想要使用者無需添加“/”,請看“在網站目錄後添加反斜線”
----------------------------------------------------------------------------------------------