Apache+Tomcat的整合与负载均衡

参考文档:http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

  1. 环境:CentOS5.8 64bit
  2. 安装Apache
    这里我选择了yum 方式安装:yum install httpd  httpd-devel
  3. 安装好之后,下载apache-tomcat  connector,去页面http://tomcat.apache.org/download-connectors.cgi查找一下linux下的源代码包,下载: wget http://labs.mop.com/apache-mirror//tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz
  4. 解压并且进入代码目录: tar -xf tomcat-connectors-1.2.37-src.tar.gz && cd tomcat-connectors-1.2.37-src/native/
  5. 编译mod_jk.so模块: ./configure --with-apxs=/usr/sbin/apxs (注意后面跟的参数是关键,如果你编译安装apxs可能在别的目录)
    make
  6. 拷贝模块文件到apache默认的模块目录: cp apache-2.0/mod_jk.so  /etc/httpd/modules/
  7. 在apache配置文件内引入该模块: vi /etc/httpd/conf/httpd.conf
    插入  LoadModule jk_module modules/mod_jk.so
    在apache的 conf.d目录下,添加有关mod_Jk的配置: vi /etc/httpd/conf.d/mod_jk.conf
    ###### 指出mod_jk模块工作所需要的工作文件workers.properties的位置
    JkWorkersFile /etc/httpd/conf/workers.properties
    
    ###### Where to put jk logs
    JkLogFile /var/log/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

     

  8. 添加workers.properties文件: vi /etc/httpd/conf/workers.properties
    worker.list=worker1,worker2,worker3,worker4,worker5,loadbalance1,jkstatus
    
    ####### Set properties for worker1
    worker.worker1.type=ajp13
    worker.worker1.host=localhost
    worker.worker1.port=8031
    worker.worker1.lbfactor=50
    worker.worker1.socket_keepalive=1
    worker.worker1.socket_timeout=300
    
    ####### Set properties for worker2
    worker.worker2.type=ajp13
    worker.worker2.host=localhost
    worker.worker2.port=8033
    worker.worker2.lbfactor=50
    worker.worker2.socket_keepalive=1
    worker.worker2.socket_timeout=300
    
    ####### Set properties for worker3
    worker.worker3.type=ajp13
    worker.worker3.host=localhost
    worker.worker3.port=8035
    worker.worker3.lbfactor=50
    worker.worker3.socket_keepalive=1
    worker.worker3.socket_timeout=300
    
    ####### Set properties for worker4
    worker.worker4.type=ajp13
    worker.worker4.host=localhost
    worker.worker4.port=8037
    worker.worker4.lbfactor=50
    worker.worker4.socket_keepalive=1
    worker.worker4.socket_timeout=300
    
    ####### Set properties for worker5
    worker.worker5.type=ajp13
    worker.worker5.host=localhost
    worker.worker5.port=8039
    worker.worker5.lbfactor=50
    worker.worker5.socket_keepalive=1
    worker.worker5.socket_timeout=300
    
    ##set loadbalance 1 这里是负载均衡部分,使用了session sticky功能
    worker.loadbalance1.type=lb
    worker.loadbalance1.balance_workers=worker1,worker2,worker3,worker4,worker5
    worker.loadbalance1.sticky_session=true
    
    # Define a 'jkstatus' worker using status
    worker.jkstatus.type=status
  9. 在apache主配置文件内编辑virtualhost
    <VirtualHost *:80>
        ServerAdmin webmaster@testserver.com
        DocumentRoot /www/docs/www.testserver.com
        ServerName www.testserver.com
        ErrorLog logs/www.testserver.com-error_log
        CustomLog logs/www.testserver.com-access_log common
        <Location /*>
            JkMount   loadbalance1
        </Location>
       <Location /jkmanager/>
          JkMount jkstatus
          Order deny,allow
          Deny from all
          Allow from 192.168.1.1
       </Location>
    </VirtualHost>

     

  10. 重启apache: /etc/init.d/httpd restart

注:引用本文,请注明来自:http://www.cnblogs.com/helloLinux/archive/2012/07/02/2573297.html

 

附录:Ubuntu12.04 环境下

安装apache: apt-get install apache2

安装devel包: apt-get install apache2-dev

apxs现在叫做 apxs2

posted @ 2012-07-02 16:21  Hello! Linux 博客  Views(811)  Comments(0Edit  收藏  举报