Windows2003下Apache+Tomcat整合并配置虚拟主机全文档

2009-12-9 16:33:14  阅读数:  

关键字:Apache2.0.55 Tomcat5.5.9 ,整合,虚拟主机,文档

步骤:

1、  下载:

a)      下载Apache:本例选用Apache2.0.55版本,下载地址到Apache官方网站,http://archive.apache.org/dist/httpd/binaries/win32/  本例下载MSI格式安装文件apache_2.0.55-win32-x86-no_ssl.msi 

b)        下在JDK:本例采用JDK1.5

c)        下载Tomcat:本例选用Tomcat5.5.9版本,下载地址到Tomcat官方网站,http://tomcat.apache.org/

d)        下载连接器jakarta-tomcat-connectors,下载地址http://tomcat.apache.org/

 

全部下载后,如下图所示

 

 

2、  安装:

a)         安装Apache

                         i.              直接双击Apache安装文件apache_2.0.55-win32-x86-no_ssl.msi安装,安装过程中,需要写入主机信息,如下图。此外,选择for all users, on port 80,as a Service –Recommended选项。

                       ii.              选择安装路径,如下图:

                      iii.              需要选择

                     iv.              安装完毕,如下图所示。

                    v.              如果有防火墙,设置防火墙例外,如下图:

                vi.              安装为服务后,启动Apache Service Monitor工具,

打开Apache Service Monitor工具后,如下图所示,可以通过此工具直接启动、停止、重启的Apache服务器。

vii.        测试安装是否成功,在地址栏输入:http://127.0.0.1/ ,若显示如下图,则表示Apache安装成功。

b)        安装JDK,直接运行jdk-1_5_0_06-windows-i586-p.exe安装。指定安装位置,如下图所示,本例安装于C:/Program Files\Java\jdk1.5.0_06目录下,记住此位置,在后边安装Tomcat的时候需要指定。

c)        安装Tomcat,

                         i.              安装Tomcat,选择Tomcat安装路径,如下图:

                       ii.             

                      iii.              输入Tomcat的端口,后台管理帐户与密码,如下图,一定要牢记帐户密码,后边配置需要使用。

                     iv.             

                       v.              选择JDK的安装路径,如下图,

                     vi.             

                    vii.              安装完毕后启动

                  viii.             

                     ix.              测试安装是否成功,在地址栏输入http://127.0.0.1:8080/ 此时,出现下边图这个页面则说明Tomcat安装通过。

3、  配置:

a)         配置Apache虚拟主机:

                         i.              http.conf最后加入一行LoadModule jk2_module modules/mod_jk2.so

                       ii.              配置Apache的虚拟主机:本例子计划配置的两个虚拟主机为www.hnspi.edu.cnbbs.hnspi.edu.cn ,具体格式如下,请更改后加入你的http.conf文档中。参数说明:ServerAdmin用来定义管理员E-Mail,用来在网站不能正常显示的时候显示,DocumentRoot定义虚拟主机的根目录,ServerName用来定义虚拟主机,ErrorLog定义错误日志文件名,CustomLog定义日志文件。

<VirtualHost *:80>

    ServerAdmin webmaster@mail.hnspi.net

    DocumentRoot C:/web

    ServerName www.hnspi.edu.cn

    ErrorLog logs/www.hnspi.edu.cn-error_log

    CustomLog logs/www.hnspi.edu.cn-access_log common

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin webmaster@mail.hnspi.net

    DocumentRoot C:/bbs

    ServerName bbs.hnspi.edu.cn

    ErrorLog logs/bbs.hnspi.edu.cn-error_log

    CustomLog logs/bbs.hnspi.edu.cn-access_log common

</VirtualHost>

重新启动Apache

测试Apache虚拟主机,在c:/web目录下新建test.htm文件,用记事本打开输入

Welcome to HaiNan SoftWare Profession Institute <br>

web:http://www.hnspi.net

保存。

c:/bbs目录下新建test.htm文件,用记事本打开输入

Welcome to HaiNan SoftWare Profession Institute <br>

web:http://bbs.hnspi.edu.cn

保存。

在地址栏输入http://www.hnspi.edu.cn/test.htm看到如下图:

 在地址栏输入http://bbs.hnspi.edu.cn/test.htm看到如下图,

则说明Apache虚拟主机配置完成。

