安装Tomcat&JDK
安装时候选择tomcat软件版本要与程序开发使用的版本一致。jdk版本要进行与tomcat保持一致。 准备2个linux虚拟机, 一个运行nginx进行负载均衡 一个用来运行tomcat
第一步:安装JDK
JDK官网地址: https://www.oracle.com/java/technologies/downloads/
##下载JDK软件包并解压缩 https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.tar.gz [root@tomcat ~]# ls 2023 jdk-20_linux-x64_bin.tar.gz nginx-1.22.1.tar.gz nginx-1.23.4.tar.gz anaconda-ks.cfg nginx-1.22.1 nginx-1.23.4 [root@tomcat ~]# mkdir /opt/jdk [root@tomcat ~]# tar -zxvf jdk-20_linux-x64_bin.tar.gz -O /opt/ [root@tomcat ~]# cd /opt/ [root@tomcat opt]# ls jdk-20.0.2 nginx rh website ##创建软连接 [root@tomcat jdk-20.0.2]# ln -s /opt/jdk-20.0.2/ /opt/jdk ##配置JAVA环境变量,进入/etc/profile增加如下信息 [root@tomcat ~]# sed -i.ori '$a export JAVA_HOME=/opt/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile [root@tomcat ~]# tail -3 /etc/profile export JAVA_HOME=/opt/jdk export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar ##读取/etc/profile环境配置 [root@tomcat ~]# source /etc/profile [root@tomcat ~]# echo $PATH /opt/jdk/bin:/opt/jdk/jre/bin:/home/Nginx-1.23.4/sbin:/op/bin:/op/jre/bin:/home/Nginx-1.23.4/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binnexport ## JDK配置检查 [root@tomcat ~]# java -version java version "20.0.2" 2023-07-18 Java(TM) SE Runtime Environment (build 20.0.2+9-78) Java HotSpot(TM) 64-Bit Server VM (build 20.0.2+9-78, mixed mode, sharing)
第二步:部署Tomcat
##下载tomcat软件包并解压缩 [root@tomcat ~]# tar -zxvf apache-tomcat-10.1.12.tar.gz -C /opt/ [root@tomcat ~]# cd /opt/ [root@tomcat opt]# ls apache-tomcat-10.1.12 jdk jdk-20.0.2 nginx rh website [root@tomcat opt]# ln -s apache-tomcat-10.1.12/ /opt/tomcat ##设置环境变量 [root@tomcat bin]# sed -i.ori.backup '$a TOMCAT=/opt/tomcat' /etc/profile [root@tomcat bin]# tail -1 /etc/profile TOMCAT=/opt/tomcat [root@tomcat bin]# source /etc/profile ##部分页面在初始化后是无法访问的,增加对应的权限 [root@tomcat bin]# ll /opt/jdk/ /opt/tomcat/ /opt/jdk/: total 24 drwxr-xr-x 2 root root 4096 Aug 22 01:16 bin drwxr-xr-x 5 root root 123 Aug 22 01:16 conf drwxr-xr-x 3 root root 132 Aug 22 01:16 include drwxr-xr-x 2 root root 4096 Aug 22 01:16 jmods drwxr-xr-x 72 root root 4096 Aug 22 01:16 legal drwxr-xr-x 5 root root 4096 Aug 22 01:16 lib lrwxrwxrwx 1 10668 10668 23 Jun 14 06:22 LICENSE -> legal/java.base/LICENSE drwxr-xr-x 3 root root 18 Aug 22 01:16 man -rw-r--r-- 1 10668 10668 290 Jun 14 06:22 README -rw-r--r-- 1 10668 10668 1269 Jun 14 06:22 release /opt/tomcat/: total 128 drwxr-x--- 2 root root 4096 Aug 22 01:58 bin -rw-r----- 1 root root 20123 Aug 8 15:16 BUILDING.txt drwx------ 2 root root 238 Aug 8 15:16 conf -rw-r----- 1 root root 6210 Aug 8 15:16 CONTRIBUTING.md drwxr-x--- 2 root root 4096 Aug 22 01:58 lib -rw-r----- 1 root root 60393 Aug 8 15:16 LICENSE drwxr-x--- 2 root root 6 Aug 8 15:16 logs -rw-r----- 1 root root 2333 Aug 8 15:16 NOTICE -rw-r----- 1 root root 3398 Aug 8 15:16 README.md -rw-r----- 1 root root 6776 Aug 8 15:16 RELEASE-NOTES -rw-r----- 1 root root 16076 Aug 8 15:16 RUNNING.txt drwxr-x--- 2 root root 30 Aug 22 01:58 temp drwxr-x--- 7 root root 81 Aug 8 15:16 webapps drwxr-x--- 2 root root 6 Aug 8 15:16 work ##检查tomcat是否正确安装 [root@tomcat bin]# /opt/tomcat/bin/version.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /opt/jdk Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Server version: Apache Tomcat/10.1.12 Server built: Aug 8 2023 19:16:13 UTC Server number: 10.1.12.0 OS Name: Linux OS Version: 3.10.0-1160.el7.x86_64 Architecture: amd64 JVM Version: 20.0.2+9-78 JVM Vendor: Oracle Corporation
Tomcat目录介绍
[root@tomcat ~]# tree /opt/tomcat/ -L 1 /opt/tomcat/ ├── bin # 存放tomcat管理脚本 ├── BUILDING.txt ├── conf # tomcat 配置文件存放目录 ├── CONTRIBUTING.md ├── lib # web应用调用的jar包存放路径 ├── LICENSE ├── logs # tomcat 日志存放目录,catalina.out 为主要输出日志 ├── NOTICE ├── README.md ├── RELEASE-NOTES ├── RUNNING.txt ├── temp # 存放临时文件 ├── webapps # web程序存放目录 └── work # 存放编译产生的.java 与 .class文件
webapps目录介绍
[root@tomcat ~]# tree /opt/tomcat/webapps/ -L 1 /opt/tomcat/webapps/ ├── docs # tomcat 帮助文档 ├── examples # web应用实例 ├── host-manager # 主机管理 ├── manager # 管理 └── ROOT # 默认站点根目录
Tomcat配置文件
[root@tomcat ~]# tree /opt/tomcat/conf/ /opt/tomcat/conf/ ├── catalina.policy ├── catalina.properties ├── context.xml ├── jaspic-providers.xml ├── jaspic-providers.xsd ├── logging.properties ├── server.xml # tomcat主配置 ├── tomcat-users.xml # tomcat管理用户配置 ├── tomcat-users.xsd └── web.xml
Tomcat启停管理
[root@tomcat ~]# /opt/tomcat/bin/startup.sh [root@tomcat ~]# /opt/tomcat/bin/shutdown.sh
启动tomcat访问
[root@tomcat ~]# /opt/tomcat/bin/startup.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /opt/jdk Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started. [root@tomcat ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 690/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 962/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1124/master tcp6 0 0 :::111 :::* LISTEN 690/rpcbind tcp6 0 0 :::8080 :::* LISTEN 1725/java tcp6 0 0 :::21 :::* LISTEN 971/vsftpd tcp6 0 0 :::22 :::* LISTEN 962/sshd tcp6 0 0 ::1:25 :::* LISTEN 1124/master udp 0 0 0.0.0.0:111 0.0.0.0:* 690/rpcbind udp 0 0 127.0.0.1:323 0.0.0.0:* 700/chronyd udp 0 0 0.0.0.0:846 0.0.0.0:* 690/rpcbind udp6 0 0 :::111 :::* 690/rpcbind udp6 0 0 ::1:323 :::* 700/chronyd udp6 0 0 :::846 :::* 690/rpcbind
检查日志
[root@tomcat ~]# tail -f /opt/tomcat/logs/catalina.out 22-Aug-2023 02:23:45.294 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-10.1.12/webapps/docs] 22-Aug-2023 02:23:45.319 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-10.1.12/webapps/docs] has finished in [25] ms 22-Aug-2023 02:23:45.319 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-10.1.12/webapps/examples] 22-Aug-2023 02:23:45.619 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-10.1.12/webapps/examples] has finished in [300] ms 22-Aug-2023 02:23:45.620 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-10.1.12/webapps/host-manager] 22-Aug-2023 02:23:45.636 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-10.1.12/webapps/host-manager] has finished in [16] ms 22-Aug-2023 02:23:45.636 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-10.1.12/webapps/manager] 22-Aug-2023 02:23:45.661 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-10.1.12/webapps/manager] has finished in [25] ms 22-Aug-2023 02:23:45.678 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 22-Aug-2023 02:23:45.748 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [912] milliseconds
管理页面入口:默认无法访问,需要在conf/tomcat-user.xml添加标签,$CATALINA_HOME/webapps/manager/META-INF/context.xml 和$CATALINA_HOME/webapps/host-manager/META-INF/context.xml 这两个文件,全部修改 RemoteAddrValve 的值:
服务状态页面(Server Status)、管理登录(Manager App)修改如下内容:
##修改管理登录的web页面,允许所有主机访问
[root@tomcat ~]# cat /opt/tomcat/webapps/manager/META-INF/context.xml <Context antiResourceLocking="false" privileged="true" > <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor" sameSiteCookies="strict" /> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" /> ##添加|\d+\.\d+\.\d+\.\d+表示允许所有主机访问 <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
##/conf/tomcat-users.xml 添加管理用户登录的标签(用户名、密码) [root@tomcat ~]# cat /opt/tomcat/conf/tomcat-users.xml <role rolename="manager"/> <role rolename="manager-gui"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <role rolename="manager-script"/> <user username="admin" password="admin" roles="manager,manager-gui,manager-jmx,manager-status,manager-script"/>
查看Server Status服务器状态
查看管理者页面
主机管理者页面(host-manager):
##修改/webapps/host-manager/META-INF/context.xml,允许所有主机访问 [root@tomcat ~]# cat /opt/tomcat/webapps/host-manager/META-INF/context.xml <Context antiResourceLocking="false" privileged="true" > <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor" sameSiteCookies="strict" /> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" /> ##添加|\d+\.\d+\.\d+\.\d+ <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> </Context>
##添加登录host-manager的用户名、密码 <role rolename="admin-gui"/> <role rolename="admin-script"/> <user username="tomcat" password="123456" roles="admin-gui,admin-script"/>
标签:opt,bin,Aug,JavaWeb,tomcat,22,开发,Tomcat,root From: https://www.cnblogs.com/junwured/p/17648478.html