踏雪寻梅 发表于 2018-11-25 11:10:17

整合 Apache 与 Tomcat

  一、 Tomcat连接器的架构
1. 基于Apache做为Tomcat前端的架构来讲,Apache可以通过mod_jk、或mod_proxy模块与后端的Tomcat进行数据交换。
2. 对Tomcat来说,每个Web容器实例都有一个Java语言开发的连接器模块组件
3. HTTP/1.1负责响应基于HTTP/HTTPS协议的请求,AJP/1.3负责响应基于AJP的请求。            
二、 连接器协议
      Tomcat的Web服务器连接器支持两种协议:AJP和HTTP,它们均定义了以二进制格式在Web服务器和Tomcat之间进行数据传输,并提供相应的控制命令。
    AJP(Apache JServ Protocol)协议:
         AJP 协议有三个版本,目前正在使用的AJP协议的版本是AJP13,它基于二进制的格式在Web服务器和Tomcat之间传输数据,而此前的版本AJP10和AJP11则使用文本格式传输数据。(二进制格式会比http协议的文本模式高效的多)
   HTTP协议:
         使用HTTP或HTTPS协议在Web服务器和Tomcat之间建立通信,此时,Tomcat就是一个完全功能的HTTP服务器,它需要监听在某端口上以接收来自于前端服务器的请求。
   APR (Apache的可移植运行时环境)是提供了通用和标准API的操作系统之上一个通讯层的本地库的集合,它能够为使用了APR的应用程序在与Apache通信时提供较好伸缩能力时带去平衡效用。
  三、 Apache 的两种代理模块(apache默认只自带了mod_proxy模块)
   mod_jk      专用于apache基于AJP协议与后端的tomcat交互通信的模块,需要额外单独编译安装                  
    mod_proxy      包含了以下三种模块( apache 2.2以后版本才有的一个模块,可以实现让apache将前端的用户请求代理至后端服务器)
          mod_proxy_http             与tomcat的http连接器通信
          mod_proxy_ajp            与tomcat的AJP连接器通信
          mod_proxy_balancer         负载均衡模块, 在代理的时候可以实现负载均衡

  
四、 整合Apache 与 Tomcat
  1.首先在另外一台机器 node1 上安装Apache
  需要用到httpd-2.4.2.tar.bz2的源码包
  在编译安装Apache之前还应该安装两个包:
  apr-1.4.6.tar.bz2             apache的可移植运行时环境
apr-util-1.4.1.tar.bz2      是apr的一个抽象库,可以让程序更好的利用apr的功能
pcre-devel
  (1). 先安装apr-1.4.6.tar.bz2


[*]# tar xf apr-1.4.6.tar.bz2
[*]# cd apr-1.4.6
[*]# ./configure --prefix=/usr/local/apr
[*]# make && make install


  (2). 安装apr-util-1.4.1


[*]# tar xf apr-util-1.4.1.tar.bz2
[*]# cd apr-util-1.4.1
[*]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[*]# make && make install

  
(3). 安装pcre-devel
       # yum -y install pcre-devel
  (4). 编译安装apache


[*]# tar xf httpd-2.4.2.tar.bz2
[*]# cd httpd-2.4.2
[*]#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util--enable-proxy --enable-proxy-http --enable-proxy-ajp
[*]# make && make install

  1.2、修改httpd的主配置文件,设置其Pid文件的路径
编辑/etc/httpd/httpd.conf,添加如下行即可:
       PidFile"/var/run/httpd.pid"
   LoadModule slotmem_shm_module modules/mod_slotmem_shm.so   启用这个模块
1.3、提供SysV服务脚本/etc/rc.d/init.d/httpd 内容如下:


