說明:所使用的操做系統爲Centos6.3,準備三臺虛擬機,202.207.178.8(作爲前端安裝apache,做爲代理),node1:202.207.178.6(已經安裝了Tomcat,並能夠訪問到其默認頁面,做爲後端),node2:202.207.178.7(已經安裝了Tomcat,並能夠訪問到其默認頁面,做爲後端)html
Tomcat相關安裝配置詳見本人博客:http://10927734.blog.51cto.com/10917734/1874112前端
1、配置後端Tomcat(兩臺配置基本相同,爲了以示區別,將二者顯示可設爲不一樣)java
一、編輯配置文件,自定義Host,並檢查有無語法錯誤node
# cd /usr/local/tomcat/conf/web
# vim server.xmlexpress
#將引擎的默認主機改成本身定義的主機apache
<Engine name="Catalina" defaultHost="www.fsy.com" vim
jvmRoute="TomcatA">後端
#自定義一個Hosttomcat
<Host name="www.fsy.com" appBase="/web"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="webapps" reLoadable="true" />
</Host>
# catalina.sh configtest
二、提供訪問頁面
# 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>
三、啓動Tomcat服務,便可進行訪問測試了
# service tomcat start
http://202.207.178.7:8080/
2、配置前端Apache
一、解決依賴關係
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
二、若是發現之前使用rpm包裝過httpd的解決辦法(從新安裝一次便可)
# rpm -e httpd --nodeps
# rm -rf /etc/httpd/
# make install
三、編譯安裝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
四、提供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
五、然後爲此腳本賦予執行權限並加入服務列表,即可啓動服務了!:
# chmod +x /etc/rc.d/init.d/httpd
# chkconfig --add httpd
# service httpd start
六、配置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/
ProxyPa***everse / 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/,能夠訪問到後端!
七、配置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/
ProxyPa***everse / balancer://lbcluster1/
<Proxy *>
Require all granted
</Proxy>
<Location / >
Require all granted
</Location>
</VirtualHost>
訪問http://202.207.178.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,能夠訪問到一個狀態頁面