一、安装apache
a、下载最新源文件httpd-2.2.3.tar.gz。下载地址:http://apache.justdn.org/httpd/httpd-2.2.3.tar.gz
b、编译 apache
1、gzip -d httpd-2.2.3.tar.gz
2、tar xvf httpd-2.2.3.tar
3、./configure --prefix=/export/home/apache2 --enable-module=most --enable-shared=max --enable-so
4、make
5、make install
c、简单配置:编辑httpd.conf,
把#ServerName www.example.com:80项中ServerName前的#号去掉,把xxxxx改成自己的机器全名,212中修改如下
#ServerName www.example.com:80改成ServerName vcmedu:80
把#User daemon改成User tomcat
把#Group daemon改成Group other
如果漏了改它,则apache是没法启动的。
d、启动Apache: #cd /export/home/apache2/bin
#./apachectl start
e、测试Apache是否已经正确运行:http://192.168.1.212/
二、安装tomcat
a、把tomcat_vcmplat目录整个的拷贝到/export/home/tomcat目录下。
chown -fR tomcat:other /export/home/tomcat/tomcat_vcmplat
b、把tomcat5_discovery目录下的bin目录的权限修改为所有者可以具有任何权限。具体命令:
chmod -R 744 bin/
c、配置java home
1、在etc/profile文件里插入以下两行:
JAVA_HOME=/usr/jdk/jdk1.5.0_06 # JAVA_HOME要设置为jdk或jre真正的安装路径
export JAVA_HOME
2、执行reboot命令重启linux系统
第二种配置java home的方法。在catalina.sh文件里添加java home信息。具体语句:JAVA_HOME=/usr/java/jdk
d、修改/tomcat5_discovery/webapps目录以及其下的所有子目录和文件的权限。具体命令:
chmod -R 755 webapps/
e、建立各个tomcat的对立目录
cp /export/home/tomcat/tomcat_vcmplat /export/home/tomcat/tomcat1
rm -fR /export/home/tomcat/tomcat1/webapps
chown tomcat:other /export/home/tomcat/tomcat1
修改tomcat的文件目录,将/export/home/tomcat/tomcat1/conf/server.xml
appBase="webapps"修改为
appBase="/export/home/tomcat/tomcat_vcmplat/webapps"
cp -fR /export/home/tomcat/tomcat1 /export/home/tomcat/tomcat2
修改各个tomcat的端口
tomcat1/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN" debug="0">改为
<Server port="11001" shutdown="SHUTDOWN" debug="0">
<Connector port="80"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="GBK"/>改为
<Connector port="11002"
......
<Connector port="8009"
enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />改为
<Connector port="11003"
......
tomcat2/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN" debug="0">改为
<Server port="12001" shutdown="SHUTDOWN" debug="0">
<Connector port="80"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="GBK"/>改为
<Connector port="12002"
......
<Connector port="8009"
enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />改为
<Connector port="12003"
......
f、启动tomcat:
#cd /export/home/tomcat/tomcat1/bin
#./startup.sh
#cd /export/home/tomcat/tomcat2/bin
#./startup.sh
g、测试tomcat是否已经正确运行:
http://192.168.1.212:11002
http://192.168.1.212:12002
三、apache + 2tomcat的负载均衡
a、下载mod_jk-1.2.19-apache-2.2.3-solaris-sparc.so,并存放到/export/home/apache2/modules/目录下。
地址:http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.19/mod_jk-apache-2.0.49-linux-i686.so
b、修改httpd.conf配置
编辑Apache的配置文件Apache安装目录的conf/httpd.conf
把DocumentRoot "/export/home/apache2/htdocs"改成
DocumentRoot "/export/home/tomcat/tomcat_vcmplat/webapps/ROOT"
把
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
改成
<IfModule dir_module>
DirectoryIndex index.htm
</IfModule>
把以下项中的Deny from all改成Allow from all,允许目录被访问,即
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
改成
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
加入一行
include conf/mod_jk.conf
d、在conf目录下创建mod_jk.conf文件,内容如下:
--------------------------------------mod_jk.conf------------------------------------------
----
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf/workers.properties
# Where to put jk logs
JkLogFile logs/mod_jk.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"
JkAutoAlias /export/home/tomcat/tomcat_vcmplat/webapps
# Send JSPs for context / to worker named loadBalancer
#JkMount /*.jsp loadBalancer
#JkMount /*.do* loadBalancer
JkMountFile conf/urimap.properties
--------------------------------------------mod_jk.conf------------------------------------
在conf目录下创建urimap.properties文件,内容如下:
/*=loadBalancer
!/*.gif=loadBalancer
!/*.jpg=loadBalancer
!/*.png=loadBalancer
!/*.css=loadBalancer
!/*.js=loadBalancer
!/*.htm=loadBalancer
!/*.html=loadBalancer
!/*.mid=loadBalancer
!/*.xml=loadBalancer
!/*.txt=loadBalancer
!/*.mp3=loadBalancer
mv /export/home/apache2/modules/mod_jk-1.2.19-apache-2.2.3-solaris-sparc.so /export/home/apache2/modules/mod_jk.so
e、在 conf目录下创建workers.properties文件,该文件将告诉apache 每个负载均衡的tomcat
服务的运行参数,如机器名和端口号等。
-----------------------------------------workers.properties-------------------------------
#
# workers.properties
#
# list the workers by name
worker.list=loadBalancer
# ------------------------
# First tomcat server
# ------------------------
worker.tomcat1.port=11003
worker.tomcat1.host=192.168.1.212
worker.tomcat1.type=ajp13
# 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.
worker.tomcat1.lbfactor=10000
# ------------------------
# Second tomcat server
# ------------------------
worker.tomcat2.port=12003
worker.tomcat2.host=192.168.1.212
worker.tomcat2.type=ajp13
# 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=10000
# ------------------------
# 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.
worker.loadBalancer.type=lb
worker.loadBalancer.balance_workers=tomcat1, tomcat2
#
# END workers.properties
#
-----------------------------------------workers.properties-----------------------------
f、配置Tomcat 服务器
修改conf/server.xml文件:
在tomcat1目录的conf/server.xml,在大约第100行, 替换:
<Engine name="Catalina" defaultHost="localhost" debug="0">为:
<Engine jvmRoute="tomcat1" name="Catalina" defaultHost="localhost" debug="0">
在tomcat2目录的conf/server.xml,替换为 jvmRoute="tomcat2".
<Engine name="Catalina" defaultHost="localhost" debug="0">为:
<Engine jvmRoute="tomcat2" name="Catalina" defaultHost="localhost" debug="0">
注意,一定是替换这一行,而包含在原来的engine里面的host不做更改。有的文档写的是在配置中加入一行
<Engine jvmRoute="tomcat1" name="Catalina" defaultHost="localhost" debug="0"><Engine>,其实是不对的,这样负载均衡是没有问题的,但是不能做session绑定了,也就是说同一用户会在两台服务器上跳来跳去。这是由于jvmRoute不正确引起的,所以一定要替换。而且这个jvmRoute一定要跟workers.properties中的名称完全匹配,否则也不能实现session绑定.
OK!!!这样设置后,这实现了apache+2tomcat的负载均衡了。
g、配置session亲和、监测各个tomcat的状态
vi /export/home/apache2/conf/workers.properties
加入
worker.loadBalancer.sticky_session=1
修改
worker.list=loadBalancer为
worker.list=loadBalancer, status
加入
worker.status.type=status
vi /export/home/apache2/conf/mod_jk.conf
加入
JkMount /jkstatus status
重启apache,可在http://192.168.1.212/jkstatus监测各个tomcat的情况
(status实际上mod_jk自带的一个应用,目的是观察每个tomcat的状态,并可以在运行时设置每个tomcat的某些参数)
如需用户/密码验证才能监测状态,配置如下
/export/home/apache2/bin/htpasswd -c /export/home/apache2/conf/jk_passwd vcmedu
vi /export/home/apache2/conf/mod_jk.conf
去掉以下项
JkMount /jkstatus status
加如下面内容
<Location /jkstatus>
JkMount status
Order deny,allow
AuthType Basic
AuthName "jkstatus"
AuthUserFile conf/jk_passwd
require valid-user
</Location>
写这个东东的所有的参考文章和参考数字及各种数据均来自互联网,
我谨代表我个人对免费提供这些材料的作者表示由衷的感谢。
欢迎转发本文章,但是希望您能保持文章的完整性.
如有问题,请联系:
Email:
[email protected]
QQ:29413069