Apache + Tomcat 集群安装与配置

最近项目需要用到Apache + Tomcat 集群,研究了一下,经过大量测试,可以用于生产环境。(不同版本配置可能有不同之处,其它版本需要自己测试)

所需软件列表:
Apache 2.2.27
Tomcat 6.0.39

安装环境:
RedHat 6.3

一. HTTP Server安装

安装过程
以root用户身份登陆,创建Apache HTTP Server相关账户,文档中安装路径为/usr/local/apache2,以实际路径为准。

1. 进入下载好的httpd-2.2.27.tar.gz文件所在目录,执行解压操作:
tar -zxvf httpd-2.2.27.tar.gz

2. 进入解压出的文件夹根目录:
cd httpd-2.2.27

3. 进行编译参数配置:
./configure --prefix=/usr/local/apache2 --enable-mods-shared=all

4. 编译安装:
make
编译成功后再执行安装
make install

5. 启动和关闭apache
完成上述步骤后,apache的安装就结束了,通过下面的命令启动和关闭apache:
启动:
/usr/local/apache2/bin/apachectl start
关闭:
/usr/local/apache2/bin/apachectl stop


安装、启动过程可能出现的错误及解决方法
1. 安装apache过程中报Cannot use an external APR with the bundled APR-util错误,解决方法是安装apache2.2.x自身携带的apr,方法为:
A. 进入apache2.2.x自带apr的安装目录
cd srclib/apr
B. 安装apr(此处将之安装在/usr/local/apr下,如果系统中已经安装apr,可找到安装目录进行覆盖)
./configure --prefix=/usr/local/apr
执行成功后进行编译
make
最后执行安装
make install
C. 安装apr-util
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
执行成功后进行编译
make
最后执行安装
make install
D. 使用新安装的apr重新进行apache编译配置
返回安装根目录httpd-2.2.27,执行:
./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-dav --enable-mainer-mode --enable-rewrite --with-included-apr
执行成功后进行编译
make
最后执行安装
make install

