一.下载和安装
1.前提部署JDK环境
(1)到官网下载并上传到Linux(我的以jdk1.8.0_371为例)
(2)放到某个目录之后进行解压解包
(3)编辑/etc/profile文件在末尾添加以下内容
(4)刷新/etc/profile并查看java版本,能正常查看到版本即成功
2.安装tomcat
(1)到官网下载这个二进制包(我的以apache-tomcat-8.5.70为例)
(2)创建目录存放并解压解包,下载相关包
(3)切换到apache-tomcat-8.5.70/bin目录下进行启动、关闭等操作,能正常启动查看版本
(4)到浏览器验证成功即可
二.tomcat配置文件/目录简介
1.bin目录
2.conf目录
3.lib目录
4.logs目录
5..temp目录
6.webapps目录
7.work目录
三.tomcat解决启动较慢问题
1.查看logs/catalina.out日志中当前的启动速度
2.查看当前有多少熵可以用于生成session随机数
3.下载rng-tools包,进一步增加熵数,验证后有启动时间缩减效果
四.tomcatweb管理功能(有较大安全风险)
1.允许用户管理,修改用户配置tomcat/apache-tomcat-8.5.70/conf/tomcat-users.xml
2.允许app管理,修改tomcat/apache-tomcat-8.5.70/webapps/manager/META-INF/context.xml
3.允许主机管理,修改tomcat/apache-tomcat-8.5.70/webapps/host-manager/META-INF/context.xml
4.验证
点击右方server status等三个区域,使用用户配置中定义的用户名和密码登录即可查看管理相关内容
注意:进入这个界面进行选择时可以先试用uname -a命令查看自己系统,x86_64就选择x64,选择合适的包,否则可能会出现cannot execute binary file: Exec format error报错
[root@localhost ~]# uname -a
Linux localhost.localdomain 4.18.0-348.el7.x86_64 #1 SMP Tue Oct 19 15:14:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost java]# ll
total 135960
drwxr-xr-x 8 root root 294 Aug 6 20:52 jdk1.8.0_371
-rw-r--r-- 1 root root 139219380 Aug 3 15:52 jdk-8u371-linux-x64.tar.gz
[root@localhost java]# pwd
/usr/java
[root@localhost ~]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_371
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.70
export TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.5.70
root@localhost java]# source /etc/profile
[root@localhost java]# java -version
java version "1.8.0_371"
Java(TM) SE Runtime Environment (build 1.8.0_371-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.371-b11, mixed mode)
[root@localhost tomcat]# ll
total 10320
drwxr-xr-x 9 tomcat tomcat 220 Aug 6 20:47 apache-tomcat-8.5.70
-rw-r--r-- 1 tomcat tomcat 10564666 Aug 3 14:51 apache-tomcat-8.5.70.tar.gz
[root@localhost tomcat]# pwd
/usr/local/tomcat
[root@localhost tomcat]# yum install -y tomcat.noarch
[root@localhost tomcat]# yum install -y tomcat-webapps.noarch
[root@localhost tomcat]# cd apache-tomcat-8.5.70/bin/
[root@localhost bin]# pwd
/usr/local/tomcat/apache-tomcat-8.5.70/bin
[root@localhost bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME: /usr/java/jdk1.8.0_371
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@localhost bin]# ./version.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME: /usr/java/jdk1.8.0_371
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Server version: Apache Tomcat/8.5.70
Server built: Aug 9 2021 16:17:14 UTC
Server number: 8.5.70.0
OS Name: Linux
OS Version: 3.10.0-1160.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_371-b11
JVM Vendor: Oracle Corporation
注意:tomcat的启动和关闭,以管理员身份可以使用相对路径或绝对路径去运行startup.sh或shutdown.sh文件,非管理员身份可更改/usr/lib/systemd/system/tomcat.service相关参数进行启动
[root@localhost bin]# netstat -lntup | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 8267/java
drwxr-x--- 2 root root 4096 Aug 3 15:57 bin #tomcat的各种管理脚本
[root@localhost bin]# ll #.bat用于windows,.sh用于Linux
total 872
-rw-r----- 1 root root 36317 Aug 10 2021 bootstrap.jar
-rw-r----- 1 root root 16840 Aug 10 2021 catalina.bat
-rwxr-x--- 1 root root 25294 Aug 10 2021 catalina.sh #真正用于启动tomcat,可以在里面设置各种参数
-rw-r----- 1 root root 1664 Aug 10 2021 catalina-tasks.xml
-rw-r----- 1 root root 2123 Aug 10 2021 ciphers.bat
-rwxr-x--- 1 root root 1997 Aug 10 2021 ciphers.sh
-rw-r----- 1 root root 25357 Aug 10 2021 commons-daemon.jar
-rw-r----- 1 root root 207420 Aug 10 2021 commons-daemon-native.tar.gz
-rw-r----- 1 root root 2040 Aug 10 2021 configtest.bat
-rwxr-x--- 1 root root 1922 Aug 10 2021 configtest.sh
-rwxr-x--- 1 root root 9100 Aug 10 2021 daemon.sh
-rw-r----- 1 root root 2091 Aug 10 2021 digest.bat
-rwxr-x--- 1 root root 1965 Aug 10 2021 digest.sh
-rw-r----- 1 root root 3460 Aug 10 2021 setclasspath.bat
-rwxr-x--- 1 root root 3708 Aug 10 2021 setclasspath.sh
-rw-r----- 1 root root 2020 Aug 10 2021 shutdown.bat
-rwxr-x--- 1 root root 1902 Aug 10 2021 shutdown.sh #关闭tomcat
-rw-r----- 1 root root 2022 Aug 10 2021 startup.bat
-rwxr-x--- 1 root root 1904 Aug 10 2021 startup.sh #启动tomcat
-rw-r----- 1 root root 51179 Aug 10 2021 tomcat-juli.jar
-rw-r----- 1 root root 425741 Aug 10 2021 tomcat-native.tar.gz
-rw-r----- 1 root root 4574 Aug 10 2021 tool-wrapper.bat
-rwxr-x--- 1 root root 5540 Aug 10 2021 tool-wrapper.sh
-rw-r----- 1 root root 2026 Aug 10 2021 version.bat
-rwxr-x--- 1 root root 1908 Aug 10 2021 version.sh #查看tomcat版本信息
drwx------ 3 root root 254 Aug 3 15:58 conf #tomcat的配置文件目录
[root@localhost conf]# ll
total 224
drwxr-x--- 3 root root 23 Aug 3 15:58 Catalina
-rw------- 1 root root 13053 Aug 10 2021 catalina.policy #项目安全文件,防止欺骗代码,在使用startup.sh启动tomcat时加上-security参数才会生效
-rw------- 1 root root 7661 Aug 10 2021 catalina.properties #配置tomcat启动的相关信息
-rw------- 1 root root 1338 Aug 10 2021 context.xml #监视加载文件,监视的文件发生变化时会自动加载
-rw------- 1 root root 1149 Aug 10 2021 jaspic-providers.xml
-rw------- 1 root root 2313 Aug 10 2021 jaspic-providers.xsd
-rw------- 1 root root 3916 Aug 10 2021 logging.properties #tomcat日志文件的配置,格式、级别等等
-rw------- 1 root root 7589 Aug 10 2021 server.xml #tomcat的核心配置文件,端口号等
-rw------- 1 root root 2756 Aug 10 2021 tomcat-users.xml #tomcat管理用户配置文件
-rw------- 1 root root 2558 Aug 10 2021 tomcat-users.xsd
-rw------- 1 root root 171879 Aug 10 2021 web.xml #web应用相关的通用设置,过滤器、过期时间等等
drwxr-x--- 2 root root 4096 Aug 3 15:57 lib #存放web调用依赖的jar包目录
[root@localhost lib]# ll
total 8728
-rw-r----- 1 root root 12353 Aug 10 2021 annotations-api.jar
-rw-r----- 1 root root 54385 Aug 10 2021 catalina-ant.jar
-rw-r----- 1 root root 121474 Aug 10 2021 catalina-ha.jar
-rw-r----- 1 root root 1723228 Aug 10 2021 catalina.jar
-rw-r----- 1 root root 78022 Aug 10 2021 catalina-storeconfig.jar
-rw-r----- 1 root root 295660 Aug 10 2021 catalina-tribes.jar
-rw-r----- 1 root root 2450404 Aug 10 2021 ecj-4.6.3.jar
-rw-r----- 1 root root 89944 Aug 10 2021 el-api.jar
-rw-r----- 1 root root 169227 Aug 10 2021 jasper-el.jar
-rw-r----- 1 root root 604688 Aug 10 2021 jasper.jar
-rw-r----- 1 root root 26847 Aug 10 2021 jaspic-api.jar
-rw-r----- 1 root root 61960 Aug 10 2021 jsp-api.jar
-rw-r----- 1 root root 249408 Aug 10 2021 servlet-api.jar
-rw-r----- 1 root root 10652 Aug 10 2021 tomcat-api.jar
-rw-r----- 1 root root 886569 Aug 10 2021 tomcat-coyote.jar
-rw-r----- 1 root root 285690 Aug 10 2021 tomcat-dbcp.jar
-rw-r----- 1 root root 73356 Aug 10 2021 tomcat-i18n-de.jar
-rw-r----- 1 root root 105580 Aug 10 2021 tomcat-i18n-es.jar
-rw-r----- 1 root root 157693 Aug 10 2021 tomcat-i18n-fr.jar
-rw-r----- 1 root root 178458 Aug 10 2021 tomcat-i18n-ja.jar
-rw-r----- 1 root root 177051 Aug 10 2021 tomcat-i18n-ko.jar
-rw-r----- 1 root root 46527 Aug 10 2021 tomcat-i18n-ru.jar
-rw-r----- 1 root root 161744 Aug 10 2021 tomcat-i18n-zh-CN.jar
-rw-r----- 1 root root 149149 Aug 10 2021 tomcat-jdbc.jar
-rw-r----- 1 root root 34876 Aug 10 2021 tomcat-jni.jar
-rw-r----- 1 root root 183139 Aug 10 2021 tomcat-util.jar
-rw-r----- 1 root root 214911 Aug 10 2021 tomcat-util-scan.jar
-rw-r----- 1 root root 236457 Aug 10 2021 tomcat-websocket.jar
-rw-r----- 1 root root 38466 Aug 10 2021 websocket-api.jar
drwxr-x--- 2 root root 4096 Aug 6 16:09 logs #存放tomcat运行时日志文件,会输出到cataline.out中
[root@localhost logs]# ll
total 52
-rw-r----- 1 root root 6156 Aug 3 15:58 catalina.2023-08-03.log #以这种形式组成的是windows写=下日志文件输出的内容
-rw-r----- 1 root root 9528 Aug 6 16:09 catalina.2023-08-06.log
-rw-r----- 1 root root 15690 Aug 6 16:09 catalina.out
-rw-r----- 1 root root 0 Aug 3 15:58 host-manager.2023-08-03.log #这种形式是访问webapps下下的host-manager项目日志
-rw-r----- 1 root root 0 Aug 6 16:09 host-manager.2023-08-06.log
-rw-r----- 1 root root 459 Aug 3 15:58 localhost.2023-08-03.log #这种形式是只记录tomcat访问日志
-rw-r----- 1 root root 735 Aug 6 16:09 localhost.2023-08-06.log
-rw-r----- 1 root root 738 Aug 3 16:00 localhost_access_log.2023-08-03.txt #这种形式是访问tomcat下所有项目日志记录
-rw-r----- 1 root root 72 Aug 6 16:10 localhost_access_log.2023-08-06.txt
-rw-r----- 1 root root 0 Aug 3 15:58 manager.2023-08-03.log #这种形式是访问webapps下manager项目日志
-rw-r----- 1 root root 0 Aug 6 16:09 manager.2023-08-06.log
drwxr-x--- 2 root root 30 Aug 3 15:57 temp #存放临时文件,可以删除
drwxr-x--- 7 root root 81 Aug 10 2021 webapps #web程序存放目录,默认有5个项目,支持文件夹、war包、jar包形式
[root@localhost webapps]# ll
total 4
drwxr-x--- 15 root root 4096 Aug 3 15:57 docs
drwxr-x--- 7 root root 99 Aug 3 15:57 examples
drwxr-x--- 6 root root 79 Aug 3 15:57 host-manager
drwxr-x--- 6 root root 114 Aug 3 15:57 manager
drwxr-x--- 3 root root 223 Aug 3 15:57 ROOT
drwxr-x--- 3 root root 22 Aug 3 15:58 work #存放因编译产生的.java和.class文件,重启tomcat可以进行清理
[root@localhost logs]# tail -f /usr/local/tomcat/apache-tomcat-8.5.70/logs/catalina.out
06-Aug-2023 22:34:50.772 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/docs]
06-Aug-2023 22:34:50.783 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/docs] has finished in [11] ms
06-Aug-2023 22:34:50.783 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/examples]
06-Aug-2023 22:34:50.913 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/examples] has finished in [130] ms
06-Aug-2023 22:34:50.913 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/host-manager]
06-Aug-2023 22:34:50.926 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/host-manager] has finished in [13] ms
06-Aug-2023 22:34:50.927 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/manager]
06-Aug-2023 22:34:50.941 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/manager] has finished in [14] ms
06-Aug-2023 22:34:50.948 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
06-Aug-2023 22:34:50.954 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 359 ms
这里我的其实还不算耗时,如果启动时间很长,是session引起的随机数问数量导致的,Tocmat的Session ID是通过SHA1算法计算得到的,计算Session ID的时候必须有一个密钥。为了提高安全性Tomcat在启动的时候会通过随机生成一个密钥。
[root@localhost ~]# cat /proc/sys/kernel/random/entropy_avail
1091
[root@localhost ~]# yum install -y rng-tools
[root@localhost ~]# systemctl start rngd
[root@localhost ~]# systemctl enable rngd
[root@localhost ~]# cat /proc/sys/kernel/random/entropy_avail
3114
[root@localhost ~]# systemctl restart tomcat
[root@localhost tail -f /usr/local/tomcat/apache-tomcat-8.5.70/logs/catalina.out
07-Aug-2023 09:44:39.390 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/docs]
07-Aug-2023 09:44:39.398 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/docs] has finished in [8] ms
07-Aug-2023 09:44:39.399 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/examples]
07-Aug-2023 09:44:39.524 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/examples] has finished in [125] ms
07-Aug-2023 09:44:39.524 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/host-manager]
07-Aug-2023 09:44:39.536 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/host-manager] has finished in [12] ms
07-Aug-2023 09:44:39.536 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/manager]
07-Aug-2023 09:44:39.548 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/apache-tomcat-8.5.70/webapps/manager] has finished in [12] ms
07-Aug-2023 09:44:39.553 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
07-Aug-2023 09:44:39.557 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 339 ms
[root@localhost conf]# pwd
/usr/local/tomcat/apache-tomcat-8.5.70/conf
[root@localhost conf]# vim tomcat-users.xml
#在这个结束标识前添加以上内容
[root@localhost META-INF]# pwd
/usr/local/tomcat/apache-tomcat-8.5.70/webapps/manager/META-INF
[root@localhost META-INF]# vim context.xml #找到以下行,默认是本地地址,可以书写具体地址和网段,也可以通过"^.*$"允许所有主机
[root@localhost META-INF]# pwd
/usr/local/tomcat/apache-tomcat-8.5.70/webapps/host-manager/META-INF
[root@localhost META-INF]# vim context.xml #修改方式同上