Apache :apache_2.0.55 1 个
Tomcat: apache-tomcat-6.0(zip版) 2个
mod_jk:: mod_jk-apache-2.0.55.so 1个
第一部分:安装 配置apache tomcat
1.安装apche,tomcat
http://httpd.apache.org/ 下载Apache 2.0.55
http://tomcat.apache.org/download-55.cgi 下载tomcat 6.0 zip(tar.gz 为linux用,win下用zip的,解压即可)
http://apache.justdn.org/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.15/ 下载mod_jk,注意和 apache版本匹配
按照jdk,我的路径为:E:\ide\apache\Apache2
解压两份Tomcat, 路径分别为 D:\tomCluser\tomcat-6.120,D:\tomCluser\tomcat-6.2.20
下载mod_jk
apache下载完,安装即可,安装前注意把应用到80端口的程序先关闭,因为apache默认会占用80端口。
解压版tomcat的配置:
1.jdk路径的配置
JAVA_HOME C:\Program Files\Java\jdk1.5.0_06
PATH C:\Program Files\Java\jdk1.5.0_06\bin
CLASSPATH C:\Program Files\Java\jdk1.5.0_06\lib\tools.jar;C:\Program Files\Java\jdk1.5.0_06\lib\dt.jar
2.tomcat的配置
打开解压后的tomcat 在bin文件夹下编辑startup.bat ,在最上面加如下2行
set CATALINA_HOME="D:\tomCluser\tomcat-6.1.20" //为每个tomcat设置自己的启动目录
SET CATALINA_OPTS=-Xms512m -Xmx1024m -XX:PermSize=128M -XX:MaxPermSize=256m //内存配置
红色部分要换成自己解压后tomcat的路径。
接下来打开tomcat的conf文件夹下的server.xml (分别打开tomcat1 tomcat2)
tomcat1,tomcat2:
-------------------------
---------------------------------------------
----------------------------------------------
--------------------------------------------多项目单独域名设置 以2个为例
打开C:\WINDOWS\system32\drivers\etc 下的hosts文件 添加如下2行
192.168.18.26 www.y1.com 其中192.168.18.26 换成你本机的IP,这样做是使apache能够识别
192.168.18.26 www.y2.com www.y1.com和y2.com
保存,关闭。打开tomcat1 tomcat2的 server.xml 在下面添加如下2行
appBase 是你项目所在的目录,docBase 是你项目的名称。保存,这样tomcat 就配置完成了
分别启动2个tomcat的startup.bat ,如果都能启动成功。说明你tomcat的配置正确。
2.配置apache 实现负载均衡和多域名虚拟主机
2.1 负载均衡,先把下载的mod_jk-apache-2.0.55.so 修改名称为mod_jk2.so 拷贝到apche的modules目录下
1.在apache安装目录下conf目录下 新建mod_jk2.conf 和 workers.properties
mod_jk2.conf 如下设置
# Load mod_jk2 module
LoadModule jk_module modules/mod_jk2.so
# Where to find workers.properties
JkWorkersFile conf/workers.properties
# Where to put jk logs
JkLogFile logs/mod_jk2.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send JSPs for context / to worker named loadBalancer
JkMount /*.jsp loadBalancer //负责jsp转发给tomcat处理 下面以此类推
JkMount /*.do loadBalancer
JkMount /servlet/* loadBalancer
#for dwr
JkMount /*/dwr/* loadBalancer
--------------------------------
workers.properties 如此设置:
# workers.properties
#
# 定义负载均衡的所有主机名,和前面tomcat配置文件的JVMroute属性相对应
# 其中的loadbalancer是虚拟的主机,负责负载均衡,姑且当成是apache本身
# list the workers by name
worker.list=tomcat1,tomcat2,loadBalancer
# ------------------------
# First tomcat server
# tomcat1使用ajp与JK通讯
# tomcat1的地址为localhost,如果tomcat在其他服务器则输入该服务器的地址
# ajp端口
# ------------------------
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009 //对应tomcat1的8009
# Specify the size of the open connection cache.
#worker.tomcat1.cachesize
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
# 负载因子,值越大则JK分发到此tomcat的负荷越多
worker.tomcat1.lbfactor=50
# 若本tomcat因故障不能接受请求,则转移到tomcat2
worker.tomcat1.redirect=tomcat2
# ------------------------
# Second tomcat server
# ------------------------
worker.tomcat2.type=ajp13
worker.tomcat2.host=localhost
worker.tomcat2.port=8010 //对应tomcat2的8010 由809修改的 参看上面tomcat2 server.xml配置
# Specify the size of the open connection cache.
#worker.tomcat2.cachesize
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat2.lbfactor=50
worker.tomcat2.redirect=tomcat1
# 如果有更多tomcatN 以此类推配置
# ------------------------
# Load Balancer worker
# ------------------------
#
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
# once in a while. Until then all work is redirected to peer
# worker.
# 定义loadbalancer类型为“负载均衡器(lb)”
worker.loadBalancer.type=lb
# 定义loadbalancer 进行负载均衡的对象
worker.loadBalancer.balanced_workers=tomcat1,tomcat2
#
# END workers.properties
#
3.打开apache的 httpd.conf 文件
在文件最后加上下面一句话就可以了
include "E:\ide\apache\Apache2\conf\mod_jk2.conf" 修改为你新建的mod_jk2.conf的地址
到这里 apache + tomcat 的负载均衡就配置完毕了
apache 根据负载因子将请求发送给对应的tomcat处理,自己负责处理静态文件