2. 执行make install命令时出现libtool: install: error: cannot install `libaprutil-1.la' to a directory not ending in /usr/local/apache2/lib
A. 执行make clean后,重新编译安装。
B. 删除source code目录,重新解压apache安装包,进行编译安装。

3. 安装时出现checking whether to enable mod_deflate... configure: error: mod_deflate has been requested but can not be built due to prerequisite failures错误
出现该错误时由于少了zlib包导致的,
centos下可以采用yum install zlib-devel,
ubuntu下可以采用apt-get install zlib1g-dev,
suse下用yast -i zlib-devel命令解决;

4. 启动apache遇到错误:httpd: Could not reliably determine the server's fully qualified domain name
A. 进入apache安装目录下的conf
B. 编辑httpd.conf文件,搜索"#ServerName"
改为:ServerName localhost:8001,保存后退出
C. 重新启动apache即可
./apachectl stop
./apachectl start
启动后可以用命令查看是否正常:
ps - ef|grep httpd
可以看到有多条记录,如xx/xx/bin/httpd -k start

补充:
如果你安装是apache-2.4以上版本,有以下几点不同:
1. 需要单独下载apr和apr-util
apr下载地址:http://mirror.bjtu.edu.cn/apache/apr/apr-1.5.1.tar.gz
apr-util下载地址:http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.3.tar.gz

2. 此外还需要安装pcre
在http://pcre.org/ 下载pcre,解压进入源码目录执行:
./configure --prefix=/usr/local/pcre
注:/usr/local/pcre以实际安装路径为准。

3. 最后安装apache时configure需要这样配置:
./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-so --enable-dav --enable-mainer-mode --enable-rewrite --with-included-apr
再执行编译安装。
make
成功后执行
make install

Apache添加模块
进入apache安装源文件目录下的modules/proxy,依次执行以下命令:
/usr/local/apache2/bin/apxs -c -i -a mod_proxy.c proxy_util.c
/usr/local/apache2/bin/apxs -c -i -a mod_proxy_ajp.c ajp*.c
/usr/local/apache2/bin/apxs -c -i -a mod_proxy_balancer.c proxy_util.c
/usr/local/apache2/bin/apxs -c -i -a mod_proxy_connect.c proxy_util.c
/usr/local/apache2/bin/apxs -c -i -a mod_proxy_ftp.c proxy_util.c
/usr/local/apache2/bin/apxs -c -i -a mod_proxy_http.c proxy_util.c
成功后会在/usr/local/apache2/modules/目录下分别生成mod_proxy.so、mod_proxy_ajp.so、mod_proxy_balancer.so、mod_proxy_connect.so、mod_proxy_ftp.so、mod_proxy_http.so文件,并自动将模块添加到httpd.conf文件中;


二. 开启tomcat集群支持
同样是在conf/server.xml文件里,需要做的改动有:
1. 找到<Engine name="Catalina" defaultHost="localhost">,添加jvmRoute参数,改动后如:<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">,在apache-tomcat-6.0.39-1中为jvm1,则apache-tomcat-6.0.39-2中为jvm2,该名字可以随便起,只要不同tomcat配置为不同且唯一的值即可。

2. 找到被注释掉的<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>,在下面添加以下信息:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="6">

          <Manager className="org.apache.catalina.ha.session.BackupManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"
                   mapSendOptions="6"/>

          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="224.0.0.0"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="192.168.1.100"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>

            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

需要改动的地方:Receiver节点下的address为机器的ip地址,port可根据实际配置(数值范围为4000至4100),如apache-tomcat-6.0.39-1配置为4000,apache-tomcat-6.0.39-2配置为4001,其它地方不需要改变(注:即使tomcat服务器不在同一台机器上,也不要将端口配置成一样的)。

3. 修改项目的web.xml文件
在web.xml的web-app标签内添加<distributable /> 即可。


开启网卡组播功能(windows下不需要该操作)
在上一章节中,Membership用于发现集群中的其他节点,这里的address用的是组播地址, 使用同一个组播地址和端口的多个节点同属一个子集群,因此需要开启网卡的组播功能
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
只需要将eth0改为实际使用的网卡名,在不同机器上可能不一样,可以通过ifconfig查看实际名称,仅需要把eth0改为实际服务器的网卡名即可。
使用route -e命令可以查看是否执行成功,查询后有类似信息:
224.0.0.0    *     240.0.0.0     U     0      0       0     eth0

注:上面的修改是临时的,机器重启后就该配置就无效了,如果需要永久生效,则需作以下处理:
1. 使用root管理员登录后,在/etc/sysconfig目录下创建static-routes文件,文件内容如下:
any net 224.0.0.0 netmask 240.0.0.0 dev eth0
需要将eth0改为实际服务器的网卡名
2. 修改network文件,使用vi命令编辑/etc/init.d/network文件,在文件的末尾exit $rc前面加上以下内容:
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
  grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
      /sbin/route add -$args
  done
fi
保存后使用network restart命令重启网络连接即可。


配置字符集
进入Tomcat下的conf目录,修改server.xml文件,找到AJP的Connector节点,添加URIEncoding="UTF-8"属性,修改后如下:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
注:集群下的所有tomcat都要做对应修改。


配置初始化内存
进入Tomcat下的bin目录,使用vi命令编辑catalina.sh文件,加上
JAVA_OPTS="-server -Xms256m -Xmx512m -XX:PermSize=128m -XX:MaxNewSize=256m -XX:MaxPermSize=512m"
具体大小请根据实际环境调整。
注:集群下的所有tomcat都要做对应修改。



三. Apache HTTP Server配置
配置httpd.conf文件
进入apache安装目录下的conf文件夹,如本文中的
cd /usr/local/apache2/conf/,使用vi命令对httpd.conf进行编辑,
1. 找到Listen 80,其中80为apache的监听端口(即对外的访问端口),需要修改为以下格式:Listen ip地址:端口,如Listen 172.17.234.163:8001

2. 找到<IfModule dir_module>节点,添加index.jsp,如
<IfModule dir_module>
DirectoryIndex index.html index.jsp
</IfModule>

3. 在httpd.conf配置文件的最后加上
ProxyRequests Off
ProxyPreserveHost on
ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=Off timeout=5 maxattempts=3
<Proxy balancer://cluster>
BalancerMember ajp://192.168.1.100:8009 loadfactor=1 route=jvm1
BalancerMember ajp://192.168.1.100:9009 loadfactor=1 route=jvm2
ProxySet lbmethod=bytraffic
</Proxy>
其中BalancerMember节点后面的ip地址为tomcat所在机器的ip址址,端口为tomcat配置文件server.xml中AJP的实际端口,jvm1则为tomcat中所做的配置,实际有多少台tomcat服务器,则添加对应数量的BalancerMember节点信息。


以上配置能实现会话复制、故障转移、负载均衡、同一个会话始终由集群下的某个机器提供(除非机器出现故障、宕机),欢迎大家提出意见、建议,大家共同进步。

猜你喜欢

转载自xiaohong.iteye.com/blog/2109305