Linux下apache tomcat ajp_proxy 负载均衡+集群+session 复制

apache-tomcat-7.0.6.tar.gz

httpd-2.2.17.tar.gz

jdk-6u23-linux-i586.bin

1个apache  2个tomcat在一台机器上,修改其中一个tomcat相关端口,对应如下

8080     8980

8005     8905

8009     8909

4000     4001

1.安装httpd

tar zxvf httpd-2.2.17.tar.gz
cd httpd-2.2.17
./configure --prefix=/usr/local/apache2 --enable-so
make && make install

2.安装JDK

chmod +x jdk-6u18-linux-i586.bin
./jdk-6u18-linux-i586.bin
mv jdk1.6.0_18 /usr/local/jdk
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
ln -s /usr/local/jdk/bin/java /usr/bin/java
ln -s /usr/local/jdk/bin/javac /usr/bin/javac

[root@CentOSlamp src]# java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)

ln -s /usr/local/jdk/jre /usr/local/jre

设置环境变量

vi /etc/profile 添加如下内容

JAVA_HOME=/usr/local/jdk
export JAVA_HOME
JRE_HOME=/usr/local/jre
export JRE_HOME

运行,使环境变量生效

source /etc/profile

3.安装tomcat

tar zxvf apache-tomcat-7.0.6.tar.gz
mv apache-tomcat-7.0.6 /usr/local/tomcat1

tar zxvf apache-tomcat-7.0.6.tar.gz
mv apache-tomcat-7.0.6 /usr/local/tomcat2

4.安装mod_proxy.so和mod_proxy_ajp.so,mod_proxy_balancer.so模块

cd /usr/local/src/httpd-2.2.17/modules/proxy

/usr/local/apache2/bin/apxs -i -a -c mod_proxy.c proxy_util.c

/usr/local/apache2/bin/apxs -i -a -c mod_proxy_ajp.c ajp*.c

/usr/local/apache2/bin/apxs -i -a -c mod_proxy_balancer.c

5. 配置httpd.conf 文件

vi /usr/local/apache2/conf/httpd.conf

ProxyPass /images !
ProxyPass /css !
ProxyPass /js !

ProxyRequests Off
ProxyPass /examples balancer://mycluster/ stickysession=JSESSIONID

ProxyPassReverse /examples balancer://mycluster/ stickysession=JSESSIONID
<Proxy balancer://mycluster>
     BalancerMember ajp://192.168.15.124:8009/examples/ route=worker1 loadfactor=1
     BalancerMember ajp://192.168.15.124:8909/examples/ route=worker2 loadfactor=1
</Proxy>
SetHandler balancer-manager

6.编辑server.xml文件

修改相关对应端口,如果在不同的机器上则不需要修改

<Connector port="8080" protocol="HTTP/1.1"
             connectionTimeout="20000"
             redirectPort="8443" />
  <!-- A "Connector" using the shared thread pool-->
  <!--
  <Connector executor="tomcatThreadPool"
             port="8080" protocol="HTTP/1.1"
             connectionTimeout="20000"
             redirectPort="8443" />

<Server port="8005" shutdown="SHUTDOWN">

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

7.复制tomcat下webapps下的文件到/usr/local/apache2/htdocs

cp –R  /usr/local/tomcat1/webapps/* /usr/local/apache2/htdocs

8.启动tomcat 和httpd

/usr/local/tomcat1/bin/startup.sh

/usr/local/tomcat2/bin/startup.sh

/usr/local/apache2/bin/apachectl start

到这里负载均衡完成

配置集群

相关推荐