Tomcat多实例
安装好 jdk
在部署 Tomcat 之前必须安装好 jdk,因为 jdk 是 Tomcat 运行的必要环境。
1. #关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
2. #将安装 Tomcat 所需软件包传到/opt目录下
apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
3. #切换至/opt下,安装JDK
cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm
4. #查看java版本
java -version
安装 tomcat
1. #切换至/opt下,解压tomcat包
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
2. #新建文件夹/usr/local/tomcat
mkdir /usr/local/tomcat
3. #将解压后的包拷贝至/usr/local/下并重命名
cp -a apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a apache-tomcat-9.0.16 /usr/local/tomcat/tomcat2
配置 tomcat 环境变量
vim /etc profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
修改tomcat2中的主配置文件
vim /usr/local/tomcat/tomcat2/conf/server.xml
22 <Server port="8006" shutdown="SHUTDOWN">
69 <Connector port="8081" protocol="HTTP/1.1"
116 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
修改启动脚本和关闭脚本
1. #修改tomcat1的/usr/local/tomcat/tomcat1/bin/startup.sh
vim /usr/local/tomcat/tomcat1/bin/startup.sh
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
2. #修改tomcat1 的/usr/local/tomcat/tomcat1/bin/shutdown.sh
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
3. #修改tomcat2的/usr/local/tomcat/tomcat2bin/startup.sh
vim /usr/local/tomcat/tomcat2/bin/startup.sh
export CATALINA_HOME1=/usr/local/tomcat/tomcat2
export CATALINA_BASE1=/usr/local/tomcat/tomcat2
export TOMCAT_HOME1=/usr/local/tomcat/tomcat2
4. #修改tomcat2的/usr/local/tomcat/tomcat2/bin/shutdown.sh
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
export CATALINA_HOME1=/usr/local/tomcat/tomcat2
export CATALINA_BASE1=/usr/local/tomcat/tomcat2
export TOMCAT_HOME1=/usr/local/tomcat/tomcat2
启动tomcat并查看
1. #启动tomcat1
cd /usr/local/tomcat/
./tomcat1/bin/startup.sh
2. #启动tomcat2
./tomcat2/bin/startup.sh
3. #查看是否启动成功
ss -ntap|grep java
http://192.168.80.130:8080/
Nginx+Tomcat负载均衡、动静分离
- standalone模式,Tomcat单独运行,直接接受用户的请求,不推荐。反向代理,单机运行,提供了一个Nginx作为反向代理,可以做到静态由nginx提供响应,动态jsp代理给Tomcat
- LNMT:Linux + Nginx + MySQL + Tomcat
- LAMT:Linux + Apache(Httpd)+ MySQL + Tomcat
- 前置一台Nginx,给多台Tomcat实例做反向代理和负载均衡调度,Tomcat上部署的纯动态页面更适合
- LNMT:Linux + Nginx + MySQL + Tomcat
- 多级代理
- LNNMT:Linux + Nginx + Nginx + MySQL + Tomcat
动态服务器的问题,往往就是并发能力太弱,往往需要多台动态服务器一起提供服务。如何把并发的压力分摊,这就需要调度,采用一定的调度策略,将请求分发给不同的服务器,这就是Load Balance负载均衡。
当单机Tomcat,演化出多机多级部署的时候,一个问题便凸显出来,这就是Session。而这个问题的由来,都是由于HTTP协议在设计之初没有想到未来的发展。
部署Nginx 负载均衡器
环境简介:
ngnix:192.168.80.130
tomcat1:192.168.80.133
tomcat2: 192.168.80.129
1. #关闭防火墙
systemctl stop firewalld
setenforce 0
2. #安装依赖关系包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3. #新建用户和组便于管理
useradd -M -s /sbin/nologin nginx
4. #切换至opt目录,将下载好的压缩包传进来解压
cd /opt
tar -zxf nginx-1.12.0.tar.gz
4. #切换至解压后的目录下编译
cd nginx-1.12.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
5. #安装
make && make install -j4
6. #做软连接,让系统识别nginx的操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
7. #将nginx命令加入服务
cd /lib/systemd/system
vim nginx.service
#!/bin.bash
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8. #重新加载单元.启动服务
systemctl daemon-reload
systemctl start nginx
9. #查看是否成功启动
ss -ntap|grep nginx
http://192.168.80.130/
部署第一台Tomcat
192.168.80.129
1. #关闭防火墙
systemctl stop firewalld
setenforce 0
2. #切换至/opt,将安装 Tomcat 所需软件包传到/opt目录下
apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
3. #安装JDK
rpm -ivh jdk-8u201-linux-x64.rpm
4. #修改换将变量配置文件
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
5. #刷新配置文件
source /etc/profile
6. #切换至/opt下,解压tomcat包
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
7. #将解压后的包拷贝至/usr/local/下并重命名
cp -r apache-tomcat-9.0.16 /usr/local/tomcat
8. #添加用户设置属主属组
useradd -s /sbin/nologin tomcat
chown tomcat:tomcat /usr/local/tomcat -R
9. #新建服务文件
vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
10. #重新加载服务,并开启,查看是否成功启动
systemctl daemon-reload
systemctl start tomcat
ss -ntap |grep 8080
#############新建动态页面站点###########3
11. #切换至webapp下,新建test目录
cd /usr/local/tomcat/webapps/
mkdir test
12. #建立动态页面文件
vim test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page </title>
</head>
<body>
<% out.println("动态页面1,http://www.test1.com");%>
</body>
</html>
13. #修改主配置文件
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" />
</Host>
14. #重启服务,并在网页测试
systemctl restart tomcat.service
http://192.168.80.129:8080/
部署第二台Tomcat
1. #关闭防火墙
systemctl stop firewalld
setenforce 0
2. #切换至/opt,将安装 Tomcat 所需软件包传到/opt目录下
apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
3. #安装JDK
rpm -ivh jdk-8u201-linux-x64.rpm
4. #修改环境变量配置文件
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
5. #刷新配置文件
source /etc/profile
6. #切换至/opt下,解压tomcat包
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
7. #将解压后的包拷贝至/usr/local/下并重命名
cp -r apache-tomcat-9.0.16 /usr/local/tomcat
8. #添加用户设置属主属组
useradd -s /sbin/nologin tomcat
chown tomcat:tomcat /usr/local/tomcat -R
9. #新建服务文件
vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
10. #重新加载服务,并开启,查看是否成功启动
systemctl daemon-reload
systemctl start tomcat
ss -ntap |grep 8080
#############新建动态页面站点###########3
11. #切换至webapp下,新建test目录
cd /usr/local/tomcat/webapps/
mkdir test
12. #建立动态页面文件
vim test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page </title>
</head>
<body>
<% out.println("动态页面2,http://www.test2.com");%>
</body>
</html>
13. #修改主配置文件
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" />
</Host>
14. #重启服务,并在网页测试
systemctl restart tomcat.service
http://192.168.80.133:8080/
nginx 配置
准备静态页面和图片
1. #切换至/usr/local/nginx/html/目录下
cd /usr/local/nginx/html/
2. #创建test文件夹,并在里面创建静态网页
mkdir test
cd test
vim test.html
this is static test web !!
3. #拖一张图片至test下改名为1.jpg
mv 1.jfif 1.jpg
4. #配置主配置文件
vim /usr/local/nginx/conf/nginx.conf
#配置负载均衡服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
#gzip on;
upstream tomcat_server {
server 192.168.59.105:8080 weight=1;
server 192.168.59.118:8080 weight=1;
}
#动静分离
location ~ .*\.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#静态图片正则
location ~* .*\.(jpg|html|png|gif)$ {
root /usr/local/nginx/html/test;
}
location / {
root html;
index index.html index.htm;
}
5. #重启nginx并测试
http://192.168.80.130/1.jpg
http://192.168.80.130/test.html