转自http://www.oxbbs.net/cgi-bin/forums.cgi?forum=38,原作者rosealet
WIN2K&WIN9X+APACHE+JSP+TOMCAT运行环境的配置总结:
以下的这片文章,决无抄袭,完全是我自己的配置总结!所用到的软件可以到httpd.apache.org、jakarta.apache.org和java.sun.com下载。
如无法找到,可以告诉我,我会给出下载地址!
需要的东西:
1、一台安装有WIN2K系统的电脑,最好是professional的。(因为用server的话,会安装IIS5,可能会占用80端口。我们现在用apache作http服务,当然就没有必要用IIS了。)
2、java的运行环境:J2EE,我们可以使用j2sdk或j2jre,只是用jre就行。(sdk是开发环境包含运行环境,jre只是运行环境)
3、mod_jk或mod_jserv使apache与tomcat进行连接。我推荐用mod_jk,因为mod_jserv只能用于apache,而mod_jk可用于多种http服务软件。windows下mod_jk是一个dll文件。
4、tomcat,最主要支持的JSP运行的软件。
5、apache,目前最好的http服务软件。
开始安装:
1、安装jdk,我用的是jdk_sdk1.3.1.01版,比如安装到c:\jdk1.3下。之后添加jdk的环境变量。
变量名:path
变量值:c:\jdk1.3\bin
变量名:JAVA_HOME
变量值:c:\jdk1.3
变量名:CLASSPATH
变量值:.\;c:\jdk1.3\lib\dt.jar;c:\jdk1.3\lib\tools.jar;
重新启动计算机使配置的环境变量生效
环境变量的添加位置:我的电脑>属性>高级>环境变量(win2k)
如win9x在autoexec.bat中添加
Path=c:\jdk1.3\bin
Set JAVA_HOME=c:\jdk1.3
Set CLASSPATH=.\;c:\jdk1.3\lib\dt.jar;c:\jdk1.3\lib\tools.jar;
2、安装apache 1.3.27 for windows版,比如安装到c:\apache下。apache默认的主目录是c:\apache\htdocs,我们可以对它进行更改,我们编辑c:\apache/conf/httpd.conf文件,找到关于Port、DocumentRoot这两行,就可以设置当前的http服务的默认端口和主目录。其他的设置,可以自己以后摸索!
在httpd.conf中添加:
LoadModule jk_module libexec/mod_jk.dll
AddModule mod_jk.c
JkWorkersFile "c:\Tomcat 4.0\conf\worker.properties"
JkLogFile logs/mod_jk.log
JkLogLevel info
JkLogStampformat "[%a %b %d %H:%M:%S %Y] "
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
以上语句的基本意思是:
第一、二句:指定apache和tomcat连接插件的位置和名称
第三句:指出mod_jk工作所需要的工作文件的位置
第七、八句:是将所有和jsp和servlet的请求通过Apj13的协议送给Tomcat,让Tomcat来处理
3、将下载的mod_jk.dll放到安装Apache目录下的libexec下。
4、安装tomcat for windows版,这里我用tomcat 4.0.4。比如安装到c:\tomcat4.0下。
在c:\Tomcat4.0\conf目录下建一个worker.properties文件
worker.properties文件内容为:
# $Header: /home/cvs/jakarta-tomcat/src/etc/Attic/workers.properties,v 1.3.2.2 2000/10/16 01:59:22 larryi Exp $
# $Revision: 1.3.2.2 $
# $Date: 2000/10/16 01:59:22 $
#
#
# workers.properties -
#
# This file provides jk derived plugins with with the needed information to
# connect to the different tomcat workers.
#
# As a general note, the characters $( and ) are used internally to define
# macros. Do not use them in your own configuration!!!
#
# Whenever you see a set of lines such as:
# x=value
# y=$(x)something
#
# the final value for y will be valuesomething
#
# Normaly all you will need to modify is the first properties, i.e.
# workers.tomcat_home, workers.java_home and ps. Most of the configuration
# is derived from these.
#
# When you are done updating workers.tomcat_home, workers.java_home and ps
# you should have 3 workers configured:
#
# - An ajp12 worker that connects to localhost:8007
# - An ajp13 worker that connects to localhost:8009
# - A jni inprocess worker.
# - A load balancer worker
#
# However by default the plugins will only use the ajp12 worker. To have
# the plugins use other workers you should modify the worker.list property.
#
#
#
# workers.tomcat_home should point to the location where you
# installed tomcat. This is where you have your conf, webapps and lib
# directories.
#
workers.tomcat_home=c:\Tomcat 4.0
#
# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
#
workers.java_home=c:\jdk1.3
#
# You should configure your environment slash... ps= on NT and / on UNIX
# and maybe something different elsewhere.
#
ps=\
# ps=/
#
#------ ADVANCED MODE ------------------------------------------------
#---------------------------------------------------------------------
#
#
#------ DEFAULT worket list ------------------------------------------
#---------------------------------------------------------------------
#
#
# The workers that your plugins should create and work with
#
worker.list=ajp12, ajp13
#
#------ DEFAULT ajp12 WORKER DEFINITION ------------------------------
#---------------------------------------------------------------------
#
#
# Defining a worker named ajp12 and of type ajp12
# Note that the name and the type do not have to match.
#
worker.ajp12.port=8007
worker.ajp12.host=localhost
worker.ajp12.type=ajp12
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.ajp12.lbfactor=1
#
#------ DEFAULT ajp13 WORKER DEFINITION ------------------------------
#---------------------------------------------------------------------
#
#
# Defining a worker named ajp13 and of type ajp13
# Note that the name and the type do not have to match.
#
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.ajp13.lbfactor=1
#
# Specify the size of the open connection cache.
#worker.ajp13.cachesize
#
#------ DEFAULT LOAD BALANCER WORKER DEFINITION ----------------------
#---------------------------------------------------------------------
#
#
# The loadbalancer (type lb) workers perform wighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
# once in a while. Until then all work is redirected to peer
# workers.
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=ajp12, ajp13
#
#------ DEFAULT JNI WORKER DEFINITION---------------------------------
#---------------------------------------------------------------------
#
#
# Defining a worker named inprocess and of type jni
# Note that the name and the type do not have to match.
#
worker.inprocess.type=jni
#
#------ CLASSPATH DEFINITION -----------------------------------------
#---------------------------------------------------------------------
#
#
# Additional class path components.
#
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)classes
#
# The XML parser provided with Tomcat
#
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)jaxp.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)parser.jar
#
# Tomcat`s implementation
#
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)common$(ps)lib$(ps)jasper.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)common$(ps)lib$(ps)servlet.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)common$(ps)lib$(ps)webserver.jar
#
# Javac as available from Java2SE
#
worker.inprocess.class_path=$(workers.java_home)$(ps)lib$(ps)tools.jar
#
# Setting the command line for tomcat
# Note: The cmd_line string may not contain spaces.
#
worker.inprocess.cmd_line=-config
worker.inprocess.cmd_line=$(workers.tomcat_home)/conf/jni_server.xml
worker.inprocess.cmd_line=-home
worker.inprocess.cmd_line=$(workers.tomcat_home)
#
# The JVM that we are about to use
#
# This is for Java2
#
worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)jvm.dll
#
# And this is for jdk1.1.X
#
#worker.inprocess.jvm_lib=$(workers.java_home)$(ps)bin$(ps)javai.dll
#
#
# Setting the place for the stdout and stderr of tomcat
#
worker.inprocess.stdout=$(workers.tomcat_home)$(ps)inprocess.stdout
worker.inprocess.stderr=$(workers.tomcat_home)$(ps)inprocess.stderr
#
# Setting the tomcat.home Java property
#
worker.inprocess.sysprops=tomcat.home=$(workers.tomcat_home)
#
# Java system properties
#
# worker.inprocess.sysprops=java.compiler=NONE
# worker.inprocess.sysprops=myprop=mypropvalue
#
# Additional path components.
#
# worker.inprocess.ld_path=d:$(ps)SQLLIB$(ps)bin
#
在这个文件中需要修改两个地方,一个是workers.tomcat_home,这是指定Tomcat工作的目录,另外一个是workers.java_home,这是指定Jdk的目录,大家可以做适当修改!
再配置TOMCAT核心文件,打开 c:\Tomcat4.0\conf\server.xml
修改如下:
将<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
port="8009" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>
-->
的<!-- --> 去掉,这句话熟悉XML的人都知道它是注释,因为默认的Tomcat4
是没有启用Apj13的8009的端口即mod_jk所使用,修改后如下:
<Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
port="8009" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>
然后保存,现在你的配置基本完成了。重新启动计算机,开始整体测试。
5、测试:
首先,启动Apache,然后在浏览器里输入http://localhost,如果能看
到Apache的欢迎页面,那么恭喜您,第一步成功了。
然后,运行startup.bat启动Tomcat,再打开浏览器输入:http://localhost:8080/
如果看到可爱的Tomcat,那么恭喜您,Tomcat也没有问题。
最后最重要的是Apache下可以解释jsp和Servlet,把下面的代码copy到文本编辑器中命名为
rosealet.jsp,
内容如下:
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%String str="rosealet studio.";%>
<%str=str+"welcome";%>
<%=str%>
</body>
</html>
将它保存到C:\Tomcat 4.0\webapps\ROOT下,然后在浏览器里输入
http://localhost/rosealet.jsp如果看到了rosealet,那么您的确成功了。
我再阐述一个问题:
对于APACHE主机下访问的JSP文件,实际上物理上不存在与APACHE的主机上,而是存在TOMCAT的主机目录里,例如:
http://localhost/rosealet.jsp 中rosealet.jsp文件实际不在 c:\apache\htdocs\下,而在C:\Tomcat 4.0\webapps\ROOT 下。这点请注意,不要混淆概念!
转自http://www.oxbbs.net/cgi-bin/forums.cgi?forum=38,原作者rosealet
--生活,是时尚,是艺术--
--TheStudioPlus--
补充日期: 2003-05-21 12:30:00
再谈apache+tomcat,对以前文章的补充
这是对以前我的那篇"WIN2K+APACHE+JSP+TOMCAT配置总结"的补充。
一、配置环境变量:
1、以前我们配置的环境变量:
变量名:path
变量值:c:\jdk1.3\bin
变量名:JAVA_HOME
变量值:c:\jdk1.3
变量名:CLASSPATH
变量值:.\;c:\jdk1.3\lib\dt.jar;c:\jdk1.3\lib\tools.jar
2、现在我们也可以配置为:
变量名:JAVA_HOME
变量值:C:\jdk1.3 //若你没按以上路径安装请更改为正确的路径
变量名:TOMCAT_HOME
变量值:C:\Tomcat4 //若你没按以上路径安装请更改为正确的路径
变量名:CLASSPATH
变量值:%JAVA_HOME%\lib;%TOMCAT_HOME%\lib;%JAVA_HOME%\jre\bin
变量名:path
变量值:c:\jdk1.3\bin;c:\jdk1.3\jre\bin;
二、更改tomcat运行测试目录:
假设你已有一个项目在e:\rosealet目录下,包含index.jsp及其它jsp文件和bean,如WEB-INF\classes\下的
servlet、javabean。
1.设置Tomcat以支持你的项目目录,打开C:\Tomcat4\conf\server.xml文件,在"</Context>"-"</Host>"添加
<Context path="/rosealet" debug="0" docBase="e:\rosealet" reloadable="true"/>并保存。
说明:Context (表示一个web应用程序);docBase定义本地路径;path 表示此web路径的url,
reloadable 这个属性非常重要,如果为true,则tomcat会自动检测应用程序的/WEB-INF/lib
和/WEB-INF/classes目录的变化,自动装载修改或新的bean及servlet,我们可以在不重起tomcat
的情况下看到bean带给jsp的变化。
这样请求的url:http://localhost/rosealet
2.启动Tomcat,在浏览器的地址中输入http://localhost/rosealet,如你的jsp及bean,servlet没有错误,
一般会将你的index.jsp/index.html文件的
三、Apache与Tomcat综合配置和虚拟主机配置:
若Apache与Tomcat正运行着,请关闭它们。
(1)配置Apache:
1.将下载的mod_jk.dll复制到c:\apache\modules或c:\apache\libexec 下。(无论复制到什么位置,
httpd.conf中是可以指定相应的位置的)
2.设置主页文件类型:
编辑C:\Apache\conf下的httpd.conf,在"DirectoryIndex"一行,index.html的后面也可以添加
index.jsp并保存,注意相互之间应有空格。
3.设置虚拟主机:
apache中添加虚拟主机格式:
<VirtualHost>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
让Apache自动控制html与jsp的解析
编辑httpd.conf,在最后加入下面这段代码。
<VirtualHost localhost> //localhost为本机,你可用本机ip
ServerAdmin admin@rosealet.com //你的mail地址
DocumentRoot e:\rosealet //你的项目根目录,因为rosealet下可能有其他目路,因此设为e:\rosealet
ServerName localhost //你的服务名,若你的机器有域名,设为域名,现用本机作测试
ErrorLog logs/rosealet_home_log.txt //错误日志,在C:\Apache\logs下,可用其它任意名字
CustomLog logs/rosealet_Custom_log.txt common //访问日志,在C:\Apache\logs下,可用其它任意名字
</VirtualHost>
4.设置Apache与Tomcat之间的连接:
让Apache遇到jsp文件时,在后台将其交由Tomcat去处理。
编辑httpd.conf,在最后加入下面这段代码。
# Using mod_jk.dll to redirect dynamic calls to Tomcat
LoadModule jk_module modules/mod_jk.dll //装载模块,用于处理连接,如mod_jk放在libexec应些为:LoadModule jk_module libexec/mod_jk.dll
AddModule mod_jk.c //使用mod_jk.c的module,此行可删除
JkWorkersFile "c:/tomcat4/conf/workers.properties" //设置模块的工作文件
JkLogFile "/logs/mod_jk.log" //设置模块工作的日志文件,Tocmat启动时会自建
JkLogLevel info //设置日志文件的等级,此行可删除
JkLogStampformat "[%a %b %d %H:%M:%S %Y] " //设置日志文件的格式,此行可删除
JkMount /servlet/* ajp13 //让Apache支持对servlet传送,用以Tomcat解析
JkMount /*.jsp ajp13 //让Apache支持对jsp传送,用以Tomcat解析
到此Apache的配置基本完成,不过这时启动Apache还不能正常工作。
(2)配置Tomcat:
在C:\Tomcat\conf下新建文件workers.properties,以前我们曾经建立过,它的内容非常多
但我们可以把它精简后如下:
workers.tomcat_home=c:\Tomcat4
workers.java_home=c:\jdk1.3
ps=\
worker.list=ajp12, ajp13
worker.ajp12.port=8007
worker.ajp12.host=localhost
worker.ajp12.type=ajp12
worker.ajp12.lbfactor=1
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
worker.ajp13.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=ajp12, ajp13
worker.inprocess.type=jni
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)classes
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)jaxp.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)parser.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)common$(ps)lib$(ps)jasper.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)common$(ps)lib$(ps)servlet.jar
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)common$(ps)lib$(ps)webserver.jar
worker.inprocess.class_path=$(workers.java_home)$(ps)lib$(ps)tools.jar
worker.inprocess.cmd_line=-config
worker.inprocess.cmd_line=$(workers.tomcat_home)/conf/jni_server.xml
worker.inprocess.cmd_line=-home
worker.inprocess.cmd_line=$(workers.tomcat_home)
worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)jvm.dll
worker.inprocess.stdout=$(workers.tomcat_home)$(ps)inprocess.stdout
worker.inprocess.stderr=$(workers.tomcat_home)$(ps)inprocess.stderr
worker.inprocess.sysprops=tomcat.home=$(workers.tomcat_home)
我们还可以更加精简,因我们只用ajp13其内容如下:
workers.tomcat_home=c:\Tomcat4 //让mod_jk模块知道Tomcat
workers.java_home=c:\jdk1.3 //让mod_jk模块知道j2sdk
ps=\
worker.list=ajp13 //模块版本,现有ajp14了,不要修改
worker.ajp13.port=8009 //工作端口,若没占用则不用修改
worker.ajp13.host=localhost //本机,若上面的Apache主机不为localhost,作相应修改
worker.ajp13.type=ajp13 //类型
worker.ajp13.lbfactor=1 //代理数,不用修改
2.编辑server.xml文件:
让tomcat打开ajp13端口8009
找到:
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector className="org.apache.ajp.tomcat4.Ajp13Connector" port="8009" minProcessors="5"
maxProcessors="75" acceptCount="10" debug="0"/>
-->
更改为:
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector className="org.apache.ajp.tomcat4.Ajp13Connector" port="8009" minProcessors="5"
maxProcessors="75" acceptCount="10" debug="0"/>
四、测试:
1.启动Apache,Tomcat。
2.打开你的浏览器,在地址栏中输入http://localhost:8080/rosealet ,若与上面的测试相同,
则Tomcat重新配置后正常!
3.在地址栏中输入http://localhost/rosealet ,若与http://localhost:8080/rosealet的结果相同,
那恭喜你,Apache与Tomcat整合成功了!
五、虚拟主机下的配置、管理和应用:
下面以一个例子来说明虚拟主机如何同时使用Apache与Tomcat
虚拟主机host1的目录结构如下所示:
/www
|---host1 虚拟主机host1的主目录
| |--htdocs 普通根目录:用于显示静态页面或php程序
| |----logs apache访问日志
| |----webapps java应用目录(新加,与tomcat的webapps目录无任何关联)
| |--ROOT jsp根目录:用于显示jsp页面程序
| |----kim 一套java应用实例
|---host2 虚拟主机host2的主目录
............
要让虚拟主机host1能同时使用Apache与Tomcat4,需要做如下配置:
1、编辑tomcat的配置文件server.xml,在最后的部分找到:
< !-- Define an Apache-Connector Service -- >
<!--
< Service name="Tomcat-Apache" >
< Connector className="org.apache.catalina.connector.warp.WarpConnector"
port="8008" minProcessors="5" maxProcessors="75"
enableLookups="true" appBase="webapps"
acceptCount="10" debug="0"/ >
< Engine className="org.apache.catalina.connector.warp.WarpEngine"
name="Apache" debug="0" >
< Logger className="org.apache.catalina.logger.FileLogger"
prefix="apache_log." suffix=".txt"
timestamp="true"/ >
< Realm className="org.apache.catalina.realm.MemoryRealm" / >
-- >
修改为:
< !-- Define an Apache-Connector Service -- >
< Service name="Tomcat-Apache" >
< Connector className="org.apache.catalina.connector.warp.WarpConnector"
port="8008" minProcessors="5" maxProcessors="75"
enableLookups="true" appBase="webapps"
acceptCount="10" debug="0"/ >
< Engine className="org.apache.catalina.connector.warp.WarpEngine"
name="Apache" debug="0" >
< Logger className="org.apache.catalina.logger.FileLogger"
prefix="apache_log." suffix=".txt"
timestamp="true"/ >
< !-- 下面是新加的内容 -- >
< Realm className="org.apache.catalina.realm.MemoryRealm" / >
< Host name="host1.rosealet.com" debug="0" appBase="/www/host1/webapps"
uppackWars="true" autoDeploy="true" > //定义虚拟主机host1的主目录
< Context path="" docBase="ROOT" debug="0"/ > //定义jsp根目录
< !-- 下面套用的是tomcat自带的管理页面,在虚拟主机下也可使用 -- >
< Context path="/manager" debug="0" privileged="true"
docBase="../server/webapps/manager"/ > //"../server/webapps/manager"指定到tomcat安装目录
<Context path="/admin" docBase="../server/webapps/admin"
debug="0" privileged="true"> //"../server/webapps/manager"指定到tomcat安装目录
< Context path="/kim" docBase="kim" debug="0"
reloadable="true" crossContext="true"/ > //在/kim下定义java应用实例
< Logger className="org.apache.catalina.logger.FileLogger"
prefix="host1_log." suffix=".txt"
timestamp="true"/ >
< /Host >
< !-- 新加内容结束 -- >
< /Engine >
< /Service >
2、编辑tomcat的管理权限文件tomcat-users.xml,在中间加一行:
< user username="name" password="passwd" roles="standard,manager"/ > //name、passwd可自定义
3、编辑apache的配置文件,httpd.conf做如下修改:
< VirtualHost xxx.xxx.xxx.xxx >
ServerName host1.rosealet.com
ServerAdmin host1@rosealet.com
DocumentRoot /www/host1/htdocs
ErrorLog /www/host1/logs/error_log
CustomLog /www/host1/logs/access_log common
WebAppDeploy ROOT warpConnection /
< /VirtualHost >
// 注:此行的作用是把ROOT目录作为虚拟主机URL的根,并使前面定义的根htdocs失效。
但如果不想整个虚拟主机都使用jsp页面程序,而想使用大量静态页面或php程序的话,
就要使用下面这行,并把所有的静态页面和php程序放在htdocs目录下。
// WebAppDeploy ROOT warpConnection /jsp
WebAppDeploy manager warpConnection /manager
WebAppDeploy jive warpConnection /kim
// 在server.xml里每定义一个都要在这里加一条WebAppDeploy。
以后访问http://host1.rosealet.com/manager/或http://host1.rosealet.com/admin/ 会提示输入密码,
在输入正确的 name和passwd后,就会看到“Tomcat Web Application Manager”的页面,里面的
Application Path已经是虚拟主机里配置的信息了,并可以对其进行管理。
到此虚拟主机下的配置和管理就全部完成,剩下的事情就是测试和应用了。