Tomcat

一、安装tomcat

bash 复制代码
systemctl disable --now firewalld.service	#关闭防火墙

#准备好tomcat压缩包,可从官网上下载
https://tomcat.apache.org/

#由于tomcat需要java环境,故
dnf install java-1.8.0-openjdk.x86_64 -y

ls
anaconda-ks.cfg               Desktop    Downloads  Pictures  rh9.3.repo  Videos
apache-tomcat-9.0.107.tar.gz  Documents  Music      Public    Templates


tar zxf apache-tomcat-9.0.107.tar.gz -C /usr/local/	#将tomcat解压到/usr/local/目录下

cd /usr/local/	#进入解压目录,查看解压后的文件
ls
apache-tomcat-9.0.107  bin  etc  games  include  lib  lib64  libexec  sbin  shar

mv apache-tomcat-9.0.107/ tomcat	#将解压后的文件放入到单独一个目录下,方便后续实验
cd tomcat/
ls
bin           CONTRIBUTING.md  logs       RELEASE-NOTES  webapps
BUILDING.txt  lib              NOTICE     RUNNING.txt    work
conf          LICENSE          README.md  temp

cd bin/
ls	#查看tomcat的控制脚本
bootstrap.jar                 configtest.sh     shutdown.sh
catalina.bat                  daemon.sh         startup.bat
catalina.sh                   digest.bat        startup.sh
catalina-tasks.xml            digest.sh         tomcat-juli.jar
ciphers.bat                   makebase.bat      tomcat-native.tar.gz
ciphers.sh                    makebase.sh       tool-wrapper.bat
commons-daemon.jar            setclasspath.bat  tool-wrapper.sh
commons-daemon-native.tar.gz  setclasspath.sh   version.bat
configtest.bat                shutdown.bat      version.sh

./startup.sh	#启动tomcat
Using CATALINA_BASE:   /usr/local/tmocat
Using CATALINA_HOME:   /usr/local/tmocat
Using CATALINA_TMPDIR: /usr/local/tmocat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tmocat/bin/bootstrap.jar:/usr/local/tmocat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

netstat -antuple | grep 8080	#查看端口确认启动成功
tcp6       0      0 :::8080                 :::*                    LISTEN      0          65256      33796/java

查看是否启动成功

浏览器输入192.168.75.10:8080

二、生成tomcat的启动文件

1.生成tomcat的主配置文件

bash 复制代码
vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/etc/alternatives/jre

2.生成启动文件

bash 复制代码
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/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

3.创建tomcat用户并设定安装目录权限

bash 复制代码
useradd -s /sbin/nologin -M tomcat
chown tomcat.tomcat /usr/local/tomcat/ -R

4.重启脚本启动服务

bash 复制代码
systemctl daemon-reload
systemctl enable --now tomcat.service

三、nginx与tomcat实现负载均衡

bash 复制代码
#tomcat配置:提前准备好一个测试文件test.jsp,放入/usr/local/tomcat/webapps/ROOT/下
两台tomcat都要

#重启服务
[root@tca ROOT]# systemctl restart tomcat.service
bash 复制代码
#nginx配置:在已经准备好的子配置文件中写入参数
vim /usr/local/nginx/conf.d/vhosts.conf

upstream tomcat {
    hash $cookie_JSESSIONID;
    server 192.168.75.10:8080 max_fails=3 fail_timeout=5;
    server 192.168.75.20:8080 max_fails=3 fail_timeout=5;
}

server {
    listen  80;
    server_name www.yna.org;

    #root /usr/local/nginx/html;
    location ~ \.jsp$ {
        proxy_pass http://tomcat;
        #fastcgi_index index.php;
        #include fastcgi.conf;
    }
}

#记得做好windows本地域名解析
#检查语法无误后重启
nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

nginx -s reload

测试:在浏览器中输入<www.yna.org/test.jsp>

第一次访问的是10,刷新后访问20,再刷新访问10,如此实现负载均衡

四、session共享服务器

msm实验配置

此实验需要建立在上一个实验基础之上

bash 复制代码
#安装memcached和telnet
dnf install memcached -y
dnf install telnet -y

#修改memcached配置
vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 0.0.0.0,::1"	#将127.0.0.1改为0.0.0.0


#实验需要以下jar包
kryo-3.0.3.jar
 asm-5.2.jar
 objenesis-2.6.jar
 reflectasm-1.11.9.jar
 minlog-1.3.1.jar
 kryo-serializers-0.45.jar
 msm-kryo-serializer-2.3.2.jar
 memcached-session-manager-tc9-2.3.2.jar
 spymemcached-2.12.3.jar
 memcached-session-manager-2.3.2.jar
 
 #将上述包放入/usr/local/tomcat/lib下
 #重启tomcat
systemctl restart tomcat.service
 
 
 #配置tomcat
 vim /usr/local/tomcat/conf/context.xml
 
     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
    #加入:
     <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
         memcachedNodes="n1:192.168.75.10:11211,n2:192.168.75.20:11211"
         failoverNodes="n1"	#此处写本机
         requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
         transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
        />

</Context>

tomcat2同样配置即可
相关推荐
叫致寒吧6 小时前
Tomcat详解
java·tomcat
S***267510 小时前
基于SpringBoot和Leaflet的行政区划地图掩膜效果实战
java·spring boot·后端
马剑威(威哥爱编程)10 小时前
鸿蒙6开发视频播放器的屏幕方向适配问题
java·音视频·harmonyos
JIngJaneIL10 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
V***u45310 小时前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
这是程序猿11 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
i***t91911 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
k***082911 小时前
【监控】spring actuator源码速读
java·spring boot·spring
麦麦鸡腿堡11 小时前
Java_网络编程_InetAddress类与Socket类
java·服务器·网络
z***62612 小时前
Docker:基于自制openjdk8镜像 or 官方openjdk8镜像,制作tomcat镜像
docker·容器·tomcat