Web 服务器 之 安装 Apache 2 和 Tomcat 5.5

作者: etony
来自: Linuxsir.org
提要:
本文档主要讲述在 Debian/testing 平台上如何简单的将Tomcat5.5和Apache2整合到一起.

安装 Apache 2 和 Tomcat 5.5

etony C.F.AN [email protected]

v0.0.1, 2006-6-1
本文档主要讲述在 Debian/testing 平台上如何简单的将Tomcat5.5和Apache2整合到一起.

1. 声明

2. 配置 JAVA 环境

操作步骤, 请参阅: 安装Java环境

3. 配置 tomcat5.5

tomcat4 太古老的一个版本了, 这里我们选择 tomcat5.5 作为配置环境.

当前 tomcat5.5 版本为 5.5.17. 由于 tomcat5.5 的更新速度很快, 我选择从 tomcat 的官方网站下载.

$ wget http://apache.justdn.org/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.5.17.tar.gz

将其解压到工作目录, 比如

# tar zxvf apache-tomcat-5.5.17.tar.gz -C /opt/ 

配置环境变量

# export CATALINA_HOME=tomcat_install_path //比如这里应该是 /opt/apache-tomcat-5.5.17/

你可以将其加入启动脚本中,比如 .bashrc. 然后, 运行 tomcat, 检查配置情况:

# tomcat_intall_path/bin/startup.sh  //  这里是/opt/tomcat5.5//bin/startup.sh

如果抛出错误, 根据错误检查自己的配置.

4. 获取 JK 模块

可以通过 apt-get 获取 JK 模块或直接从官方网站获取编译好的二进制模块, 但是编译JK一点也不麻烦, 这里我选择编译生成 JK 模块, 这有背 Debian 的方法 ;)

  • 下载JK模块的源代码

    JK的当前版本是 1.2.15, 运行下边的命令获取源代码:

    $ wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/source/jk-1.2.15/jakarta-tomcat-connectors-1.2.15-src.tar.gz
    

    解压到临时目录:

    # tar zxvf jakarta-tomcat-connectors-1.2.15-src.tar.gz -C /tmp/
    

  • 配置编译环境

    安装编译 JK 所需要的软件包:

    #aptitude update
    #aptitude install ibtool autoconf gcc apache2-prefork-dev  g++ 
    

  • 编译 JK 模块

    JK的解压目录下运行:

    #cd /tmp/jakarta-tomcat-connectors-1.2.15-src/jk/native
    
    #./configure --with-apxs=/usr/bin/apxs2 
    
    or
    
    #./configure --with-apache=/etc/apache2
    
    #make
    

    详细的编译说明, 请参阅: Jakarta Tomcat Connector: Installation for Apache 2.x 将生成的 JK 模块 JK 复制到 /usr/lib/apache2/modules/

    #cp ./apache-2.0/mod_jk.so /usr/lib/apache2/modules/
    

5. 创建配置文件

  • /etc/apache2/conf.d/mod_jk.conf

    # 指出mod_jk模块工作所需要的工作文件workers.properties的位置
    JkWorkersFile /etc/apache2/workers.properties
    
    # Where to put jk logs
    JkLogFile /var/log/apache2/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"
    
    # 将所有servlet 和jsp请求通过ajp13的协议送给Tomcat,让Tomcat来处理
    JkMount /servlet/*  worker1
    JkMount /*.jsp worker1
    

  • /etc/apache2/mods-available/jk.load

    LoadModule jk_module modules/mod_jk.so
    Include /etc/apache2/conf.d/mod_jk.conf
    

    并创建符号链接到mods-enabled

  • /etc/apache2/workers.properties

    一个workers.properties例子:

      # Define some properties
      workers.apache_log=/var/log/httpd/
      workers.tomcat_home=/var/tomcat3
      workers.java_home=/opt/IBMJava2-131/
      ps=/
      # Define 4 workers, 3 real workers using ajp12, ajp13, jni, the last one being a loadbalancing worker 
      worker.list=worker1, worker2, worker3, worker4
      # Set properties for worker1 (ajp12)
      worker.worker1.type=ajp12
      worker.worker1.host=locahost
      worker.worker1.port=8007
      worker.worker1.lbfactor=1
      # Set properties for worker2 (ajp13)
      worker.worker2.type=ajp13
      worker.worker2.host=locahost
      worker.worker2.port=8009
      worker.worker2.lbfactor=1
      worker.worker2.cachesize=10
      worker.worker2.cache_timeout=600
      worker.worker2.socket_keepalive=1
      worker.worker2.socket_timeout=60
      # Set properties for worker3 (jni)
      worker.worker3.type=jni
      # Set worker3 bridge type, here Tomcat 3.3
      worker.worker3.bridge=tomcat33
      # Set worker3 classpath
      worker.worker3.class_path=$(workers.tomcat_home)$(ps)classes
      worker.worker3.class_path=$(workers.tomcat_home)$(ps)lib$(ps)tomcat.jar
      # Set worker3 tomcat command line
      worker.worker3.cmd_line=-home
      worker.worker3.cmd_line=$(workers.tomcat_home)
      # Set worker3 Tomcat/JVM settings
      worker.worker3.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)libjvm.so
      worker.worker3.stdout=$(workers.apache_log)$(ps)inprocess.stdout
      worker.worker3.stderr=$(workers.apache_log)$(ps)inprocess.stderr
      worker.worker3.sysprops=tomcat.home=$(workers.tomcat_home)
      # Set properties for worker4 (lb) which use worker1 and worker2
      worker.worker4.balance_workers=worker1,worker2
    

    或直接使用

    # Defining a worker named worker1 and of type ajp13
    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
    

6. 创建虚拟站点

创建虚拟站点配置文件 /etc/apache2/sites-available/

创建符号链接到目录 /etc/apache2/sites-enabled/

最后编辑Tomcat的配置文件, 将其根目录指向所创建站点的根目录。

7. 测试

创建一个简单的jsp文件,比如

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Test Page</title>
</head>
<body>
    <h1>JSP Test Page</h1>
    
    <%= new java.util.Date() %>
</body>
</html>
    
  

置于根目录下,然后通过浏览器访问!

8. 参考文档

你可能感兴趣的:(apache)