b)        配置Tomcat

                         i.              添加admin模块:配置Tomcat的虚拟主机,所有对于Tomcat的配置在本例中采用管理控制台来配置,在本例采用的Tomcat5.5.9版本中,默认是不带有管理控制台的,因此需要另外再下载,下载地址为http://archive.apache.org/dist/tomcat/tomcat-5/archive/v5.5.9/bin/ ,选择jakarta-tomcat-5.5.9-admin.zip文件,下载后,解压,将server/webapps/下的admin目录复制到Tomcat安装目录的server/webapps/目录下,然后将conf/Catalina/localhost目录下的admin.xml文件复制到Tomcat安装目录的conf/Catalina/localhost目录下,然后重新启动Tomcat。在地址栏中输入http://127.0.0.1:8080/admin/ ,如果出现如下界面说明admin管理控制台可以正常使用。

                       ii.              配置Tomcat虚拟主机:在地址栏中输入http://127.0.0.1:8080/admin/   ,输入安装时候的输入的用户名和密码,登入后如下图所示:

然后点Service (Catalina),出现如下界面,然后选择Create New Host来创建主机;

如下图,Name填写虚拟主机的名称,本例子填写www.hnspi.edu.cn Application Base:

填写虚拟主机的根目录,本例中指向C:/web文件夹,设置好后,点Save按钮保存

测试虚拟主机:在C:/web文件夹下新建一个test.jsp文件,输入内容

<%out.println("Welcome to HaiNan SoftWare Profession Institute");

out.println("<br> web:http://www.hnspi.net");

%>

后保存,在地址栏输入http://www.hnspi.edu.cn:8080/test.jsp ,若出现如下界面,说明Tomcat虚拟主机配置完成。

另外一个Tomcat虚拟主机bbs.hnspi.edu.cn配置过程与www.hnspi.edu.cn配置相同。

                      iii.             

c)        整合ApacheTomcat

                         i.              解压连接器jakarta-tomcat-connectors,将modules目录下mod_jk2.so复制到Apache安装目录下的modules目录下。

                       ii.              http.conf的最后加上下边代码

<IfModule !mod_jk2.c>
LoadModule jk2_module modules/mod_jk2.so
</IfModule>

                      iii.              Apacheconf文件夹下新建workers2.properties文件(如果文件不存在新建),然后输入下边代码后保存:

[channel.socket:localhost:8009]

port=8009

host=127.0.0.1

 

[ajp13:localhost:8009]

channel=channel.socket:localhost:8009

 

#[uri:/*]

#worker=ajp13:localhost:8009

 

# Uri mapping