[*]#!/bin/bash
[*]#
[*]# httpd      Startup script for the Apache HTTP Server
[*]#
[*]# chkconfig: - 85 15
[*]# description: Apache is a World Wide Web server.It is used to serve \
[*]#          HTML files and CGI.
[*]# processname: httpd
[*]# config: /etc/httpd/conf/httpd.conf
[*]# config: /etc/sysconfig/httpd
[*]# pidfile: /var/run/httpd.pid
[*]
[*]# Source function library.
[*]. /etc/rc.d/init.d/functions
[*]
[*]if [ -f /etc/sysconfig/httpd ]; then
[*]      . /etc/sysconfig/httpd
[*]fi
[*]
[*]# Start httpd in the C locale by default.
[*]HTTPD_LANG=${HTTPD_LANG-"C"}
[*]
[*]# This will prevent initlog from swallowing up a pass-phrase prompt if
[*]# mod_ssl needs a pass-phrase from the user.
[*]INITLOG_ARGS=""
[*]
[*]# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
[*]# with the thread-based "worker" MPM; BE WARNED that some modules may not
[*]# work correctly with a thread-based MPM; notably PHP will refuse to start.
[*]
[*]# Path to the apachectl script, server binary, and short-form for messages.
[*]apachectl=/usr/local/apache/bin/apachectl
[*]httpd=${HTTPD-/usr/local/apache/bin/httpd}
[*]prog=httpd
[*]pidfile=${PIDFILE-/var/run/httpd.pid}
[*]lockfile=${LOCKFILE-/var/lock/subsys/httpd}
[*]RETVAL=0
[*]
[*]start() {
[*]      echo -n $"Starting $prog: "
[*]      LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
[*]      RETVAL=$?
[*]      echo
[*]      [ $RETVAL = 0 ] && touch ${lockfile}
[*]      return $RETVAL
[*]}
[*]
[*]stop() {
[*]    echo -n $"Stopping $prog: "
[*]    killproc -p ${pidfile} -d 10 $httpd
[*]    RETVAL=$?
[*]    echo
[*]    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
[*]}
[*]reload() {
[*]    echo -n $"Reloading $prog: "
[*]    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
[*]      RETVAL=$?
[*]      echo $"not reloading due to configuration syntax error"
[*]      failure $"not reloading $httpd due to configuration syntax error"
[*]    else
[*]      killproc -p ${pidfile} $httpd -HUP
[*]      RETVAL=$?
[*]    fi
[*]    echo
[*]}
[*]
[*]# See how we were called.
[*]case "$1" in
[*]start)
[*]    start
[*]    ;;
[*]stop)
[*]    stop
[*]    ;;
[*]status)
[*]      status -p ${pidfile} $httpd
[*]    RETVAL=$?
[*]    ;;
[*]restart)
[*]    stop
[*]    start
[*]    ;;
[*]condrestart)
[*]    if [ -f ${pidfile} ] ; then
[*]      stop
[*]      start
[*]    fi
[*]    ;;
[*]reload)
[*]      reload
[*]    ;;
[*]graceful|help|configtest|fullstatus)
[*]    $apachectl $@
[*]    RETVAL=$?
[*]    ;;
[*]*)
[*]    echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
[*]    exit 1
[*]esac
[*]
[*]exit $RETVAL

  
而后为此脚本赋予执行权限:
      # chmod +x /etc/rc.d/init.d/httpd
1.4、加入服务列表:
      # chkconfig --add httpd
1.5、加入PATH环境变量
      # vi /etc/profile
            PATH=$PATH:/usr/local/apache/bin/
      # export PATH=$PATH:/usr/local/apache/bin/
1.6、 查看支持的模块


[*]# httpd -l            显示加载的静态模块
[*]Compiled in modules:
[*]core.c
[*]mod_so.c
[*]http_core.c
[*]event.c



[*]# httpd -D DUMP_MODULES         显示加载的所有模块
[*]Loaded Modules:                                    要有proxy模块
[*]
[*] proxy_module (shared)
[*]
[*] proxy_http_module (shared)
[*] proxy_ajp_module (shared)
[*] proxy_balancer_module (shared)
[*] proxy_express_module (shared)

  1.7、启动测试apache


[*]# /etc/init.d/httpd start
[*]Starting httpd:                                          



  2 . 配置Apache 通过 mod_proxy 模块与 Tomcat连接
  mod_proxy模块又支持ajp连接器和http连接器与Tomcat通信
  2.1使用 http 连接器
  1> 首先编辑Apache 的主配置文件/etc/httpd/httpd.conf
    启用    Include /etc/httpd/extra/httpd-vhosts.conf
    注释中心主机: # DocumentRoot "/usr/local/apache/htdocs"
  2> 配置虚拟主机


