部署前准备:
iptables和selinux不做配置,关掉
系统光盘作为yum源,配置yum文件
源码包准备jdk-7u65-linux-x64.gz apache-tomcat-7.0.54.tar.gz
注意源码包存放位置要与脚本中相互对应
环境介绍:
一台nginx,两台tomcat
分别在后端tomcat1和tomcat2上配置:
[[email protected] ~]# vi install_tomcat.sh
#!/bin/bash
##by linuxfan
#################解压######################
tar zxvf /usr/src/jdk-7u65-linux-x64.gz -C /usr/src/
tar zxvf /usr/src/apache-tomcat-7.0.54.tar.gz -C /usr/src/
###############config java env#############
yum -y remove java
mv /usr/src/jdk1.7.0_65/ /usr/local/java
cat <<END >>/etc/profile ##设置JAVA的环境变量
export JAVA_HOME=/usr/local/java
export PATH=\$PATH:\$JAVA_HOME/bin
END
source /etc/profile
java -version
################config tomcat7 #############
mv /usr/src/apache-tomcat-7.0.54 /usr/local/tomcat7
reboot
:wq
sh -x install_tomcat.sh
等待重启后,启动服务:
/usr/local/tomcat7/bin/startup.sh &&netstat -utpln |grep 8080
cd /usr/local/tomcat7/webapps/ROOT/
[[email protected] ROOT]# rm -rf ./*
[[email protected] ROOT]# vi index.jsp ##这里注意tomcat1和2服务器的网页不同,分别设为test1,test2
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP TEST PAGE1 </title>
</head>
<body>
<% out.println("Welcome to test site;http://www.test1.com");%>
</body>
</html>
:wq
分别访问测试:
http://ip地址:8080/
配置前端nginx:
yum -y install pcre-devel zlib-devel openssl-devel
useradd -s /bin/false nginx
tar axvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module \
--with-http_ssl_module &&make &&make install
vim nginx.conf
upstream tomcat_server { ##在http{}中
server tomcat1的ip地址:8080 weight=1; ##weight表权重,根据实际情况可设置
server tomcat2的ip地址:8080 weight=1;
}
location / { ##在server{}中
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
}
:wq
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
多次访问测试:
最终实现负载均衡