说明:所使用的操作系统为Centos6.3,准备三台虚拟机,202.207.178.8(做为前端安装apache,作为代理),node1:202.207.178.6(已经安装了Tomcat,并可以访问到其默认页面,作为后端),node2:202.207.178.7(已经安装了Tomcat,并可以访问到其默认页面,作为后端)
Tomcat相关安装配置详见本人博客:http://10927734.blog.51cto.com/10917734/1874112
一、配置后端Tomcat(两台配置基本相同,为了以示区别,将两者显示可设为不同)
1、编辑配置文件,自定义Host,并检查有无语法错误
# cd /usr/local/tomcat/conf/
# vim server.xml
#将引擎的默认主机改为自己定义的主机
<Engine name="Catalina" defaultHost="www.fsy.com"
jvmRoute="TomcatA">
#自定义一个Host
<Host name="www.fsy.com" appBase="/web"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="webapps" reLoadable="true" />
</Host>
# catalina.sh configtest
2、提供访问页面
# mkdir -pv /web/webapps
# cd /web/webapps/
# vim index.jsp
添加以下内容:
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
3、启动Tomcat服务,即可进行访问测试了
# service tomcat start
http://202.207.178.7:8080/
二、配置前端Apache
1、解决依赖关系
httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。这里使用
源码包进行升级(apr-1.5.2,apr-util-1.5.4 )
(1) 编译安装apr
# tar xf apr-1.5.2.tar.bz2
# cd apr-1.5.2
# ./configure --prefix=/usr/local/apr
# make && make install
(2) 编译安装apr-util
# tar xf apr-util-1.5.4.tar.bz2
# cd apr-util-1.5.4
# ./configure --prefix=/usr/local/apr-util
--with-apr=/usr/local/apr
# make && make install
(3)httpd-2.4.4编译过程也要依赖于pcre-devel软件包,需要事先安装。
#yum -y install pcre-devel
(4)可在编译安装httpd时会报错:checking for OpenSSL version >=0.9.7 ...
#yum -y install openssl-devel
#yum update openssl
2、如果发现以前使用rpm包装过httpd的解决办法(重新安装一次即可)
# rpm -e httpd --nodeps
# rm -rf /etc/httpd/
# make install
3、编译安装httpd-2.4.4
# tar xf httpd-2.4.4.tar.bz2
# cd httpd-2.4.4
#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre
--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
--enable-mpms-shared=all --with-mpm=event --enable-proxy
--enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer
--enable-lbmethod-heartbeat --enable-heartbeat --enable-slotmem-shm
--enable-slotmem-plain --enable-watchdog
# make && make install
4、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
5、而后为此脚本赋予执行权限并加入服务列表,便可启动服务了!:
# chmod +x /etc/rc.d/init.d/httpd
# chkconfig --add httpd
# service httpd start
6、配置apache通过mod_proxy模块与Tomcat连接(代理至202.207.178.7)
要使用mod_proxy与Tomcat实例连接,需要apache已经装载mod_proxy、 mod_proxy_http、mod_proxy_ajp和proxy_balancer_module
(实现Tomcat集群时用到)等模块
(1)查看上述模块是否已经装载:
# /usr/local/apache/bin/httpd -D DUMP_MODULES | grep proxy
proxy_module (shared)
proxy_connect_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_fcgi_module (shared)
proxy_scgi_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_express_module (shared)
(2)修改主配置文件
# vim /etc/httpd/httpd.conf
注释中心主机:
#DocumentRoot "/usr/local/apache/htdocs"
添加虚拟主机相关的内容:
Include /etc/httpd/extra/httpd-proxy.conf
添加pid文件:
PidFile "/var/run/httpd.pid"
(3)添加从配置文件:
# vim /etc/httpd/extra/httpd-proxy.conf
<VirtualHost *:80>
ProxyVia On
ProxyRequests Off
ProxyPass / http://202.207.178.7:8080/
ProxyPassReverse / http://202.207.178.7:8080/
<Proxy *>
Require all granted
</Proxy>
<Location / >
Require all granted
</Location>
</VirtualHost>
(4)启动httpd服务
# service httpd restart
启动报错,查看/usr/local/apache/logs/error_log发现报错,需要启动两个模块
# vim /etc/httpd/httpd.conf
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule socache_shmcb_modulemodules/mod_socache_shmcb.so
访问http://202.207.178.8/,可以访问到后端!
7、配置apache通过mod_proxy模块与Tomcat连接,实现负载均衡
(1)修改从配置文件,改为以下内容
# vim /etc/httpd/extra/httpd-proxy.conf
ProxyRequests Off
<proxy balancer://lbcluster1>
BalancerMember ajp://202.207.178.6:8009 loadfactor=10 route=TomcatA
BalancerMember ajp://202.207.178.7:8009 loadfactor=10 route=TomcatB
ProxySet lbmethod=byrequests
</proxy>
<VirtualHost *:80>
#没啥用,可以不定义
ServerName localhost
ProxyVia On
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Proxy *>
Require all granted
</Proxy>
<Location / >
Require all granted
</Location>
</VirtualHost>
访问http://202.207.178.8/,可以访问到后端,并实现负载均衡!
8、提供后端状态页面
(1)修改配置文件,在虚拟主机中添加以下内容
# vim /etc/httpd/extra/httpd-proxy.conf
<Location /balancer-manager>
SetHandler balancer-manager
Proxypass !
Require all granted
</Location>
(2)重启服务,即可访问测试了
http://202.207.178.8/balancer-manager,可以访问到一个状态页面
本文出自 “10917734” 博客,请务必保留此出处http://10927734.blog.51cto.com/10917734/1886752
原文地址:http://10927734.blog.51cto.com/10917734/1886752