浅谈apache与tomact的整合

一般来讲,配置apache和tomcat的整合有三种方式,mod_proxy,mod_ajp和mod_jk等三种方式,前2种配置步骤类似且相对容易,且只支持apache2.2以后的版本,mod_jk则配置相对复杂,但强在稳定性和性能方面,因为没有系统的写过这方面的文档,故在此简要的记录下使用mod_proxy和mod_jk两种方式来整合apache和tomcat!

三种方式的对比,具体请参考:http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/

本文环境介绍:
os版本:centos5.4 64bit
jdk版本:jdk-6u2-linux-i586.bin
tomcat版本:7.0.29
apache版本:2.4.2
apache服务器IP:192.168.123.110/24
tomcat实例一IP:192.168.123.110/24 (端口默认)
tomcat实例二IP:192.168.123.20/24 (端口默认)

一:mod_proxy方式的整合
1:编译安装apache,需要编译proxy相关的模块,这里采用目前最新版的apache2.4.2

   
   
   
   
  1. [root@db1 ~]# tar -zxvpf httpd-2.4.2.tar.gz   
  2. [root@db1 ~]# cd httpd-2.4.2  
  3. [root@db1 httpd-2.4.2]# ./configure --prefix=/usr/local/apache --enable-proxy --enable-proxy-ajp  --enable-rewrite --enable-so  --enable-headers --enable-expires  --enable-modules-shared=most --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/  
  4. [root@db1 httpd-2.4.2]# make   
  5. [root@db1 httpd-2.4.2]# make install 

若启动apache出现下面的错误,基本上属于apr的问题,要么apr没安装,要么版本不对!

[root@db1 ~]# /usr/local/apache/bin/apachectl -t
httpd: Syntax error on line 113 of /usr/local/apache/conf/httpd.conf: Cannot load /usr/local/apache/modules/mod_proxy.so into
server: /usr/local/apache/modules/mod_proxy.so: undefined symbol: apr_global_mutex_lockfile

[root@db1 ~]# yum remove apr
[root@db1 ~]# /usr/local/apache/bin/apachectl -t
Syntax OK

[root@db1 ~]# /usr/local/apache/bin/apachectl -t -D DUMP_MODULES |grep proxy
 proxy_module (shared)
 proxy_connect_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_fcgi_module (shared)
 proxy_scgi_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_express_module (shared)

2:配置apache的虚拟主机

   
   
   
   
  1. [root@db1 ~]# /usr/local/apache/bin/apachectl -t -D DUMP_VHOSTS  
  2. VirtualHost configuration:  
  3. *:80                   is a NameVirtualHost  
  4.          default server 192.168.123.110 (/usr/local/apache/conf/extra/httpd-vhosts.conf:23)  
  5.          port 80 namevhost 192.168.123.110 (/usr/local/apache/conf/extra/httpd-vhosts.conf:23)  
  6.          port 80 namevhost 192.168.123.110 (/usr/local/apache/conf/extra/httpd-vhosts.conf:23)  
  7.          port 80 namevhost www.yang.com (/usr/local/apache/conf/extra/httpd-vhosts.conf:31)  
  8.          port 80 namevhost www.yang.com (/usr/local/apache/conf/extra/httpd-vhosts.conf:31) 

若启动apache报错如下,则需要加载mod_slotmem_shm.so模块,之前在使用apache中很少用到mod_proxy模块,所以遇到的问题多多!

[root@db1 ~]# /usr/local/apache/bin/apachectl -k start
[root@db1 ~]# echo $?
0
[root@db1 ~]# netstat -ntpl |grep :80
[root@db1 ~]# cat /usr/local/apache/logs/error_log
[Fri Aug 10 18:32:25.298436 2012] [proxy_balancer:emerg] [pid 7859:tid 47937562389216] AH01177: Failed to lookup provider
'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Fri Aug 10 18:32:25.298650 2012] [:emerg] [pid 7859:tid 47937562389216] AH00020: Configuration Failed, exiting
[Fri Aug 10 18:34:04.717801 2012] [proxy_balancer:emerg] [pid 7876:tid 47508265659104] AH01177: Failed to lookup provider
'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Fri Aug 10 18:34:04.717951 2012] [:emerg] [pid 7876:tid 47508265659104] AH00020: Configuration Failed, exiting

