同济大学德语听力mp3:分布式部署Apache与Tomcat, 整合经历 (完全成功)

来源:百度文库 编辑:偶看新闻 时间:2024/05/08 12:16:19
分布式部署Apache与Tomcat, 整合经历 (完全成功)

一、技术准备:

Tomcat是Sun和Apache合作开发的JSP Server,支持Servlet 和JSP,Tomcat本身可以作为WebServer,当处理静态页面时,Tomcat不如Apache迅速,不象Apache一样可配置,强壮。Apache作为最流行的Web服务器功能强大,高效,但并不支持JSP及Servlet,所以通常的做法是把它们整合起来,让Apache处理静态页面,而把动态页面的请求交给Tomcat处理,发挥各自的优势. 通过在Apache中加载整合模块和进行设置,Apache就能够根据URL,把不属于自己的请求转给Tomcat。

要让Apache和Tomcat联合工作,还必须有一个连接器把它们联系起来,Connector对于性能、配置的方便性有很重要的影响,目前大致上有JK1.x,JK2,mod_webapp三种connector可以使用。JK使用比较广泛,JK2是JK1.x的改进,比较新。这三种Connector的配置方法大相径庭。



二、软件准备:

1.服务器1的功能:

       运行Apache前端,直接面向web Client(各种浏览器)。

2.服务器1上的软件环境:

操作系统:RedHat AdvanceEnterpriseServer3,该系统默认安装了apache,但是好像功能不全。

IP地址:192.168.1.114

3.服务器1上需要的软件:

Apache(版本为2.0.52)源代码:Apache httpd-2.0.52.tar.gz 可以从http://www.apache.org下载。

Jk2Connector(版本为2.2.0.4)源代码:jakarta-tomcat-connectors-jk2-src-current.tar.gz可以从http://www.apache.org/dist/jakarta/tomcat-connectors/jk2/下载。

4.服务器2的功能:

运行tomcat,处理服务器1转发来的web Client的页面请求,并且将回应页面发送给服务器1上的Apache,已使Apache将此回应转发给web Client。

5.服务器2上的软件环境:

       操作系统:Micorsoft Windows2003 Server。

       IP地址:192.168.1.111

6.服务器2上需要的软件:

Tomcat(版本为4.1以上)的二进制安装程序:可以从http://www.apache.org/dist/jakarta/tomcat-4/下载,我使用的是Tomcat4.1。

Java开发包(版本为1.4):可以从http://www.sun.com下载。我安装的j2dk1.4。



三、安装Tomcat:

       安装Tomcat之前先要安装j2dk,并且设置JAVA_HOME和将$JAVA_HOME\bin放入到Path中。

       由于java开发包和tomcat4.1的安装在windows下进行,所以整个的安装过程比较简单,就不多言了。



四、配置Tomcat:

       根据网上的一些关于Apache和Tomcat整合的文章,和我自身的经验,配置Tomcat主要需要以下两个步骤:

1.  修改Tomcat\conf目录下的jk2.properties文件,修改后的文件内容如下:

## THIS FILE MAY BE OVERRIDEN AT RUNTIME. MAKE SURE TOMCAT IS STOPED

## WHEN YOU EDIT THE FILE.



## COMMENTS WILL BE _LOST_



## DOCUMENTATION OF THE FORMAT IN JkMain javadoc.



# Set the desired handler list

# handler.list=apr,request,channelJni

handler.list = channelSocket,request

#

# Override the default port for the socketChannel

# channelSocket.port=8019

channelSocket.port=8009

# Default:

# channelUnix.file=${jkHome}/work/jk2.socket

# Just to check if the the config  is working

# shm.file=${jkHome}/work/jk2.shm



# In order to enable jni use any channelJni directive

# channelJni.disabled = 0

# And one of the following directives:



# apr.jniModeSo=/opt/apache2/modules/mod_jk2.so



# If set to inprocess the mod_jk2 will Register natives itself

# This will enable the starting of the Tomcat from mod_jk2

