操作系统:CentOS-6.0-i386-bin-DVD
内核版本:2.6.32
[root@localhost ~]# cat /proc/version
Linux version 2.6.32-71.el6.i686
gcc版本:4.4.4
g++版本:4.4.7
java版本:1.6.0_45
gcc、g++和java是必须的,执行命令:
[root@localhost ~]# gcc --version gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13) Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@localhost ~]# g++ --version -bash: g++: command not found [root@localhost ~]# java -verion -bash: java: command not found
1、需要安装g++和Java环境
[root@localhost ~]#yum install gcc-c++ [root@localhost ~]# g++ --version g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
安装Java环境:
chmod 777 jdk-6u45-linux-i586.bin ./jdk-6u45-linux-i586.bin
修改环境变量 #vi /etc/profile,
在文件最后面添加下面一段内容:
JAVA_HOME=/usr/local/jdk1.6.0_45 PATH=$JAVA_HOME/bin:$PATH CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME PATH CLASSPATH
保存
#source /etc/profile,使文件立即生效
检查jdk是否安装成功:
[root@localhost ~]# java -version java version "1.6.0_45" Java(TM) SE Runtime Environment (build 1.6.0_45-b06) Java HotSpot(TM) Client VM (build 20.45-b01, mixed mode, sharing)
2、接下来安装Apache
[root@localhost software]# tar -zxvf httpd-2.2.8_apache.tar.gz [root@localhost software]# cd httpd-2.2.8 [root@localhost httpd-2.2.8]# ./configure --prefix=/usr/local/apache --enable-mods-shared=most --with-mpm=worker [root@localhost httpd-2.2.8]# make [root@localhost httpd-2.2.8]# make install
查看工作模式:
[root@localhost bin]# pwd /usr/local/apache/bin [root@localhost bin]# ./httpd -l Compiled in modules: core.c worker.c http_core.c mod_so.c
浏览器访问虚拟机IP:http://192.168.88.130/ 若出现it works则成功
(注意:虚拟机的防火墙要关闭状service iptables stop(暂时的关闭,下次重启还是会开启);永久性关闭是chkconfig iptables off,但还是要从下次虚机开启时才生效)。
3、安装tomcat
由于要在本地开启两个tomcat服务器实例以模拟负载均衡+群集的效果,因此需要将之前解压出来的tomcat复制成两份,进入解压时的目录,重命名解压出来的原始目录为worker2,然后复制此目录,副本目录名称为worker3:
[root@localhost local]# mv apache-tomcat-7.0.41/ worker2 [root@localhost local]# cp worker2/ -r worker3
现在测试tomcat能否正常工作:
将测试文件TestProject.war放到的webapps目录下。然后切换到worker2的bin目录下,启动worker2:
[root@localhost bin]# ./startup.sh
在浏览器中访问 http://192.168.88.130:8080/TestProject/showInfo.do,若出现如下信息则配置正确:
This message is from Server, RealPath: /usr/local/worker2/webapps/TestProject/ Current Session Id: A480B9239CFC9DA0EC6FC18FED6FCB1B
可以正常工作,先关闭worker2.
4、编译配置jk链接器
下载mod_jk-1.2.23-apache-2.2.x-linux-i686.so (注意:一定要跟自己的系统版本吻合,否则会出错)
改名为mod_jk.so,赋予777权限,拷贝到/usr/local/apache/modules下
# cp mod_jk.so /usr/local/apache/modules
在Apache的httpd.conf里增加如下内容:
include "/usr/local/apache/conf/mod_jk.conf"
在conf路径下新建mod_jk.conf文件,并添加如下内容
#加载mod_jk Module LoadModule jk_module modules/mod_jk.so #指定 workers.properties文件路径 JkWorkersFile conf/workers.properties #指定哪些请求交给tomcat处理,"loadbalancer"为在workers.propertise里指定的负载分配控制器名 JkLogFile logs/mod_jk.log #JkLogLevel debug JkMount /*.do loadbalancer JkMount /*.jsp loadbalancer
在conf下新建workers.properties文件,并添加如下内容
worker.list=loadbalancer #此处与mod_jk.conf中保持一致 worker.worker2.port=8009 #tomcat2中 ajp13 端口号,对应tomcat配置文件server.xml中Connector port="8009",默认8009 worker.worker2.host=127.0.0.1 #worker2的地址,本机loaclhost或者127.0.0.1,远程直接写IP worker.worker2.type=ajp13 #与worker2中的server.xml的protocol保持一致 worker.worker2.lbfactor=1 #负载的权重值,越高表示负载越大 worker.worker3.port=9009 worker.worker3.host=127.0.0.1 worker.worker3.type=ajp13 worker.worker3.lbfactor=1 worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=worker2,worker3 #指定负载的worker列表,用逗号分隔 worker.loadbalancer..sticky_session=false #配置session会话是否为粘性 #这样负载均衡器lb就会尽量保持一个session,也就是使用户在一次会话中跟同一个Tomcat进行交互 worker.loadbalancer..sticky_session_force=false worker.loadbalancer.sticky_session=1 #是否启用session共享
worker.worker2.port:第一台负载均衡服务器AJP协议连接器的连接端口,这里配置为8009
worker.worker2.host:第一台负载均衡服务器的主机名、域名或者IP地址,这里配置为本机localhost
worker.worker2.type:JK模块实现负载均衡采用的是AJP协议1.3版本,因此第一台负载均衡服务器的类型配置为ajp13
worker.worker2.lbfactor:第一台负载均衡服务器在整个负载均衡系统中所占的权重,这里配置为10,权重越大,越有可能处理更多的请求,建议给性能好的机器配置更高的权重。
5、修改2个tomcat的server.xml,保证2个tomcat的端口号不一致,具体配置如下,
[root@localhost conf]# vim server.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!--指定tomcat服务器的Telnet登录端口 并设置关闭服务器指令为"SHUTDOWN" 此处需注意服务器安全 --> <Server port="8005" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <!--指定tomcat服务器的Telnet登录端口 并设置关闭服务器指令为"SHUTDOWN" 此处需注意服务器安全 --> <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" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <!-- 此处的端口要与workers.properties文件中s1负载均衡服务器一致 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <!-- jvmRoute属性用来负载均衡的转发对象 值一定要与在workers.properties文件中指定的另外一台一样 --> <Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2" > <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> </Host> </Engine> </Service> </Server>
需要修改的地方都有注释,8005 8080 8009 需要修改的端口号,worker3的分别是8006 8081 9009
开启了tomcat服务器之后,开启apache服务器(注意,顺序很重要!一定要先开tomcat)
http://192.168.88.130/TestProject/showInfo.do出现如下图 则代表成功
This message is from Server, RealPath: /usr/local/worker2/webapps/TestProject/ Current Session Id: DC185E2CA3CAEB64BE50CEE2DC7CBC5E.worker2
再次刷新 应该会变成worker3
停掉其中一台tomcat,继续访问上诉网址 看能否打开
本文出自 “面朝大海,真闲” 博客,请务必保留此出处http://yuanyujiao.blog.51cto.com/2095122/1629081
原文地址:http://yuanyujiao.blog.51cto.com/2095122/1629081