apache2.4 tomcat8整合

来源:互联网 发布:淘宝卖家复制宝贝 编辑:程序博客网 时间:2024/06/05 20:04
大纲:
1、安装apache2.4.x:
apache2.4版本需要手动安装apr、apr-util以及prce,可以参考如下网址。

#apache 配置参考使用命令(可在后面加鉴权):
./configure --prefix=/data/apache2(你安装的路径) --enable-so --enable-rewrite=shared --enable-speling --enable-mods-shared=most --with-mpm=worker

http://www.cnblogs.com/kerrycode/p/3261101.html
配置apache中的extra/httpd-vhosts.conf文件来指定虚拟主机

# Virtual Hosts
#
# Required modules: mod_log_config

# 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/2.4/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.

#
# 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.
#
<VirtualHost *:8080>
    #DocumentRoot "/usr/angelotong/apache-tomcat-8.5.12/webappd/"
    #DocumentRoot "/usr/angelotong/apache-tomcat-8.5.12"
    DocumentRoot "/data/apache2/htdocs"
    JkMountCopy On
    ServerName localhost
    ErrorLog "logs/cdnconf.oa.com-error_log"
    CustomLog "logs/cdnconf.oa.com-access_log" common
   # <Directory "/usr/angelotong/apache-tomcat-8.5.12/webappd/">
    #<Directory "/usr/angelotong/apache-tomcat-8.5.12">
    <Directory "/data/apache2/htdocs">
        Options FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

此时可以先测试apache是否安装正确,然后在进行下一步

2、安装tomcat8:
包括安装jdk、配置环境变量,安装tomcat,配置权限
http://blog.csdn.net/tongzidane/article/details/44035087
3、使用tomcat-connectors(mod_jk)连接apache和tomcat。
mod_jk下载地址为:http://tomcat.apache.org/download-connectors.cgi
不同版本apache对应不同的mod_jk,我选择了tomcat-connectors-1.2.41-src.tar.gz,亲测可用。
下载后,解压,进入./native文件夹依次输入./configure --with-apxs=[apache安装目录]/bin/apxs和make完成编译(不需要install),在/native/apache-2.0文件夹下可以找到mod_jk.so。将这个文件复制到apache安装路径下的modules文件夹下。
4、创建相关配置文件。
创建配置文件可以有很多种方式,主要目的是为了让apache把接收到jsp或servlet相关的应该由tomcat处理的请求交给tomcat处理。接下来我介绍其中一种配置方式,进入apache安装路径下的conf文件夹(位置可以自己指定),创建两个文件,mod_jk.conf以及workers.properties。文件对应内容如下:

mod_jk.conf:
LoadModule jk_module modules/mod_jk.so

#JkWorkersFile 路径根据实际情况填写
JkWorkersFile /data/apache2/conf/workers.properties
# Where to put jk logs
JkLogFile /data/apache2/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 +ForwardURICompatUnparsed -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

#根据实际情况自己定义即可,就是将什么样的请求转发给worker1处理
JkMount /* worker1
JkMount /*.jsp worker1
JkMount /servlet/* worker1
JkMount /*.do worker1
JkMount /*.action worker1
#JkMount /*.class worker1
#JkMount /*.jar worker1


workers.properties
#路径根据实际情况填写
workers.tomcat_home=/usr/angelotong/apache-tomcat-8.5.12/  
workers.java_home=/usr/angelotong/java/jdk1.7.0_79/jre
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

5、修改apache和tomcat配置文件
apache2/httpd.conf中添加:
Include /etc/httpd/conf/mod_jk.conf
包含刚才的mod_jk配置文件,然后找到DirectoryIndex index.html在后面添加 index.jsp:
DirectoryIndex index.html index.jsp

tomcat/conf/server.xml中在<Host></Host>标签之间加入
<Context path="" docBase="你的路径" debug="0" reloadable="true" crossContext="true"/>
docBase路径和apache vhost中的document路径一致即可。
讲现有的connector注释掉,我们只保留一个connector,即端口为8009(上面workers.properties 中的端口)的ajp-connector。

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

6、需要注意的地方
6.1、看教程一定要注意版本

6.2、apache2.2和apache2.4配置方面有一定区别,不要将2.2的参数放到2.4中。
        比如2.2中allow from all 到2.4变成 require all granted。

6.3、如果apache不能成功转发到tomcat,检查文件:apache/conf/httpd.conf、apache/conf/workers.properties、apache/conf/mod_jk.conf、tomcat/conf/server.xml 中的每个参数是否正确,我就是vhost中没有加JkMountCopy On导致转发不成功。

笔者将正确的war包放到apache指定的网站根目录下,可以成功将apache收到的请求转发由tomcat处理。
0 0
原创粉丝点击