apache+tomcat负载均衡
# 实验环境
- Apache 192.168.200.101
- Tomcat1 192.168.200.102
- Tomcat2 192.168.200.103
# 一、安装tomcat1
# (1)解压jdk
tar xf jdk-7u65-linux-x64.gz
mv jdk1.7.0_65 /usr/local/java
1
2
2
# (2)配置环境变量
vim /etc/profile.d/java.sh #添加以下内容
export JAVA_HOME=/usr/local/java #设置java的根目录
export PATH=$PATH:$JAVA_HOME/bin
1
2
3
2
3
# (3)生效脚本
source /etc/profile.d/java.sh
1
# (4)安装tomcat
tar xf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat7
mkdir -p /webapp
cd /webapp
vim index.jsp // 设置默认网页,新建jsp页面并添加以下内容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title> JSP test1 page</title>
</head>
<body>
<% out .println("welcom to test sits,http://www.text1.com");%>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
编辑主配置文件
vim /usr/local/tomcat7/conf/server.xml #在130行下添加以下红色部分内容
128 <Host name="localhost" appBase="webapps"
129 unpackWARs="true" autoDeploy="true"
130 xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/webapp" path="" reloadable="false" >
</Context>
1
2
3
4
5
6
7
2
3
4
5
6
7
启动tomcat
/usr/local/tomcat7/bin/startup.sh
1
# 二、安装tomcat2
# (1)解压jdk
tar xf jdk-7u65-linux-x64.gz
mv jdk1.7.0_65 /usr/local/java
1
2
2
# (2)配置环境变量
vim /etc/profile.d/java.sh #添加以下内容
export JAVA_HOME=/usr/local/java #设置java的根目录
export PATH=$PATH:$JAVA_HOME/bin
1
2
3
2
3
# (3)生效脚本
source /etc/profile.d/java.sh
1
# (4)安装tomcat
tar xf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat7
mkdir -p /webapp
cd /webapp
vim index.jsp #设置默认网页,新建jsp页面并添加以下内容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title> JSP test2 page</title>
</head>
<body>
<% out .println("welcom to test sits,http://www.text2.com");%>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
编辑主配置文件
vim /usr/local/tomcat7/conf/server.xml #在130行下添加以下红色部分内容
128 <Host name="localhost" appBase="webapps"
129 unpackWARs="true" autoDeploy="true"
130 xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/webapp" path="" reloadable="false" >
</Context>
1
2
3
4
5
6
2
3
4
5
6
启动tomcat
/usr/local/tomcat7/bin/startup.sh
1
保证tomcat能够正常访问
# 三、安装apache
tar xf httpd-2.2.17.tar.gz
cd httpd-2.2.17
./configure --prefix=/usr/local/httpd --enable-so --enable-cgi --enable-charset --enable-charset-lite --enable-rewrite --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-proxy-balabcer --enable-lbmenthod-heartbeat --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog
make && make install
ln -s /usr/local/httpd/bin/* /usr/local/bin
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
vi /etc/init.d/httpd #编辑启动脚本 ,增加红色部分内容
#!/bin/sh
#chkconfig:345 66 88
#description:http apache
chmod +x /etc/init.d/httpd
chkconfig --add httpd
vim /usr/local/httpd/conf/httpd.conf #修改配置文件
// 去掉388行的注释,改为:
Include conf/extra/httpd-vhosts.conf
// 修改虚拟主机配置文件
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot /webapp/index.jsp
ServerName CentOS6-node1
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://192.168.200.102:8080/
ProxyPassReverse / http://192.168.200.102:8080/
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /webapp/index.jsp
ServerName CentOS6-node1
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://192.168.200.103:8080/
ProxyPassReverse / http://192.168.200.103:8080/
</VirtualHost>
ProxyPass / balancer://proxy/
<Proxy balancer://proxy/>
BalancerMember http://192.168.200.102:8080/
BalancerMember http://192.168.200.103:8080/
</Proxy>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
启动apache
/etc/init.d/httpd start //出现以下warning不用管
Warning: DocumentRoot [/webapp/index.jsp] does not exist
Warning: DocumentRoot [/webapp/index.jsp] does not exist
1
2
3
2
3
# 四、==测试==
使用浏览器访问http://192.168.200.101 刷新轮流出现tomcat1和tomcat2界面如下:
上次更新: 2022/06/12, 15:48:09