[root@db1 ~]# grep 'slotmem' /usr/local/apache/conf/httpd.conf
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so

[root@db1 ~]# /usr/local/apache/bin/apachectl -k start
[root@db1 ~]# netstat -ntpl |grep :80
tcp        0      0 :::80                       :::*                        LISTEN      7898/httpd

注意:2.4.2版本的apache中需要在目录下面添加 Require all granted,否则无论怎么访问都是403,悲了个催的!具体报错如下:

[Fri Aug 10 19:07:13.263893 2012] [authz_core:error] [pid 8918:tid 1126259008] [client 192.168.123.102:53420] AH01630: client  denied by server configuration: /tmp

   
   
   
   
  1. [root@db1 ~]# grep -v '^#' /usr/local/apache/conf/extra/httpd-vhosts.conf |grep -v '^$'  
  2. <VirtualHost *:80> 
  3.     ServerName 192.168.123.110   
  4.     <Location /> 
  5.     Order deny,allow  
  6.     Deny from all  
  7.     </Location> 
  8. </VirtualHost> 
  9. <VirtualHost *:80> 
  10.     ServerName   www.yang.com  
  11.     DocumentRoot /tmp  
  12.     <Directory /tmp> 
  13.      Options +indexes  
  14.      Order allow,deny  
  15.      Allow from all  
  16.      Require all granted   
  17.     </Directory> 
  18. </VirtualHost> 

 

浅谈apache与tomact的整合_第1张图片 

 

浅谈apache与tomact的整合_第2张图片 

浅谈apache与tomact的整合_第3张图片

3:安装jdk和tomcat,并启动tomcat

   
   
   
   
  1. [root@db2 ~]# java -version  
  2. java version "1.6.0"  
  3. OpenJDK  Runtime Environment (build 1.6.0-b09)  
  4. OpenJDK 64-Bit Server VM (build 1.6.0-b09, mixed mode)  
  5.  
  6. [root@db2 ~]# /usr/local/tomcat7/bin/startup.sh   
  7. Using CATALINA_BASE:   /usr/local/tomcat7  
  8. Using CATALINA_HOME:   /usr/local/tomcat7  
  9. Using CATALINA_TMPDIR: /usr/local/tomcat7/temp  
  10. Using JRE_HOME:        /usr/local/java  
  11. Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar  
  12.  
  13. [root@db2 ~]# netstat -ntpl |grep java  
  14. tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      7601/java             
  15. tcp        0      0 :::8009                     :::*                        LISTEN      7601/java             
  16. tcp        0      0 :::8080                     :::*                        LISTEN      7601/java  

浅谈apache与tomact的整合_第4张图片

4:使用mod_proxy转发jsp的请求,test目录,tomcat.css,tomcat.png请求使用apache处理

   
   
   
   
  1. <VirtualHost *:80> 
  2.     ServerName   www.yang.com  
  3.     DocumentRoot /tmp  
  4.     <Directory /tmp> 
  5.      Options +indexes  
  6.      Order allow,deny  
  7.      Allow from all  
  8.      Require all granted  
  9.     </Directory> 
  10.     Proxypass /test !  
  11.     Proxypass /tomcat.css !  
  12.     Proxypass /tomcat.png !  
  13.     Proxypass / http://192.168.123.20:8080  
  14. </VirtualHost> 

5:mod_proxy配置集群

   
   
   
   
  1. <VirtualHost *:80> 
  2.     ServerName   www.yang.com  
  3.     DocumentRoot /tmp  
  4.     <Directory /tmp> 
  5.      Options +indexes  
  6.      Order allow,deny  
  7.      Allow from all  
  8.      Require all granted   
  9.     </Directory> 
  10.     Proxypass /test !  
  11.     Proxypass /tomcat.css !  
  12.     Proxypass /tomcat.png !  
  13.     Proxypass / balancer://yang  
  14.     <Proxy balancer://yang/> 
  15.     BalancerMember http://192.168.123.20:8080/  
  16.     BalancerMember http://192.168.123.110:8080/  
  17.     </Proxy> 
  18. </VirtualHost> 

6:测试 

 

浅谈apache与tomact的整合_第5张图片

 二:mod_jk方式整合
