CentOS下httpd与tomcat整合

有时候需要在Web入口地址中隐藏非80端口,而最常见的方案是整合httpd和tomcat ,下面是通过mod_jk实现httpd和tomcat整合的详细流程,希望对大家有所帮助。

OS: CentOS5.7 X64

 主要步骤:

1.   安装JDK

 略

2.   安装httpd

yum install httpd
/etc/init.d/httpd start

http://localhost 看到欢迎界面

3. 安装 tomcat (十分简单)

  3.1官网下载apache-tomcat-7.0.42.tar.gz ,解压缩

  tar –xzvf apache-tomcat-7.0.42.tar.gz
  mv apache-tomcat-7.0.42/opt/tomcat7

  3.2vim ~/bash_profile 添加:

export TOMCAT_HOME=/opt/tomcat7
  3.3开启tomcat服务

$TOMCAT_HOME/bin/startup.sh

 3.4http://localhost:8080 看到欢迎界面

4.  采用mod_jk方式整合httpd 和tomcat

4.1安装httpd-devel 

安装成功后,会有/usr/sbin/apxs服务,mod_jk的编译依赖于apxs

yum install httpd-devel

4.2 安装mod_jk

apache网下载tomcat-connectors-1.2.37-src.tar.gz

http://tomcat.apache.org/download-connectors.cgi

安装tomcat-connectors (mod_jk)

tar -xzvftomcat-connectors-1.2.37-src.tar.gz
mv tomcat-connectors-1.2.37-src  /opt/mod_jk
cd /opt/mod_jk/native
./configure--with-apxs=/usr/sbin/apxs --with-java-home=/opt/java
Make
Make install
复制mod_jk.so

cp/opt/mod_jk/native/apache-2.0/mod_jk.so /usr/lib64/httpd/modules/

4.3修改httpd的配置

4.3.1 创建workers.properties

vim  /etc/httpd/conf/workers.properties 添加以下内容:

# Defining a workernamed worker1 and of type ajp13
worker.list=worker1
# Set properties forworker1
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

4.3.2 创建 mod_jk.conf

vim  /etc/httpd/conf/mod_jk.conf 添加以下内容:

JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk logs
JkLogFile/etc/httpd/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 tosend SSL KEY SIZE,
JkOptions  +ForwardKeySize +ForwardURICompat-ForwardDirectories
# JkRequestLogFormat setthe request format
JkRequestLogFormat"%w %V %T"
# 将所有servlet 和jsp请求通过ajp13的协议送给Tomcat,让Tomcat来处理
JkMount /servlet/*  worker1
JkMount /*.jsp worker1
4.3.3 修改httpd.conf

vim httpd.conf 修改以下内容

ServerName修改为:ServerName 10.11.18.45:80
DirectoryIndex 行添加 index.jsp index.htm

添加以下内容,其中”/var/wwwroot”是你要发布的Web项目所在的父目录

DocumentRoot"/var/wwwroot"

    Options Includes FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
    XBitHack on


    Order deny,allow
    Deny from all

以下内容:添加

LoadModule jk_module/usr/lib64/httpd/modules/mod_jk.so
Include/etc/httpd/conf/mod_jk.conf
4.3.4 修改tomcat的配置

vim $TOMCAT_HOME/conf/server.xml

部分 修改为

4.3.5  restart httpd 和 tomcat

/etc/init.d/httpd restart
$TOMCAT_HOME/bin/shutdown.sh
$TOMCAT_HOME/bin/startup.sh
4.3.6 测试

在/var/wwwroot 目录下新建目录myproject,myproject下新建index.jsp

<%@ page language="java" import="java.util.*"pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    ">
    My JSP 'index.jsp' startingpage
        
        
           
        
        
        
  
  
    Hello, I am mango_song ,this is my JSPpage. 

http://localhost/myproject 看到相应界面,至此httpd tomcat已经整合成功

你可能感兴趣的:(linux,Web开发)