Tomcat多实例的步骤(理论)
1.安装好 jdk
2.安装 tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
3.配置 tomcat 环境变量
vim /etc/profile.d/tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
source /etc/profile.d/tomcat.sh
4.修改 tomcat2 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号
vim /usr/local/tomcat/tomcat2/conf/server.xml
#22行,修改Server prot,默认为8005 -> 修改为8006
修改为8081
#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
5.修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
vim /usr/local/tomcat/tomcat1/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat2/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
6.启动各 tomcat 中的 /bin/startup.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
netstat -natp | grep java
7.浏览器访问测试
http://192.168.14.154:8080
http://192.168.14.154:8081
1.2 Tomcat多实例的具体实验步骤(实操)
第一步:放入安装关闭防火墙和selinux
第五步:修改 tomcat2 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号
1)修改主配置文件,修改Server prot,默认为8005 -> 修改为8006
2)修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081
3)修改Connector port AJP/1.3,默认为8009 -> 修改为8010
第六步:修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
1)修改tomcat1的startup.sh
4)修改tomcat2的shutdown.sh
第七步:启动各 tomcat 中的 /bin/startup.sh
2.1 Nginx+Tomcat负载均衡、动静分离的步骤(理论)
Nginx 服务器:192.168.153.50:80
Tomcat服务器1:192.168.153.60:8080
Tomcat服务器2:192.168.153.40:8080 192.168.153.40:8081
1.部署Nginx 负载均衡器
systemctl stop firewalld
setenforce 0
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./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-stream
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
2.部署2台Tomcat 应用服务器
systemctl stop firewalld
setenforce 0
tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
tar zxvf apache-tomcat-8.5.16.tar.gz
mv /opt/apache-tomcat-8.5.16/ /usr/local/tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
netstat -ntap | grep 8080
3.动静分离配置
(1)Tomcat1 server 配置
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title> #指定为 test1 页面
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true">
</Context>
</Host>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
(2)Tomcat2 server 配置
mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/test
vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title> #指定为 test2 页面
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>
vim /usr/local/tomcat/tomcat1/conf/server.xml
#删除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" />
</Host>
/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test3 page</title> #指定为 test3 页面
</head>
<body>
<% out.println("动态页面 3,http://www.test3.com");%>
</body>
</html>
vim /usr/local/tomcat/tomcat2/conf/server.xml
#删除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" />
</Host>
/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
(3)Nginx server 配置
#准备静态页面和静态图片
echo '这是静态页面
' > /usr/local/nginx/html/index.html
mkdir /usr/local/nginx/html/img
cp /root/game.jpg /usr/local/nginx/html/img
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
#gzip on;
#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
upstream tomcat_server {
server 192.168.153.60:8080 weight=1;
server 192.168.153.40:8080 weight=1;
server 192.168.153.40:8081 weight=1;
}
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
#access_log logs/host.access.log main;
#配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理
location ~ .*.jsp$ {
proxy_pass http://tomcat_server;
#设置后端的Web服务器可以获取远程客户端的真实IP
##设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。
proxy_set_header HOST KaTeX parse error: Expected 'EOF', got '#' at position 7: host; #̲#把remote_addr赋值给X-Real-IP,来获取源IP
proxy_set_header X-Real-IP $remote_addr;
##在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#配置Nginx处理静态图片请求
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img;
expires 1d;
}
location / {
root html;
index index.html index.htm;
}
......
}
......
}
4.测试效果
测试静态页面效果
浏览器访问 http://192.168.80.10/
浏览器访问 http://192.168.80.10/game.jpg
测试负载均衡效果,不断刷新浏览器测试
浏览器访问 http://192.168.80.10/index.jsp
2.2 Nginx+Tomcat负载均衡、动静分离的具体实验步骤(实操)
第一步:准备好Nginx服务器(192.168.153.50),具体步骤参考:互联网最受欢迎的开源Web服务器软件——Nginx的搭建
第二步:准备好tomcat服务器(192.168.153.60:8080),具体步骤参考:必须要掌握的开源Web应用服务器—Tomcat服务部署和优化
第三步:准备好tomcat服务器的两个实例(192.168.153.40:8080和192.168.153.40:8081),具体步骤参考本文第一段
第四步:动静分离Tomcat1 server 配置
1)配置Tomcat1的动态网页显示内容
第五步:Tomcat2 server 配置(实例一)
1)创建Tomcat实例1和Tomcat实例2的网页测试目录test和测试网页文件
2)Tomcat1主配置删除前面的 HOST 配置,添加新的HOST配置
第五步:Tomcat2 server 配置(实例二)
1)创建Tomcat实例2的测试网页文件
2)Tomcat2主配置删除前面的 HOST 配置,添加新的HOST配置
第六步:Nginx server 配置(192.168.153.50)
1)准备网页显示相关内容