天天看點

apache & jboss 簡單整合

簡單整合apache 和 jboss 步驟如下(暫不考慮叢集之類): 

參考:

    http://man.chinaunix.net/newsoft/jboss.htm

    http://www.itlearner.com/code/apache2.2/vhosts/index.html

     首先配置apache,在conf 目錄下,建立mod_jk.conf檔案,内容如下:

    LoadModule jk_module modules/mod_jk.so

JkWorkersFile /usr/local/apache2/conf/workers.properties
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%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"

JkMount /servlet/*  worker1  
JkMount /*.jsp worker1 

#JkMount /* loadbalancer
#apache will serve the static picture.
#以下指令意味着所有的圖檔将由APACHE解析
#JkUnMount /*.jpg loadbalancer 
#JkUnMount /*.gif loadbalancer
#JkUnMount /*.swf loadbalancer
#JkUnMount /*.bmp loadbalancer
#JkUnMount /*.png loadbalancer      

    建立worker.properties  内容如下:

#worker.list=loadbalancer,server105,server106
 
worker.list=worker1 
# Set properties for worker1 

worker.worker1.type=ajp13
worker.worker1.host=localhost  
worker.worker1.port=8009 
worker.worker1.lbfactor=50  
worker.worker1.cachesize=10 
worker.worker1.cache_timeout=600  
worker.worker1.socket_keepalive=1 
worker.worker1.socket_timeout=300  


# Define the first node...
#worker.server105.port=9090
#worker.server105.host=127.0.0.1
#worker.server105.type=ajp13
#worker.server105.lbfactor=1
##worker.server105.local_worker=1
#worker.server105.cachesize=100
 
# Define the 2nd node...
#worker.server106.port=9090
#worker.server106.host=127.0.0.1
#worker.server106.type=ajp13
#worker.server106.lbfactor=1
#worker.server106.local_worker=1
#worker.server106.cachesize=100
 
# Now we define the load-balancing behaviour
#worker.loadbalancer.type=lb
#worker.loadbalancer.balanced_workers=server105,server106
#worker.loadbalancer.sticky_session=1       

   最後需要在httpd.conf 加入:

Include conf/mod_jk2.conf      

      這時候啟動會報找不到mod_jk.so的錯誤,需要手工生成,先去http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/source/ 這個位址下載下傳個jakarta-tomcat-connectors-jk-1.2.6-src.tar.tar      然後就可以編譯生成:       

# tar xzvf jakarta-tomcat-connectors-jk-1.2.6-src.tar.tar
# cd jakarta-tomcat-connectors-jk-1.2.6-src/jk/native 
# ./configure --with-apxs=/usr/local/apache2/bin/apxs 
# make 
# cp ./apache-2.0/mod_jk.so /usr/local/apache2/modules/ 
      

    到這裡apache 部配置設定置完成,接下來jboss,其實jboss都不用配置,修改個端口,别跟apache端口沖突就可以了,預設是8080,改個9090之類的就可以,配置檔案在jboss-4.0.5.GA/server/default/deploy/jbossweb-tomcat55.sar/server.xml 。

    到這裡完成啦,搞一個war包,裡面簡單弄個jsp檔案,源碼比如:

Hello ~~~~
圖檔:<img src="10.jpg"/>
           

  把war放到jboss default/deploy目錄下,啟動jboss  和 apache ,通過apache端口通路war包内的jsp檔案試試,可以了吧~~

   然後問題來了, 我的html檔案是放在apache處,war包名為Test,那我通路工程檔案是http://www.test.com:8080/Test/index.html,但是這樣是通路不到的,因為mod的規則是,selvet和jsp是跳轉到jboss,html是在apache,但是apache沒有建立Test目錄,怎麼樣讓他跳轉到apache處指定目錄下的同名靜态檔案呢?

  需要在httpd.con 對應的VirtualHost中加上RewriteRule ^/WebTest/(.*).html /$1.html [R,NE], 這樣通路就會跳轉了。

  同樣的,在jsp中使用的jpg檔案,需要跳轉到apache的預設目錄,則需要加上RewriteRule ^/WebTest/(.*).jpg /$1.jpg [R,NE];

  完整的httpd.conf VirtualHost配置:

<VirtualHost *>
   ServerAdmin [email protected]
   DocumentRoot /usr/local/apache2/htdocs/sefer
   ServerName www.test.com
   ErrorLog logs/test.sefer.com-error_log
   TransferLog logs/test.sefer.com-access_log
   RewriteEngine On
   RewriteRule /test/(.*) /index.html [R,NE]
   RewriteRule /out/(.*) http://www.163.com [R,NE]
   RewriteRule ^/WebTest/(.*).html /$1.html [R,NE]
   RewriteRule ^/WebTest/(.*).jpg /$1.jpg [R,NE]
</VirtualHost>      

 這樣圖檔也能出來了,通路http://www.test.com:8080/WebTest/index.html 直接跳轉到http://www.test.com:8080/index.jsp。