24 虚拟主机与Tomcat【Virtual Hosting and Tomcat】

来源:互联网 发布:关家垴战斗 知乎 编辑:程序博客网 时间:2024/05/29 16:41

Apache Tomcat 6.0

虚拟主机与Tomcat【Virtual Hosting and Tomcat

目录【Table of Contents
  • 背景【Assumptions
  • server.xml
  • Web应用目录【Webapps Directory
  • 配置环境【Configuring Your Contexts
    1. 综述【General】
    2. context.xml 配置方法#1 【context.xml - approach #1
    3. context.xml 配置方法#2 【context.xml - approach #2
    4. 进阶内容【Further Information
背景【Assumptions

作为全文的背景,假设你要将你开发的应用程序映射到名叫ren和stimpy的主机名上。还要假设,有一个正在运行的Tomcat服务,$CATALINA_HOME 是对于这个服务的安装路径的引用,例如:/usr/local/tomcat。
For the sake of this how-to, assume you have a development host with two host names, ren and stimpy. Let's also assume one instance of Tomcat running, so $CATALINA_HOME refers to wherever it's installed, perhaps /usr/local/tomcat.

 

另外,文中会使用unix系统的路径分隔符和命令;如果你使用的是windows系统,请自行修改。

Also, this how-to uses Unix-style path separators and commands; if you're on Windows modify accordingly.

server.xml

作为最简单的方法,就是去编辑server.xml文件的Enging元素,像下面这样:
At the simplest, edit the Engine portion of your server.xml file to look like this:

<Engine name="Catalina" defaultHost="ren">    <Host name="ren"    appBase="renapps"/>    <Host name="stimpy" appBase="stimpyapps"/></Engine>    

注意,每个应用的appBase下的目录结构都可能不太一样。可以查看Engine和Host元素的属性的相关文档。

Note that the directory structures under the appBase for each host should not overlap each other.

Consult the configuration documentation for other attributes of the Engine and Host elements.

Web应用目录【Webapps Directory

为每一个虚拟主机创建如下的目录结构:
Create directories for each of the virtual hosts:

mkdir $CATALINA_HOME/renappsmkdir $CATALINA_HOME/stimpyapps    
配置环境【Configuring Your Contexts
综述【General

Contexts元素通常位于appBase目录下。例如,将ren虚拟主机映射到foobar.war这个文件上,使用$CATALINA_HOME/renapps/foobar.war。虚拟主机ren的默认或者ROOT环境是$CATALINA_HOME/renapps/ROOT.war (WAR) 或者 $CATALINA_HOME/renapps/ROOT (directory).
Contexts are normally located underneath the appBase directory. For example, to deploy the foobar context as a war file in the ren host, use $CATALINA_HOME/renapps/foobar.war. Note that the default or ROOT context for ren would be deployed as $CATALINA_HOME/renapps/ROOT.war (WAR) or $CATALINA_HOME/renapps/ROOT (directory).

 

注意:一个主机环境下的docBase与appBase是不同的。

NOTE: The docBase for a context should never be the same as the appBase for a host.

context.xml配置方法1context.xml - approach #1

在你的环境下,创建一个META-INF目录,将你的环境上下文定义写在这个目录下的context.xml文件中。例如$CATALINA_HOME/renapps/ROOT/META-INF/context.xml 这样使得你将应用打包成一个war文件时,部署是非常容易的。
Within your Context, create a META-INF directory and then place your Context definition in it in a file named context.xml. i.e. $CATALINA_HOME/renapps/ROOT/META-INF/context.xml This makes deployment easier, particularly if you're distributing a WAR file.

context.xml配置方法1【context.xml - approach #2

$CATALINA_HOME/conf/Catalina 下创建与你的虚拟主机一致的结构,例如:
Create a structure under $CATALINA_HOME/conf/Catalina corresponding to your virtual hosts, e.g.:

mkdir $CATALINA_HOME/conf/Catalina/renmkdir $CATALINA_HOME/conf/Catalina/stimpy      

注意上面Catalina目录后面的目录名称,这个名字与Enging元素的name属性一致。

Note that the ending directory name "Catalina" represents the name attribute of the Engine element as shown above.

 

现在对于你的默认应用,添加如下:

Now, for your default webapps, add:

$CATALINA_HOME/conf/Catalina/ren/ROOT.xml$CATALINA_HOME/conf/Catalina/stimpy/ROOT.xml      

 

如果你需要使用Tomcat manager管理所有主机的web应用,你需要再进行如下操作:

If you want to use the Tomcat manager webapp for each host, you'll also need to add it here:

cd $CATALINA_HOME/conf/Catalinacp localhost/manager.xml ren/cp localhost/manager.xml stimpy/      
进阶内容【Further Information

请查阅Context元素的其他属性的配置文档

Consult the configuration documentation for other attributes of the Context element.

 

-------------------------------------------------------

终于在年前将Tomcat6新增的这几篇文档翻译完了,这些日子一直查看自己的博客,根本就没人看呀,呵呵,可见自己根本没有人气。要努力了。今天是大年二十九,上班的最后一天,明天放假准备过年,祝我新的一年取得理想的成绩。