[*]# vi extra/httpd-vhosts.conf
[*]
[*]
[*]ProxyVia            Off         关闭正向代理
ProxyRequests       Off
ProxyPreserveHost   Off         off 表示任何对 "tomcat.yue.com" 的请求都定位到后端 Tomcat 的默认虚拟主机上
[*]
[*]
[*]      ServerName      tomcat.yue.com
[*]#       DocumentRoot    "/web/tomcat"
[*]      
[*]      ProxyPass         / http://192.168.1.12:8080/   把前端用户请求的URL路径,以哪种协议转到后端的哪个tomcat服务器上的哪个端口上
[*]      ProxyPa***everse    / http://192.168.1.12:8080/
[*]   
[*]

  3> 修改 Windows 主机的hosts文件
  C:\Windows/System32\drivers\etc\hosts文件中添加      “192.168.1.10    tomcat.yue.com”
  4> 测试一下效果

  5>让在apache中定义的虚拟主机定位到与后端 Tomcat中同名的虚拟主机上
  修改 extra/httpd-vhosts.conf


[*]# vi extra/httpd-vhosts.conf
[*]
[*]
[*]ProxyVia            Off
ProxyRequests       Off
ProxyPreserveHost   On          定位到后端Tomcat中与当前主机名相同的虚拟主机上
[*]
[*]
[*]      ServerName      tomcat.yue.com
[*]#       DocumentRoot    "/web/tomcat"
[*]      
[*]      ProxyPass         / http://192.168.1.12:8080/
[*]      ProxyPa***everse    / http://192.168.1.12:8080/
[*]
[*]
[*]# /etc/init.d/httpd restart
[*]Stopping httpd:                                          [ OK]
[*]Starting httpd:                                          [ OK]

  测试一下效果

  此时我们也可以打开Tomcat的管理器,查看Tomcat的状态信息:

  2.2使用 ajp 连接器
  修改extra/httpd-vhosts.conf


[*]# vi extra/httpd-vhosts.conf
[*]
[*]ProxyVia            Off
[*]ProxyRequests       Off
[*]ProxyPreserveHost   Off
[*]
[*]
[*]      ServerName      tomcat.yue.com
[*]#       DocumentRoot    "/web/tomcat"
[*]      ProxyPass         / ajp://192.168.1.12:8009/   注意: ajp连接器使用的是8009端口
[*]      ProxyPa***everse    / ajp://192.168.1.12:8009/
[*]

   重启Apache:


[*]# /etc/init.d/httpd restart
[*]Stopping httpd:                                          [ OK]
[*]Starting httpd:                                          

  测试一下:

  查看此时连接的状态信息:

  3. 配置apache通过mod_jk模块与Tomcat连接
  mod_jk是ASF的一个项目,是一个工作于apache端基于AJP协议与Tomcat通信的连接器,它是apache的一个模块,是AJP协议的客户端(服务端是Tomcat的AJP连接器)
  3.1 由于mod-jk 模块apache并没有自带,所以需要手动编译安装


[*]    # tar xf tomcat-connectors-1.2.37-src.tar.gz
[*]    # cd tomcat-connectors-1.2.37-src/native/
[*]    # ./configure --with-apxs=/usr/local/apache/bin/apxs    注意此时所在的路径
[*]
[*]
[*]    checking build system type... i686-pc-linux-gnu
[*]    checking host system type... i686-pc-linux-gnu
[*]    checking target system type... i686-pc-linux-gnu
[*]    checking for a BSD-compatible install... /usr/bin/install -c
[*]    checking whether build environment is sane... yes
[*]    checking for gawk... gawk
[*]    checking whether make sets $(MAKE)... yes
[*]    checking for test... /usr/bin/test
[*]    checking for grep... /bin/grep
[*]    checking for echo... /bin/echo
[*]    checking for sed... /bin/sed
[*]    checking for cp... /bin/cp
[*]    checking for mkdir... /bin/mkdir
[*]    need to check for Perl first, apxs depends on it...
[*]    checking for perl... /usr/bin/perl
[*]    APRINCLUDEDIR is-I/usr/local/apr/include/apr-1 -I/usr/local/apr-util/include/apr-1
[*]    building connector for "apache-2.0"
[*]
[*]......   ......
[*]    configure: creating ./config.status
[*]    config.status: creating Makefile
[*]    config.status: creating apache-1.3/Makefile
[*]    config.status: creating apache-1.3/Makefile.apxs
[*]    config.status: creating apache-2.0/Makefile
[*]    config.status: creating apache-2.0/Makefile.apxs
[*]    config.status: creating common/Makefile
[*]    config.status: creating common/list.mk
[*]    config.status: creating common/jk_types.h
[*]    config.status: creating common/config.h
[*]    config.status: executing depfiles commands
[*]      
[*]    # make && make install

  3.2apache要使用mod_jk连接器,需要在启动时加载此连接器模块。为了便于管理与mod_jk模块相关的配置,这里使用一个专门的配置文件/etc/httpd/extra/httpd-jk.conf来保存相关指令及相关设ersFile指定的,在apache启动时,mod_jk会扫描此文件获取每一个worker的配置信息。