1:下载并编译安装mod_jk模块

   
   
   
   
  1. [root@db1 ~]# wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz  
  2. [root@db1 ~]# tar -zxvf tomcat-connectors-1.2.37-src.tar.gz   
  3. [root@db1 ~]# cd tomcat-connectors-1.2.37-src/native/  
  4. [root@db1 native]# ./configure --with-apxs=/usr/local/apache/bin/apxs   
  5. [root@db1 native]# make && make install  
  6.  
  7. [root@db1 ~]# grep 'mod_jk' /usr/local/apache/conf/httpd.conf   
  8. Include conf/extra/mod_jk.conf 

2:修改mod_jk配置文件如下

   
   
   
   
  1. [root@db1 ~]# grep -v '^#' /usr/local/apache/conf/extra/mod_jk.conf   
  2. LoadModule jk_module modules/mod_jk.so  
  3. JkWorkersFile conf/extra/workers.properties  
  4. JkLogFile "logs/mod_jk.log"  
  5. JkLogLevel info  
  6. JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"  
  7. JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  
  8. JkRequestLogFormat "%w %V %T"  
  9. JkShmFile logs/jk.shm  
  10. HostnameLookups Off 

3:配置workers.properties,指定ajp连接tomcat的信息

   
   
   
   
  1. [root@db1 ~]# cat /usr/local/apache/conf/extra/workers.properties   
  2. worker.list=yang,jkstatus  
  3. #110   
  4. worker.server110.port=8009 
  5. worker.server110.host=192.168.123.110  
  6. worker.server110.type=ajp13 
  7. worker.server110.lbfactor=0  
  8. worker.server110.socket_keepalive=1  
  9. worker.server110.socket_timeout=0  
  10. worker.server110.retries=3 
  11.  
  12. #20  
  13. worker.server20.port=8009 
  14. worker.server20.host=192.168.123.20  
  15. worker.server20.type=ajp13 
  16. worker.server20.lbfactor=9 
  17. worker.server20.socket_keepalive=1 
  18. worker.server20.socket_timeout=0  
  19. worker.server20.retries=3 
  20.  
  21. worker.yang.type=lb 
  22. worker.retries=3 
  23. worker.yang.balance_workers=server110, server22  
  24. worker.yang.sticky_session=true 
  25. worker.yang.sticky_session_force=true 
  26.  
  27. worker.jkstatus.type=status  
  28. worker.jkstatus.read_only=true 

4:配置虚拟主机,jkmount_yang.conf文件指定了客户端http请求的走向,test目录下的请求交给apache处理

   
   
   
   
  1. <VirtualHost *:80> 
  2.     ServerName   www.yang.com  
  3.     DocumentRoot /tmp  
  4.     Include  conf/extra/jkmount_yang.conf  
  5.     <Directory /tmp> 
  6.      Options +indexes  
  7.      Order allow,deny  
  8.      Allow from all  
  9.      Require all granted   
  10.     </Directory> 
  11. </VirtualHost> 
  12.  
  13. [root@db1 ~]# cat /usr/local/apache/conf/extra/jkmount_yang.conf   
  14. JkMount   /*              yang  
  15. JkMount   /status         jkstatus  
  16. JkunMount /test/*         yang 

5:重启apache

   
   
   
   
  1. [root@db1 ~]# /usr/local/apache/bin/apachectl -t  
  2. Syntax OK  
  3. [root@db1 ~]# /usr/local/apache/bin/apachectl -k restart 

6:修改tomcat配置文件如下,重启tomcat实例,文件中主要修改jvmroute参数,同workers.properties中的一致

[root@db1 ~]# cat sr/local/tomcat7/conf/server.xml
<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN"> 
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="server110">

<!-- 配置集群session 同步相关 --> 
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">          
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/> 
             
          <Channel className="org.apache.catalina.tribes.group.GroupChannel"> 
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.5"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="192.168.123.110"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>
            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>
          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster> 
<!-- 配置集群session 同步相关 -->
   
      <Realm className="org.apache.catalina.realm.LockOutRealm">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

7:测试

浅谈apache与tomact的整合_第6张图片

 

浅谈apache与tomact的整合_第7张图片

 

浅谈apache与tomact的整合_第8张图片

[root@db1 ~]# tail -f  /usr/local/apache/logs/mod_jk.log
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.017531
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.003080
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.001009
[Thu Aug 16 16:45:23 2012]yang www.yang.com 0.001876


你可能感兴趣的:(浅谈apache与tomact的整合)