# apr.jniModeSo=inprocess

很明确目的就是定义Tomcat和Apache通讯的端口,我设置为8009。



2.  修改Tomcat\conf目录下的server.xml文件,增加一个虚拟主机:

;

;

;

;

理所应当,建立这个虚拟主机是必须的,否则Tomcat将无法/webapps/Root/中的页面。



五、蒙难日到来了!-安装Apache:

1.初次安装jk2Connector:

       由于RedHat AdvanceEnterprise Server默认安装了Apache,但是这个Apache中缺少,整合工作过程中所需要的重要部件-mod_jk2.so库文件,而要得到这个库文件就要编译Jk2Connector源代码。

      但是当我解开jakarta-tomcat-connectors-jk2-src-current.tar.gz,进入到jakarta-tomcat-connectors-jk2-2.0.4-src/jk/native2,执行./configure时发生了错误1:

       [root@sms jk]# cd native2/

[root@sms native2]# ls

aclocal.m4      BUILD.txt     common        INSTALL.txt  scripts

apr             build.xml     configure     jni          server

autom4te.cache  CHANGES.html  configure.in  Makefile.in  STATUS.txt

buildconf.sh    CHANGES.txt   include       README.txt   tomcat

[root@sms native2]# ./configure

checking build system type... i686-redhat-linux-gnu

checking host system type... i686-redhat-linux-gnu

checking target system type... i686-redhat-linux-gnu



checking for mkdir... /bin/mkdir

no apxs given

no apxs2 given

checking checking for apache13...... not provided

checking checking for apache20...... not provided

checking checking for iis...... not provided

checking checking for iPlanet...... not provided

checking for tomcat33 location... not provided

checking for tomcat40 location... not provided

checking for tomcat41 location... not provided

configure: error: Cannot find any WebServer

       配置被错误中断了,原因好像是要求的apxs或者apxs2没有提供。apxs或者apxs2是什么?于是我help了一下:

       [root@sms native2]# ./configure --help

...

  --with-apxs=FILE      location of apxs for Apache 1.3

  --with-apxs2=FILE      location of apxs for Apache 2.0

...

       真的需要提供apxs文件的路径,用locate命令查找一下:

[root@sms httpd]# locate apsx

[root@sms httpd]#

系统中不存在apsx这个文件。这可急坏了我。为什么RedHat在发布企业版时不做完整!往往易用的事物都有隐藏的缺陷,这真是至理名言呀!没办法,只好自己安装一个了。

2.安装Apache2.0.52:

       按照linux下安装程序的三部曲:./configure,make,makeinstall。这个过程对于一般的linux爱好者来讲,It’seasy。。JApache2.0.52被安装在了/usr/local/Apache目录下,并且那个apxs文件也被自动放在了/usr/local/apache2/bin/apxs。

3.再次编译安装jk2Connector:

       注意在configure时一定要指定apxs文件的路径!不然就白安装Apache2.0.52了。

       [root@sms native2]# ./configure --with-apxs2=/usr/local/apache2/bin/apxs

       …

[root@sms native2]# make



Make完成后,在/home/liudan/jakarta-tomcat-connectors-jk2-2.0.4-src/jk/build/jk2/apache2目录下生成了许多的文件和一个usr目录。但是我在makeinstall时发生了错误,只好手动安装了。L

       看看那个usr目录下有些什么:

       [root@sms apache2]# ls usr/local/apache2/modules/

mod_jk2.a  mod_jk2.la  mod_jk2.so

       哦!我要找的mod_jk2.so就在这里,将它拷贝到Apache的modules目录中。

[root@sms apache2]# cp usr/local/apache2/modules/* /usr/local/apache2/modules/

4.小结:

       这样Apache2.0.52和jk2Connector就安装好了。



六、拨云见日-配置Apache:

       Apache的配置文件放在/usr/local/Apache/conf目录中。Apache的整个配置由建立或者修改其中的两个文件组成。

1.建立workers2.properties:

# only at beginnin. In production uncomment it out

[logger.apache2]

level=DEBUG



[shm]

file=/usr/local/apache/logs/shm.file

size=1048576



# Example socket channel, override port and host.

[channel.socket:192.168.1.111]

port=8009

host=192.168.1.111

       在/usr/local/Apache/conf目录中建立这个文件的目的同在配置Tomcat时修改jk.properties文件一样,是告诉Apache如何连接Tomcat。

2.修改httpd.conf文件,添加LoadModule和增加一个虚拟主机。



LoadModule jk2_module "/usr/local/apache2/modules/mod_jk2.so"



NameVirtualHost *:80

;

     ServerAdmin webmaster@demo

     ServerName 192.168.1.114

     DirectoryIndex index.jsp index.html

        ;

         JkUriSet worker ajp13:192.168.1.111:8009

     
;



;

      LoadModule描述了jk2模块的路径,而虚拟主机则建立了一个使用192.168.1.114的80端口的web服务,此服务通过jk2Connector连接Tomcat(地址为192.168.1.111)的8009端口的方式与Tomcat进行通讯。

3.启动Apache服务:

       执行/usr/local/Apache/bin目录中的apachectl命令:

[root@sms bin]# ./apachectl start

(9Address already in use: make_sock: could not bind to address 0.0.0.0:80

no listening sockets available, shutting down

Unable to open logs

说明启动了系统默认安装的httpd,只要kill掉httpd后再次运行./apachectl start即可。

如果遇到以下错误,请检查httpd.conf中的LoadModule行是否些正确了!

[root@sms bin]# ./apachectl start

Syntax error on line 1053 of /usr/local/apache2/conf/httpd.conf:

Invalid command ‘JkUriSet‘, perhaps mis-spelled or defined by a module not included in the server configuration

七.大结局-访问Apache!

       如果在访问Apache时浏览器出现以下错误的话,请检查Apache和Tomcat的服务是否启动正常,Apache和Tomcat的配置文件是否正确。

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.



Please contact the server administrator, webmaster@demo and inform themof the time the error occurred, and anything you might have done thatmay have caused the error.



More information about this error may be available in the server error log.





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



Apache/2.0.52 (Unix) mod_jk2/2.0.4 Server at 192.168.1.114 Port 80



当然也可以看一下日志,寻找错误原因:

[root@sms build]# vi /usr/local/apache2/logs/error_log

[Thu Jan 20 15:16:56 2005] [error] uriEnv.init() map to invalid worker / ajp13:192.168.1.111:8009

[Thu Jan 20 15:16:56 2005] [error] uriEnv.init() map to invalid worker / ajp13:192.168.1.111:8009

[Thu Jan 20 15:16:56 2005] [error] uriEnv.init() map to invalid worker / ajp13:192.168.1.111:8009

[Thu Jan 20 15:16:56 2005] [error] uriEnv.init() map to invalid worker / ajp13:192.168.1.111:8009

[Thu Jan 20 15:16:56 2005] [error] uriEnv.init() map to invalid worker / ajp13:192.168.1.111:8009

[Thu Jan 20 15:16:56 2005] [notice] Apache/2.0.52 (Unix) mod_jk2/2.0.4 configured -- resuming normal operations

[Thu Jan 20 15:22:47 2005] [error] mod_jk2.handle() No worker for /index.jsp

[Thu Jan 20 15:22:47 2005] [error] mod_jk2.handle() No worker for /favicon.ico

[Thu Jan 20 15:22:51 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:22:51 2005] [error] mod_jk2.handle() No worker for /favicon.ico

[Thu Jan 20 15:22:53 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:22:53 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:22:55 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:22:56 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:23:18 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:23:19 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:23:22 2005] [error] mod_jk2.handle() No worker for /

[Thu Jan 20 15:23:22 2005] [error] mod_jk2.handle() No worker for /favicon.ico

~



花了两天,终于完成了。不干子享,于大家共同分享。