一、环境说明
在一台服务器上安装一个apache作为web server,多个tomcat作为应用的容器,此时我们便可以使用apache来作为一个调度员将用户发来的请求发送到不同的tomcat。在此我使用192.168.0.3作为apache和tomcat的服务器,192.168.0.4作为另一台tomcat应用服务器。
拓扑如下:
二、原理
tomcat 为一个jsp的容器,apache为一个web server,两者之间通信通过worker进行(由Tomcat使用Server.xml文件中Connector的标签来定义其端口和协议),通过 mod_jk的模块(由web服务器像apache、iis等使用)和Web Server通信。整个过程其实就是让apache的httpd.conf文件调用mod_jk.conf,mod_jk.conf调用workers.properties,最后配置虚拟主机。
文件说明
mod_jk.conf
主要定义mod_jk模块的位置以及mod_jk模块的连接日志设置,还有定义worker.properties文件的位置。
worker.properties
定义worker的参数,主要是连接tomcat主机的地址和端口信息。如果Tomcat与apache不在同一台机器上,或者需要做多台机器上tomcat的负载均衡只需要更改workers.properties文件中的相应定义即可。
三、 安装软件
1.先安装了所需的程序库
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel
2.Apache 安装:
tar zxvf httpd-2.2.8.tar.gz
cd httpd-2.2.8
./configure --prefix=/usr/local/apache2 --enable-modules=so --enable-so
make && make install
3.JDK的安装
chmod 755 jdk-6u26-linux-i586-rpm.bin
./jdk-6u26-linux-i586-rpm.bin
mv /usr/java/jdk1.6.0_26 /usr/local/
到/usr/bin目录下,把原用的java,javac文件删除:
rm -rf /usr/bin/{java,javac}
在/usr/bin 下建立 软连接 java
ln -s /usr/local/jdk1.6.0_26/bin/java /usr/bin/java
ln -s /usr/local/jdk1.6.0_26/bin/javac /usr/bin/javac
javac -version
为了方便下在的工作建立两个软链接:
ln -s /usr/local/jdk1.6.0_26/ /usr/local/jdk
ln -s /usr/local/jdk1.6.0_26/jre /usr/local/jre
4.Tomcat的安装
tar -zxvf apache-tomcat-7.0.16.tar.gz
mv apache-tomcat-7.0.16 /usr/local/tomcat
5.设置环境变量:
vi .bash_profile
JAVA_HOME=/usr/local/jdk
export JAVA_HOME
JRE_HOME=/usr/local/jre
export JRE_HOME
CLASSPATH=/usr=/usr/local/tomcat/lib/:/usr/local/jdk/lib:/usr/local/jre/lib
export CLASSPATH
PATH=$PATH:/usr/local/tomcat/bin/:/usr/local/apache/bin:/usr/local/jdk/bin:/usr/local/jre/bin
export PATH
TOMCAT_HOME=/usr/local/tomcat
export TOMCAT_HOME
source .bash_profile //使环境变量立即生效
6.JK 安装 (整合apache tomcat)
tar zxvf tomcat-connectors-1.2.27-src.tar.gz
cd tomcat-connectors-1.2.27-src/native
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-java-home=/usr/local/jdk
sleep 3
make && make install
在/usr/local/apache2/modules/ 下会产生mod_jk.so
(同样在192.168.0.4上也要安装jdk和tomcat)
四、 配置Apache
1.修改apache配置文件
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.baiying21.com
JkMountFile conf/mount_baiying21.properties
</VirtualHost>
<VirtualHost *:80>
ServerName www.baiying23.com
JkMountFile conf/mount_baiying23.properties
</VirtualHost>
include conf/mod_jk.conf
2.调度器JK的配置
建立JK配置文件:
vi /usr/local/apache2/conf/workers.properties
worker.list=jvm_baiying21,jvm_baiying23
#========jvm_baiying21========
worker.jvm_baiying21.type=ajp13
worker.jvm_baiying21.port=8009 //第6步会配置此端口
worker.jvm_baiying21.host=localhost
worker.jvm_baiying21.lbfactor=1
#========jvm_baiying23========
worker.jvm_baiying23.type=ajp13
worker.jvm_baiying23.port=8010
worker.jvm_baiying23.host=192.168.0.4
worker.jvm_baiying23.lbfactor=1
3.vi /usr/local/apache2/conf/mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkShmFile logs/mod_jk.shm
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
#JkMount /* controller
4.vi /usr/local/apache2/conf/mount_baiying21.properties
/*=jvm_baiying21
5.vi /usr/local/apache2/conf/mount_baiying23.properties
/*=jvm_baiying23
6.修改baiying21的tomcat调用端口
vi /usr/local/tomcat/conf/server.xml
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> //我们使用默认端口8009
修改baiying23的tomcat调用端口
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
五、 创建jsp测试页面
在baiying21的tomcat安装路径下建立目录test
mkdir /usr/local/tomcat/webapps/ROOT/test/
vi index.jsp
<html>
<title>
baiying21 jsp
</title>
<%
String showMessage="Oh My God baiying21!";
out.print(showMessage);
%>
</html>
----------------------------
在baiying23的tomcat安装路径下建立目录test
mkdir /usr/local/tomcat/webapps/ROOT/test/
vi index.jsp
<html>
<title>
baiying23 jsp
</title>
<%
String showMessage="Oh My God baiying23!";
out.print(showMessage);
%>
</html>
修改客户端的hosts文件
192.168.0.3 www.baiying21.com
192.168.0.3 www.baiying23.com
在客户端浏览器分别输入www.baiying21.com/test/index.jsp、www.baiying23.com/test/index.jsp验证apache是否调度成功。
错误总结:
在未正确配置tomcat调用端口时,日志报错如下错误信息,因为之前我把调度器JK的配置中的tomcat调用端口配置成了8080,而tomcat默认的ajp借口为8009
# tail -f access_log
192.168.0.1 - - [24/Aug/2011:14:14:15 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.0.1 - - [24/Aug/2011:15:08:20 +0800] "GET / HTTP/1.1" 503 323
192.168.0.1 - - [24/Aug/2011:15:16:23 +0800] "GET /test/index.jsp HTTP/1.1" 502 232
192.168.0.1 - - [24/Aug/2011:15:17:42 +0800] "GET / HTTP/1.1" 502 232
192.168.0.1 - - [24/Aug/2011:15:17:52 +0800] "GET / HTTP/1.1" 502 232
# tail -f mod_jk.log
[Wed Aug 24 15:53:52 2011] [25050:3086759680] [info] jk_handler::mod_jk.c (2678): Service error=0 for worker=jvm_chanjet21
[Wed Aug 24 15:54:01 2011] [25050:3086759680] [error] ajp_connection_tcp_get_message::jk_ajp_common.c (1296): wrong message format 0x4854 from 127.0.0.1:8080
[Wed Aug 24 15:54:01 2011] [25050:3086759680] [error] ajp_get_reply::jk_ajp_common.c (2118): (jvm_chanjet21) Tomcat is down or refused connection. No response has been sent to the client (yet)
[Wed Aug 24 15:54:01 2011] [25050:3086759680] [info] ajp_service::jk_ajp_common.c (2607): (jvm_chanjet21) sending request to tomcat failed (recoverable), because of protocol error (attempt=1)
还可以通过netstat -anp | grep java查看java使用的jk配置中端口信息