[*]# vi extra/httpd-jk.conf    内容如下:
[*]
[*]# Load the mod_jk
[*]LoadModulejk_modulemodules/mod_jk.so
[*]JkWorkersFile/etc/httpd/extra/workers.properties          每个tomcat的engine(或实例)就叫一个work,使用一个专用的配置文件记录所有的work
[*]JkLogFilelogs/mod_jk.log          指定mod_jk模块的日志文件
[*]JkLogLeveldebug                   用于指定日志的级别(info, error, debug),第一次使用时可以打开,便于查找错误
[*]JkMount/*Tomcat_A               * 表示根下的所有内容都转发, 后端的work名称,    work的名字在tomcat的配置文件的engine中定义--> "jvmRoute=“work_Name”"
[*]JkMount/status/stat1            所有的状态定义到stat1 上 ,我们可以在路径后加上stat1来查看相关的状态信息

  3.3 对于apache代理来说,每一个后端的Tomcat实例中的engine都可以视作一个worker,而每一个worker的地址、连接器的端口等信息都需要在apache端指定以便apache可以识别并使用这些worker。
具体路径则是使用前面介绍过的JkWorkersFile指定的,在apache启动时,mod_jk会扫描此文件获取每一个worker的配置信息。我们这里使用/etc/httpd/extra/workers.properties


[*]# vi extra/workers.properties
[*]
[*]worker.list=Tomcat_A,stat1      worker列表
[*]worker.Tomcat_A.port=8009
[*]worker.Tomcat_A.host=192.168.1.12
[*]worker.Tomcat_A.type=ajp13
[*]worker.Tomcat_A.lbfactor=1
[*]worker.stat1.type = status   定义了stat1 的类型

  3.4 编辑Apache的主配置文件


[*] # vi /etc/httpd/httpd.conf
[*]
[*] 注释掉这行:# Include /etc/httpd/extra/httpd-vhosts.conf
[*]   加入:Include /etc/httpd/extra/httpd-jk.conf
[*]
[*]
[*]重启Apache
[*]# /etc/init.d/httpd restart
Stopping httpd:                                          [OK]
Starting httpd:                                          [OK]

  3.5 编辑Tomcat的主配置文件


[*]# vi conf/server.xml
[*]         在Engine 中添加关于“Tomcat_A”的设置
[*]      
[*]# bin/catalina.sh stop
[*]Using CATALINA_BASE:   /usr/local/tomcat
[*]Using CATALINA_HOME:   /usr/local/tomcat
[*]Using CATALINA_TMPDIR: /usr/local/tomcat/temp
[*]Using JRE_HOME:      /usr/java/jdk1.7.0_05
[*]Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[*]
[*]
[*]# bin/catalina.sh start
[*]Using CATALINA_BASE:   /usr/local/tomcat
[*]Using CATALINA_HOME:   /usr/local/tomcat
[*]Using CATALINA_TMPDIR: /usr/local/tomcat/temp
[*]Using JRE_HOME:      /usr/java/jdk1.7.0_05
[*]Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

  3.6. 在Windows主机的hosts文件中添加 “mail.yue.com   192.168.1.10” 可以使用测试使用
  测试一下:



  通过添加 status 路径来查看状态信息


  


  Over !



页: [1]
查看完整版本: 整合 Apache 与 Tomcat