码迷,mamicode.com
首页 > Windows程序 > 详细

Win7系统64位环境下使用Apache——Apache2.4整合Tomcat与mod_jk

时间:2017-04-23 10:54:46      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:nec   博客   web服务器   machine   include   文件夹   his   常见问题   trunk   

转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/70398091
本文出自【我是干勾鱼的博客

之前的几篇文章:

Win7系统64位环境下使用Apache——Apache2.2下载

Win7系统64位环境下使用Apache——Apache2.2安装及常见问题解决

Win7系统64位环境下使用Apache——Apache2.4版本安装及卸载

Win7系统64位环境下使用Apache——下载mod_jk

这几篇文章分别讲解了Apache以及mod_jk的下载,今天就来说说具体的应用了,Apache2.4整合Tomcat与mod_jk。

在介绍之前,还是有必要再说一下Apache、tomcat、mod_jk之间的关系。

1 Apache、tomcat、mod_jk之间的关系

先讲一下Apache、tomcat、mod_jk这三者之间的关系。

Apache:这里指的是httpd,Web服务器,用来处理静态文件。

tomcat:应用服务器,是servlet、jsp(jsp在后面其实也是被解释成servlet)的运行容器,可以处理servlet、jsp这些动态文件,当然也可以处理静态文件,但是性能不如Apache、nginx这些web服务器性能好。虽然一般还是单独使用web服务器来处理静态文件,应用服务器处理动态文件。

mod_jk:是tomcat提供的、用来连接Apache与tomcat的一个插件,默认下载的tomcat中是没有的,需要单独下载。

2 预期效果

来说一下我们想达到的效果。现在我们有一个Apache,两个tomcat,为了说的典型一点,我们假设这三个系统都部署在一台硬件服务器上,做实验嘛。当然如果换成独立服务器时原理是一样的,只需要修改ip的配置。

通过域名:

one.test.com

可以经过Apache访问第1台tomcat:

apache-tomcat-6.0.51-1

而通过域名:

two.test.com

可以经过Apache访问第2台tomcat:

apache-tomcat-6.0.51-1

3 修改Hosts

修改Hosts的目的是使得域名

one.test.com

two.test.com

访问本地ip。

Hosts文件位于:

C:\Windows\System32\drivers\etc

目录下。在里面插入内容:

127.0.0.1 one.test.com two.test.com

就可以了。

之后如果输入:

http://one.test.com

或者:

http://two.test.com

就都访问本地的

127.0.0.1

的80端口了,而这正指向了本地的Apache2.4。

4 整合

4.1 配置tomcat

Apache是对外的,它占用的端口号肯定是80了,默认也是。

而对于tomcat,我们这里先对第一个tomcat进行配置,假设将其命名为apache-tomcat-6.0.51-1(当然第二个就命名为apache-tomcat-6.0.51-2)。打开apache-tomcat-6.0.51-1的server.xml文件,即:

${apache-tomcat-6.0.51-1}/conf/server.xml

在里面配置端口和工程的位置,这里所说的工程就是你所要部署在tomcat中的项目。内容如下:

<?xml version=‘1.0‘ encoding=‘utf-8‘?>

<Server port="9015" 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.ServerLifecycleListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

    <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="9110" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />

    <Connector port="9019" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">

    <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"
            xmlValidation="false" xmlNamespaceAware="false">

        <Context
            path=""
            docBase="E:\Project1\WebRoot"
            reloadable="true" >
        </Context>

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

为了规范并内容,这里我把所有的注释都删掉了,但是你在apache-tomcat-6.0.51的环境下是可以直接拿来用的。所有内容如图所示:

技术分享

另一个tomcat有前面一个的配置是类似的不同的是把端口号:

9015
9110
9019

分别改为了:

8015
8110
8019

工程目录从:

E:\Project1\WebRoot

改为了:

E:\Project2\WebRoot

4.2 配置Apache与mod_jk

首先,假设Apache2.4的安装目录为:

${Apache2.4}

那么进入${Apache2.4},确保里面有httpd.conf文件,如图:

技术分享

打开httpd.conf这个文件,需要做2件事:

4.2.1 httpd-vhosts.conf

