說明:所使用的作業系統為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>
<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
(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
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
fi
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
status)
status -p ${pidfile} $httpd
restart)
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
reload)
reload
graceful|help|configtest|fullstatus)
$apachectl $@
*)
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 / >
</Location>
</VirtualHost>
(4)啟動httpd服務
# service httpd restart
啟動報錯,檢視/usr/local/apache/logs/error_log發現報錯,需要啟動兩個子產品
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)修改從配置檔案,改為以下内容
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>
#沒啥用,可以不定義
ServerName localhost
ProxyVia On
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Proxy *>
Require all granted
</Proxy>
<Location / >
</Location>
通路http://202.207.178.8/,可以通路到後端,并實作負載均衡!
8、提供後端狀态頁面
(1)修改配置檔案,在虛拟主機中添加以下内容
<Location /balancer-manager>
SetHandler balancer-manager
Proxypass !
Require all granted