用apache整合tomcat可以提高对静态文件的处理性能,还可以利用web服务来做负载均衡以及容错,以及无缝的升级应用程序等。
实验过程如下:
tomcat1: 192.168.186.166 tomcat2: 192.168.186.165 apache: 192.168.186.164
配置tomcat 【192.168.186.166/192.168.186.165】 配置Java环境 # tar xf jdk-7u67-linux-x64.tar.gz # mv jdk1.7.0_67/ /usr/local/java # vim /etc/profile.d/java.sh export JAVA_HOME=/usr/local/java export PATH=$JAVA_HOME/bin:$PATH # source /etc/profile.d/java.sh # java -version java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b01) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode) 【出现以上内容则表明Java环境配置成功】 # mkdir /usr/local/tomcat # tar xf apache-tomcat-7.0.73.tar.gz -C /usr/local/tomcat [tomcat是基于Java开发的,解压即用] 【制作网页,便于观察实验结果】 echo "tomcat1" > /usr/local/tomcat/webapps/ROOT/index.jsp [192.168.186.165] echo "tomcat2" > /usr/local/tomcat/webapps/ROOT/index.jsp [192.168.186.166]2.安装apache【192.168.186.164】
# yum install httpd httpd-devel -y3.安装tomcat-connectors 【192.168.186.164】
# tar xf tomcat-connectors-1.2.42-src.tar.gz # cd tomcat-connectors-1.2.42-src/native/ # ./configure --with-apxs # make && make install4.配置tomcat-connectors 【192.168.186.164】
# cd tomcat-connectors-1.2.42-src/conf # cp httpd-jk.conf /etc/httpd/conf.d/ # cp uriworkermap.properties workers.properties /etc/httpd/conf # vim /etc/httpd/conf.d/httpd-jk.conf JkWorkersFile conf/workers.properties JkMountFile conf/uriworkermap.properties # vim /etc/httpd/conf/uriworkermap.properties /*.jsp=balancer #加上 # vim /etc/httpd/conf/workers.properties worker.list=balancer worker.balancer.type=lb worker.balancer.error_escalation_time=0 worker.balancer.max_reply_timeouts=10 worker.balancer.balance_workers=node1 worker.node1.reference=worker.template worker.node1.host=192.168.186.165 worker.node1.port=8009 worker.node1.lbfactor=1 worker.node1.activation=A worker.node2.reference=worker.template worker.node2.host=192.168.186.166 worker.node2.port=8009 worker.node2.lbfactor=1 worker.node2.activation=A # systemctl restart httpd 【重启服务即可】5.用浏览器访问httpd的IP http://192.168.186.164/index.jsp ,多次刷新会交替出现 “tomcat1” “tomcat2” 则表明实验成功
================================================================== 注意:如果httpd服务不是yum安装的,部署过程略有不同。 首先将三个文件移到/usr/local/httpd/conf/ 在httpd的配置文件中写入一句:
include /usr/local/httpd/conf/httpd-jk.conf(如果不写绝对路径就默认在 /usr/local/httpd里面找)
注意要修改httpd-jk.conf里面的文件路径,要表明另外两个文件的地方
# vim /etc/httpd/conf.d/httpd-jk.conf JkWorkersFile /usr/local/httpd/conf/workers.properties JkMountFile /usr/local/httpd/conf/uriworkermap.properties这样配置好,启动httpd可能会报错:
httpd: Syntax error on line 506 of /usr/local/httpd/conf/httpd.conf: Syntax error on line 22 of /usr/local/httpd/conf/httpd-jk.conf: Cannot load modules/mod_jk.so into server: /usr/local/httpd/modules/mod_jk.so: cannot open shared object file: No such file or directory
原因是加载httpd-jk.conf时,找不到mod_jk.so模块,此时可以用find去找一下这个模块的在哪,一般httpd编译之后都会有,就可以进入httpd-jk.conf,替换掉modules/mod_jk.so的相对路径,就可以解决这个问题。