确保以下脚本:

Include conf/extra/httpd-vhosts.conf

没有被注释掉,如图:

技术分享

因为系统会用到

${Apache2.4}/conf/extra/httpd-vhosts.conf

文件的内容。

这里多说一句,有的人习惯于在:

${Apache2.4}/conf

目录下重新建立一个名为:

vhosts.conf

的文件,然后再在

${Apache2.4}/conf/httpd.conf

文件的最后加入以下内容:

Include conf/vhosts.conf

这其实是没必要的。直接使用:

${Apache2.4}/conf/extra/httpd-vhosts.conf

就可以了,效果都是一样的。

4.2.2 引用mod_jk.conf

这个时候要用到我们之前在文章Win7系统64位环境下使用Apache——下载mod_jk中提到的mod_jk了,需要进行对mod_jk的配置。

首先在文件:

${Apache2.4}/conf/httpd.conf

的最后加入脚本:

Include conf/mod_jk.conf

如图:

技术分享

4.2.3 添加mod_jk.conf内容

然后在:

${Apache2.4}/conf

目录下创建文件:

mod_jk.conf

文件内容如下:

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
# Where to put jk logs
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"

如图所示:

技术分享

这里有两个地方比较重要,第一个是:

LoadModule jk_module modules/mod_jk.so

这是指明mod_jk.so文件的引用位置。

第二个是:

JkWorkersFile conf/workers.properties

这是在指明workers.properties文件的引用位置。workers.properties文件的作用后面会说。

4.2.4 添加workers.conf

4.2.4.1 workers.conf的作用

先说说workers.conf是干什么的。其实这个问题我在文章Win7系统64位环境下使用Apache——下载mod_jk中已经提到过了,在官网链接中曾经提到过workers.conf的作用,如图:

技术分享

它的作用,简单来说,就是有的时候我们只有一个Apache,但是有多个tomcat。我们需要在这个Apache中将不同的ip或不同的域名指向到不同的tomcat服务器中,这时我们就需要使用workers.properties了。这个链接里有对workers.properties的详细介绍。

4.2.4.2 创建workers.conf文件

在目录:

${Apache2.4}/conf

下创建文件:

workers.conf

如图:

技术分享

4.2.4.3 配置workers.conf

好了,tomcat配好了,就可以在workers.conf中配置。我们想要达到的目的是,通过域名:

one.test.com

借助Apache,访问到:

apache-tomcat-6.0.51-1

通过域名:

two.test.com

借助Apache,访问到:

apache-tomcat-6.0.51-2

当然这里要在本地配置虚拟主机,使得可以通过域名直接访问本地ip,这个后面再说。

目前我们要在文件:

${Apache2.4}/conf/workers.conf

中加入以下内容:

worker.list=one,two
# Set sso
worker.one.type=ajp13
worker.one.host=127.0.0.1
worker.one.port=9019
worker.one.lbfactor=50
worker.one.cachesize=80
worker.sso.cache_timeout=600
worker.one.socket_keepalive=1
worker.sso.recycle_timeout=300
#worker.one.max_packet_size=65536

# Set portal
worker.two.type=ajp13
worker.two.host=127.0.0.1
worker.two.port=8019
worker.two.lbfactor=50
worker.two.cachesize=80
worker.portal.cache_timeout=600
worker.two.socket_keepalive=1
worker.portal.recycle_timeout=300

能够看到:

worker.one.type

指向的就是在:

http://tomcat.apache.org/connectors-doc/

中提到的ajp13协议。

而:

worker.one.host

指向的是本地ip。

接着:

worker.one.port

指向的是tomcat的server.xml中第二个Connector中的端口号。其它参数的意义可以到官网去找详细的解释,这里就不赘言了。

4.3 配置Apache的虚拟主机

好了,到目前为止,我们可以说是万事俱备,只欠东风。Apache2.4、Tomcat、mod_jk该配置的我们都配置了,目前要做的就是让mod_jk功能生效,Apache2.4就可以与Tomcat整合在一起并且可以通过域名访问了。

配置虚拟主机,从效果上来说,就是直接通过域名访问本地服务,之前提到了文件:

${Apache2.4}/conf/extra/httpd-vhosts.conf

现在httpd-vhosts.conf该配上用场了。

在httpd-vhosts.conf文件中加入内容:

<VirtualHost *:80>
    ServerName one.test.com
    JkMount /* one
    ErrorLog "logs/one-error_log"
    CustomLog "logs/one-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName two.test.com
    JkMount /* two
    ErrorLog "logs/two-error_log"
    CustomLog "logs/two-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "D:\apache-tomcat-6.0.51-file"
    ServerName   file.test.com
    ErrorLog logs/file-error_log
    CustomLog logs/file-access_log combined
    <Directory "D:\apache-tomcat-6.0.51-file">
        Require all granted
    </Directory>
</VirtualHost>

其完整内容如下:

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn‘t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/trunk/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option ‘-S‘ to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

# NOTE 
# First Virtual Host MUST be a shallow duplicate of the main host 
# in httpd.conf. It will become the default host. Any request coming
# in that does not match any other VirtualHost‘s ServerName will be
# served by this host.

<VirtualHost _default_:80>
DocumentRoot "${SRVROOT}/htdocs"
#ServerName www.example.com:80
</VirtualHost>

# Add any other Virtual Hosts below

########### - start - ###########

<VirtualHost *:80>
    ServerName one.test.com
    JkMount /* one
    ErrorLog "logs/one-error_log"
    CustomLog "logs/one-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName two.test.com
    JkMount /* two
    ErrorLog "logs/two-error_log"
    CustomLog "logs/two-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "D:\apache-tomcat-6.0.51-file"
    ServerName   file.test.com
    ErrorLog logs/file-error_log
    CustomLog logs/file-access_log combined
    <Directory "D:\apache-tomcat-6.0.51-file">
        Require all granted
    </Directory>
</VirtualHost>

########### - end - ###########

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot "${SRVROOT}/docs/dummy-host.example.com"
#    ServerName dummy-host.example.com
#    ServerAlias www.dummy-host.example.com
#    ErrorLog "logs/dummy-host.example.com-error.log"
#    CustomLog "logs/dummy-host.example.com-access.log" common
#</VirtualHost>

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host2.example.com
#    DocumentRoot "${SRVROOT}/docs/dummy-host2.example.com"
#    ServerName dummy-host2.example.com
#    ErrorLog "logs/dummy-host2.example.com-error.log"
#    CustomLog "logs/dummy-host2.example.com-access.log" common
#</VirtualHost>

删除没用的注释,如下图所示:

技术分享

看到没,这里面的:

JkMount /* one

就是使mod_jk生效的脚本。本来域名:

one.test.com

跳转到应该是:

127.0.0.1

现在加上:

JkMount /* one

那么mod_jk就会引用:

workers.conf

发现应该转向到ip127.0.0.1的9010端口去,这样本地

apache-tomcat-6.0.51-1

上的工程就启动起来了。

而有的时候我们可能想在服务器上建个文件夹用来保存本地一些静态文件,如图中所示3那里就是指明路径的,其中3、4、5指明了本地文件夹的路径,而6是用来设置对目录的访问权限。

需要注意的是Apache2.4的设置与Apache2.2的设置在这里是有却别的。对于Apache2.4来说,我们看到这里这支撑了:

Require all granted

而Apache2.2时是设置成了:

Order allow,deny
Allow from all

如果对Apache2.4在这里进行了错误的设置是会出问题的,会报错:

Invalid command ‘Order‘, perhaps misspelled or defined by a module not included
in the server configuration

如图所示:

技术分享

所以输入方式一定要注意。

这里参考了:

http://systembash.com/apache-2-4-upgrade-and-the-invalid-command-order-error/

http://httpd.apache.org/docs/2.4/upgrading.html

5 完成

ok,这样我们让Apache2.4整合Tomcat与mod_jk的工作就完成了。

Win7系统64位环境下使用Apache——Apache2.4整合Tomcat与mod_jk

标签:nec   博客   web服务器   machine   include   文件夹   his   常见问题   trunk   

原文地址:http://blog.csdn.net/dongdong9223/article/details/70398091

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!