快速安全部署 Tomcat
主机环境
IP | 域名 | 用途 |
---|---|---|
172.25.254.10 | tomcat1.org | Tomcat |
172.25.254.20 | tomcat2.org | Tomcat |
172.25.254.30 | nginx-server.org | Nginx 负载均衡 |
Tomcat 安装
172.25.254.10
# 安装JAVA
[root@tomcat1 ~]# yum install java-1.8.0-openjdk.x86_64 -y
# 下载Tomcat,推荐在Windows主机下载后,上传虚拟机
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.93/src/apache-tomcat-9.0.93.tar.gz
tar zxf apache-tomcat-9.0.93.tar.gz -C /usr/local/
# 创建软连接(简化命令)
ln -s /usr/local/apache-tomcat-9.0.93/ /usr/local/tomcat
# 添加用户,修改权限
useradd -s /sbin/nologin -M tomcat
chown -R tomcat.tomcat /usr/local/tomcat/
# 为Tomcat添加JAVA
vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/etc/alternatives/jre_openjdk/
# 设置Tomcat命令启停脚本
vim /usr/bin/tomcat
#!/bin/bash
case $1 in
start)
/usr/local/tomcat/bin/startup.sh
;;
stop)
/usr/local/tomcat/bin/shutdown.sh
esac
chmod +x /usr/bin/tomcat
# 设置开机自启脚本
vim /lib/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
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/bin/tomcat start
ExecStop=/usr/bin/tomcat stop
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
# 重载配置文件,并启动
systemctl daemon-reload
# 启动不成功,可能是/usr/local/apache-tomcat-9.0.93/bin/下的.sh没有x权限
# 官方下载的包内,.sh文件没有x权限
systemctl enable --now tomcat
# 复制文件给172.25.254.20
scp -rp /usr/local/apache-tomcat-9.0.93 root@172.25.254.20:/usr/local/
scp -p /usr/bin/tomcat root@172.25.254.20:/usr/bin/
scp -p /lib/systemd/system/tomcat.service root@172.25.254.20:/lib/systemd/system/
172.25.254.20
[root@tomcat2 ~]# yum install java-1.8.0-openjdk.x86_64 -y
ln -s /usr/local/apache-tomcat-9.0.93 /usr/local/tomcat
useradd -s /sbin/nologin -M tomcat
chown -R tomcat.tomcat /usr/local/tomcat/
systemctl daemon-reload
systemctl enable --now tomcat
效果演示
Tomcat 反向代理
Tomcat 服务器
cp test.jsp /usr/local/tomcat/webapps/ROOT/
172.25.254.30
# 安装nginx
dnf install nginx -y
vim /etc/hosts # Windows主机 hosts 文件 也需要添加
172.25.254.10 tomcat1.org
172.25.254.20 tomcat2.org
172.25.254.30 nginx-server.org
# 编辑子配置文件
vim /etc/nginx/conf.d/vhost.conf
server {
listen *:80;
server_name nginx-server.org;
root /data/web/html;
index index.html;
location ~ \.jsp$ {
proxy_pass http://172.25.254.10:8080;
}
}
# 启动nginx
systemctl enable --now nginx
效果演示
Tomcat 负载均衡
172.25.254.30
vim /etc/nginx/conf.d/vhost.conf
upstream tomcat {
# ip_bash;
# hash $cookie_JSESSIONID;
server 172.25.254.10:8080;
server 172.25.254.20:8080;
}
server {
listen *:80;
server_name nginx-server.org;
root /data/web/html;
index index.html;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
# 检测语法是否出错
nginx -t
# 重载nginx配置文件
nginx -s reload
效果演示
目前配置中存在的问题
- 交替访问的IP不一致,会话不一致,导致提交的数据消失
- nginx配置中使用ip_bash算法进行解决
- 刷新导致会话断开,提交的数据消失
- nginx配置中使用hash $cookie_JSESSIONID算法进行上述问题
Memcached
Tomcat 服务器
# 安装 Memcached
yum install memcached -y
# 编辑配置文件
vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 0.0.0.0,::1"
# 启动Memcached
systemctl enable --now memcached
# 查看端口是否打开
netstat -antlupe | grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 983 61722 31178/memcached
tcp6 0 0 ::1:11211 :::* LISTEN 983 61723 31178/memcached
session 共享服务器
有时候,当客户访问Tomcat时,恰巧Tomcat宕机,导致客户提交的数据丢失,给用户带来不好的体验
需要部署Tomcat会话共享,避免这种情况发生
Tomcat 服务器
# 上传所有.jar文件到Tomcat服务器的/usr/local/tomcat/lib/中
# 编辑会话共享配置文件
vim /usr/local/tomcat/conf/context.xml
# 172.25.254.10
...
<Manager pathname="" />
-->
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.254.10:11211,n2:172.25.254.20:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
# 172.25.254.20
...
<Manager pathname="" />
-->
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.254.10:11211,n2:172.25.254.20:11211"
failoverNodes="n2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
systemctl restart tomcat
Nginx 服务器
vim /usr/local/nginx/conf.d/vhosts.conf
upstream tomcat {
hash $cookie_JSESSIONID;
server 172.25.254.10:8080;
server 172.25.254.20:8080;
}
server {
listen *:80;
server_name nginx-server.org;
root /data/web/html;
index index.html;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
nginx -s reload
效果演示
此时暂停 172.25.254.10 的 Tomcat 服务
systemctl stop tomcat
然后输入ccc 333 点击提交
可以看见,即使172.25.254.10的Tomcat服务宕掉后,会话依然可以正常进行,并没有导致提交的数据丢失