[uri:127.0.0.1/*.jsp]

worker=ajp13:localhost:8009

 

[uri:www.hnspi.edu.cn/*.jsp]

worker=ajp13:localhost:8009

 

[uri:bbs.hnspi.edu.cn/*.jsp]

worker=ajp13:localhost:8009

                     iv.              重新启动Apache,测试,在地址栏输入http://www.hnspi.edu.cn/test.jsp ,如果看到如下图结果,则说明整合成功。

 

附录一:Apache以服务方式运行

Apache仅能够在Windows NT上作为服务运行。

你可以选择在安装Apache时自动将其安装为一个服务。如果你选择"for all users",那么Apache将会被安装为服务。如果你选择了"only for the Current User",你可以在安装后手动将Apache注册为服务。注意,你必须是Administrators组的成员才能成功注册服务。

使用Apache Service Monitor工具,可以查看和管理你所在网络上的所有机器上安装的Apache服务的状态。为了能够使用这个工具管理Apache服务,你必须首先自动或手动安装Apache服务。

你可以在Apache安装目录的bin子目录下,使用如下命令将Apache安装为Windows NT服务:

apache -k install

如果你想指定服务的名称,可以使用下面的命令。当你在同一机器上安装多个Apache服务时,你必须为它们指定不同的名字。

apache -k install -n "服务名"

如果你想为不同名称的服务使用不同的配置文件,则安装时需要指定配置文件:

apache -k install -n "服务名" -f "c:\files\my.conf"

如果你使用的是第一个命令,也就是除 -k install 外没有其它命令行参数,那么被安装的服务名称将是:Apache2 ,配置文件将使用conf\httpd.conf

要移除一个Apache服务很简单:

apache -k uninstall

或者使用下述命令移除特定名称的Apache服务:

apache -k uninstall -n "服务名"

通常,启动、重启、关闭Apache服务的方法是使用Apache Service Monitor工具,另外也可以使用控制台命令:NET START Apache2 NET STOP Apache2 或者通过Windows服务控制面板。在启动Apache服务之前,你应当使用下面的命令检查一下配置文件的正确性:

apache -n "服务名" -t

你可以通过命令行开关来控制Apache服务。要启动一个已经安装的Apache服务,可以使用:

apache -k start

要停止一个已经安装的Apache服务,可以使用:

apache -k stop

apache -k shutdown

要重启一个运行中的Apache服务,强制它重新读取配置文件,可以使用:

apache -k restart

附录二:ApacheWindows下的安全性

默认情况下,Apache服务将被注册为以本地系统用户(LocalSystem帐号)身份运行。LocalSystem帐号没有网络权限,不能通过任何Windows安全机制访问网络,包括文件系统、命名管道、DCOMsecure RPC ,但是它对于本地资源却拥有广泛的特权。

永远不要把网络权限授予LocalSystem帐号!如果你需要Apache能够访问网络资源,最好按照下述方法为Apache另外建立一个单独的帐号。

你应该建立一个单独的帐号来运行Apache服务。特别是在必须通过Apache访问网络资源的时候,我们更加强烈建议你这样做。

  1. 创建一个普通域用户帐号,并牢记对应的密码。
  2. 授予这个新建的帐号作为服务登陆作为操作系统一部分运行权限。在Windows 2000/XP/2003上你可以使用"组策略"或通过"本地安全策略"MMC接口来完成这个操作。
  3. 确认新建的帐号是Users组的一个成员。
  4. 确认新建的帐号具有读取和执行(RX)所有文档和脚本目录(例如:htdocscgi-bin)的权限。
  5. 确认新建的帐号对Apachelogs目录具有读//删除(RWD)的权限。
  6. 确认新建的帐号对Apache.exe二进制文件具有读取和执行(RX)的权限。

一个很好的实践经验是赋予运行Apache服务的用户读取和执行(RX)整个Apache2目录的权限,并且对logs子目录具有读//删除(RWD)的权限。

如果你允许使用这个帐号作为一个用户和服务登录,你就可以用这个帐号登录上去测试执行脚本、读取web页的权限,还可以通过控制台窗口启动Apache 。如果这样工作正常,你又执行了上述的操作,那么Apache就能够正常地作为服务运行了。

错误代码2186是一个很好的提示,说明你需要检查"登陆为"选项,因为服务器不能访问必要的网络资源。

当启动Apache服务时你可能会遇到一个来自Windows服务管理器的错误信息。 例如,如果你想使用控制面板中的服务小程序启动Apache,可能会得到下面这条信息:

Could not start the Apache2 service on \\COMPUTER
Error 1067; The process terminated unexpectedly.

只要启动Apache出错你就会得到这个错误信息。为了弄清是什么引起了错误, 你应该遵循作为控制台程序运行Apache中的建议。

对于让ApacheWindows 9x下以类似Windows NT服务的方式运行有一些支持。都是高度试验性的,即使能够工作,Apache软件基金会将不会证实其可靠性和未来的支持。继续进行你自己的冒险吧!

这两种"服务"有相当大的区别:

如果你输入下列命令,Apache会尝试启动,如果成功他将在后台运行:

Apache -n "服务名" -k start

例如,通过桌面的快捷方式运行,如果服务启动成功一个控制台窗口会快速闪过并立刻消失。如果启动时Apache检测到比如httpd.conf文件中有不正确的设置这样的问题,则控制台窗口会保持可见。这样将显示一个有助于追踪错误原因的错误信息。

Windows 9x不支持NET START或者NET STOP命令,因此你在命令中必须使用Apache的服务控制选项。你可能希望为每个命令设置一个快捷方式以便你能够只需要从开始菜单或者桌面一点就能执行所需的操作。

ApacheWindows 9x没有提供让Apache服务以特定的具有网络权限的用户身份运行的支持。实际上,Windows 9x在本地机器上根本没有提供安全性。这就是Apache软件基金会从不支持使用Windows 9x作为公用httpd服务器的原因。这个便利存在的唯一目的是协助用户开发web内容和学习Apache服务器,或者也许在一个安全的、私有的网络上充当intranet服务器。

英文:

附录一:

Apache can be run as a service on Windows NT. There is some highly experimental support for similar behavior on Windows 9x.

You can install Apache as a service automatically during the installation. If you chose to install for all users, the installation will create an Apache service for you. If you specify to install for yourself only, you can manually register Apache as a service after the installation. You have to be a member of the Administrators group for the service installation to succeed.

Apache comes with a utility called the Apache Service Monitor. With it you can see and manage the state of all installed Apache services on any machine on your network. To be able to manage an Apache service with the monitor, you have to first install the service (either automatically via the installation or manually).

You can install Apache as a Windows NT service as follows from the command prompt at the Apache bin subdirectory:

apache -k install

If you need to specify the name of the service you want to install, use the following command. You have to do this if you have several different service installations of Apache on your computer.

apache -k install -n "MyServiceName"

If you need to have specifically named configuration files for different services, you must use this:

apache -k install -n "MyServiceName" -f "c:\files\my.conf"

If you use the first command without any special parameters except -k install, the service will be called Apache2 and the configuration will be assumed to be conf\httpd.conf.

Removing an Apache service is easy. Just use:

apache -k uninstall

The specific Apache service to be uninstalled can be specified by using:

apache -k uninstall -n "MyServiceName"

Normal starting, restarting and shutting down of an Apache service is usually done via the Apache Service Monitor, by using commands like NET START Apache2 and NET STOP Apache2 or via normal Windows service management. Before starting Apache as a service by any means, you should test the service's configuration file by using:

apache -n "MyServiceName" -t

You can control an Apache service by its command line switches, too. To start an installed Apache service you'll use this:

apache -k start

To stop an Apache service via the command line switches, use this:

apache -k stop

or

apache -k shutdown

You can also restart a running service and force it to reread its configuration file by using:

apache -k restart

 

附录二:

By default, all Apache services are registered to run as the system user (the LocalSystem account). The LocalSystem account has no privileges to your network via any Windows-secured mechanism, including the file system, named pipes, DCOM, or secure RPC. It has, however, wide privileges locally.

Never grant any network privileges to the LocalSystem account! If you need Apache to be able to access network resources, create a separate account for Apache as noted below.

You may want to create a separate account for running Apache service(s). Especially, if you have to access network resources via Apache, this is strongly recommended.

1.      Create a normal domain user account, and be sure to memorize its password.

2.      Grant the newly-created user a privilege of Log on as a service and Act as part of the operating system. On Windows NT 4.0 these privileges are granted via User Manager for Domains, but on Windows 2000 and XP you probably want to use Group Policy for propagating these settings. You can also manually set these via the Local Security Policy MMC snap-in.

3.      Confirm that the created account is a member of the Users group.

4.      Grant the account read and execute (RX) rights to all document and script folders (htdocs and cgi-bin for example).

5.      Grant the account change (RWXD) rights to the Apache logs directory.

6.      Grant the account read and execute (RX) rights to the Apache.exe binary executable.

It is usually a good practice to grant the user the Apache service runs as read and execute (RX) access to the whole Apache2 directory, except the logs subdirectory, where the user has to have at least change (RWXD) rights.

If you allow the account to log in as a user and as a service, then you can log on with that account and test that the account has the privileges to execute the scripts, read the web pages, and that you can start Apache in a console window. If this works, and you have followed the steps above, Apache should execute as a service with no problems.

Error code 2186 is a good indication that you need to review the "Log On As" configuration for the service, since Apache cannot access a required network resource. Also, pay close attention to the privileges of the user Apache is configured to run as.

When starting Apache as a service you may encounter an error message from the Windows Service Control Manager. For example, if you try to start Apache by using the Services applet in the Windows Control Panel, you may get the following message:

Could not start the Apache2 service on \\COMPUTER
Error 1067; The process terminated unexpectedly.

You will get this generic error if there is any problem with starting the Apache service. In order to see what is really causing the problem you should follow the instructions for Running Apache for Windows from the Command Prompt.

There is some support for Apache on Windows 9x to behave in a similar manner as a service on Windows NT. It is highly experimental. It is not of production-class reliability, and its future is not guaranteed. It can be mostly regarded as a risky thing to play with - proceed with caution!

There are some differences between the two kinds of services you should be aware of:

·         Apache will attempt to start and if successful it will run in the background. If you run the command

apache -n "MyServiceName" -k start

via a shortcut on your desktop, for example, then if the service starts successfully, a console window will flash up but it immediately disappears. If Apache detects any errors on startup such as incorrect entries in the httpd.conf configuration file, the console window will remain visible. This will display an error message which will be useful in tracking down the cause of the problem.

·         Windows 9x does not support NET START or NET STOP commands. You must control the Apache service on the command prompt via the -k switches.

·         Apache and Windows 9x offer no support for running Apache as a specific user with network privileges. In fact, Windows 9x offers no security on the local machine, either. This is the simple reason because of which the Apache Software Foundation never endorses use of a Windows 9x -based system as a public Apache server. The primitive support for Windows 9x exists only to assist the user in developing web content and learning the Apache server, and perhaps as an intranet server on a secured, private network.

Once you have confirmed that Apache runs correctly as a console application you can install, control and uninstall the pseudo-service with the same commands as on Windows NT. You can also use the Apache Service Monitor to manage Windows 9x pseudo-services.

 

  • 新闻搜索