简介
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
诀窍是,当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为10.1。
tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。
tomcat应用场景:lnmt
Tomcat 部署
//java环境安装
[root@localhost ~]# yum -y install java-17-openjdk*
//下载tomcat包
[root@localhost ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz
--2021-10-17 06:52:55-- https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz
正在解析主机 archive.apache.org (archive.apache.org)... 138.201.131.134, 2a01:4f8:172:2ec5::2
正在连接 archive.apache.org (archive.apache.org)|138.201.131.134|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:11576317 (11M) [application/x-gzip]
正在保存至: “apache-tomcat-9.0.54.tar.gz”
apache-tomcat-9.0.54. 100%[=========================>] 11.04M 76.0KB/s 用时 3m 0s
2021-10-17 06:55:57 (62.6 KB/s) - 已保存 “apache-tomcat-9.0.54.tar.gz” [11576317/11576317])
[root@localhost ~]#
//解压 做软连接
[root@localhost ~]# ls /usr/local/
apache-tomcat-9.0.54 bin etc games include lib lib64 libexec sbin share src
[root@localhost ~]#
[root@localhost local]# ln -sv apache-tomcat-9.0.54 tomcat
'tomcat' -> 'apache-tomcat-9.0.54'
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 9 root root 220 10月 17 06:56 apache-tomcat-9.0.54
drwxr-xr-x. 2 root root 6 5月 18 2020 bin
drwxr-xr-x. 2 root root 6 5月 18 2020 etc
drwxr-xr-x. 2 root root 6 5月 18 2020 games
drwxr-xr-x. 2 root root 6 5月 18 2020 include
drwxr-xr-x. 2 root root 6 5月 18 2020 lib
drwxr-xr-x. 3 root root 17 10月 14 02:12 lib64
drwxr-xr-x. 2 root root 6 5月 18 2020 libexec
drwxr-xr-x. 2 root root 6 5月 18 2020 sbin
drwxr-xr-x. 5 root root 49 10月 14 02:12 share
drwxr-xr-x. 2 root root 6 5月 18 2020 src
lrwxrwxrwx. 1 root root 20 10月 17 06:57 tomcat -> apache-tomcat-9.0.54
//家目录下创建index.jsp 用来创建java默认页面
[root@localhost ~]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hello yaya");
%>
</body>
</html>
//创建网站位置/usr/local/tomcat/webapps/test
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/test
//复制index.jsp到/usr/local/tomcat/webapps/test中
[root@localhost ~]# cp index.jsp /usr/local/tomcat/webapps/test/
[root@localhost ~]# ls /usr/local/tomcat/webapps/test/
index.jsp
//启动tomcat
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
####8005是停止tomcat用的,8080是tomcat默认端口
//配置文件
[root@localhost ~]# cd /usr/local/tomcat/
[root@localhost tomcat]# ls
bin conf lib logs README.md RUNNING.txt webapps(项目存放位置)
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@localhost tomcat]# ls webapps/
docs examples host-manager manager ROOT(默认网页存放位置) test
[root@localhost tomcat]# ls bin
bootstrap.jar configtest.sh shutdown.sh(停止tomcat脚本)
catalina.bat (.bat结尾是windows批处理脚本) daemon.sh startup.bat(启动tomcat脚本)
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
[root@localhost tomcat]#
Tomcat管理界面的配置
当我们点击默认页面的右边的第一个按钮Server Status时,出现以下错误,所以我们需要修改配置文件
将指定内容加入到指定路径 conf/tomcat-users.xml 的文件中
将下面两行加入到tomcat-users.xml文件</tomcat-users>上面,然后passowrd可以自己进行修改
[root@localhost ~]# cd /usr/local/tomcat/
[root@localhost tomcat]# ls conf/
Catalina context.xml logging.properties tomcat-users.xsd
catalina.policy jaspic-providers.xml server.xml web.xml
catalina.properties jaspic-providers.xsd tomcat-users.xml
[root@localhost tomcat]# cd conf/
[root@localhost conf]# vim tomcat-users.xml
55 -->
56 <role rolename="manager-gui"/>
57 <user username="tomcat" password="1" roles="manager-gui"/>
58 </tomcat-users>
[root@localhost ~]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
21 <Valve className="org.apache.catalina.valves.RemoteAddrValve"
22 allow="192.\168.\235\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
重启tomcat
[root@localhost ~]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost ~]# ss -antl // 查看端口是否停止,还有查看进程是否彻底杀死,因为tomcat可能假死
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ps -aux |grep tomcat
root 28667 0.0 0.0 12320 1080 pts/0 S+ 07:28 0:00 grep --color=auto tomcat
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
再次访问点击Server Status(服务器状态) 和 Manager APP(管理应用程序) 输入上面配置文件中设置的用户名和密码登录
在真正使用中,图片中的JVM虚拟机是需要设置的,在/usr/local/tomcat/bin/catalina.sh中做限制,不然一直用,把内存用完就会出现内存溢出(OOM),宕机卡死
然后还需要配置第三个按钮,Host Manager(主机管理)
[root@localhost ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
-->
<role rolename="admin-gui"/> //添加这行
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/> //添加admin-gui
</tomcat-users>
[root@localhost ~]# vim /usr/local/tomcat/webapps/host-manager/META-INF/context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.235\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
//重启tomcat
[root@localhost ~]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ps -aux |grep tomcat
root 71772 0.0 0.0 12320 976 pts/0 S+ 07:51 0:00 grep --color=auto tomcat
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
如果需要在一台主机中部署多个tomcat,需要修改配置文件中的端口来使用不同的端口在/usr/local/tomcat/conf/server.xml文件