tomcat 简介
Tomcat是一款我们平时开发过程中最常用到的Servlet容器。本系列博客会记录Tomcat(以Tomcat 7为列)的整体架构、核心组件、IO线程模型、请求在Tomcat内部的流转过程以及一些Tomcat调优的相关知识点。力求能达到以下几个目的:
- 更加熟悉Tomcat的工作机制,工作中遇到Tomcat相关问题能够快速定位,从源头来解决;
- 对于一些高并发场景能够对Tomcat进行调优;
- 通过对Tomcat源码的分析,吸收一些Tomcat的设计的理念,应用到自己的软件开发过程中。
tomcat 是什么
tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。
tomcat项目部署
准备工作
// 关闭防火墙和selinux
[root@localhost bin]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost bin]# setenforce 0
安装java环境
[root@localhost ~]# cd /usr/src
[root@localhost src]# ls
debug kernels
[root@localhost src]# yum -y install java-11-openjdk*
// 下载tomcat
[root@localhost src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.53/bin/apache-tomcat-9.0.53.tar.gz
// 解压
[root@localhost src]# tar xf apache-tomcat-9.0.53.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-9.0.53 bin etc games include lib lib64 libexec sbin share src
// 更改名字
[root@localhost local]# mv apache-tomcat-9.0.53 tomcat
// 写一个java页面
[root@localhost local]# cd
[root@localhost ~]# cat > index.jsp <<EOF
> <html>
> <head>
> <title>test page</title>
> </head>
> <body>
> <%
> out.println("Hello Java");
> %>
> </body>
> </html>
> EOF
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/amu
[root@localhost ~]# cp index.jsp /usr/local/tomcat/webapps/amu/
// 启动、停止tomcat脚本
[root@localhost ~]# cd /usr/local/tomcat/bin/
[root@localhost bin]# ls
bootstrap.jar makebase.sh
catalina-tasks.xml setclasspath.bat
catalina.bat setclasspath.sh
catalina.sh shutdown.bat
ciphers.bat shutdown.sh
ciphers.sh startup.bat
commons-daemon-native.tar.gz startup.sh
commons-daemon.jar tomcat-juli.jar
configtest.bat tomcat-native.tar.gz
configtest.sh tool-wrapper.bat
daemon.sh tool-wrapper.sh
digest.bat version.bat
digest.sh version.sh
makebase.bat
// 启动tomcat
[root@localhost bin]# /usr/local/tomcat/bin/startup.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 bin]# 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 *:* //必须有8085端口才可以用脚本停止服务
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
查看web页面
web页面管理
// 修改配置文件
[root@localhost bin]# cd /usr/local/tomcat/conf/
[root@localhost conf]# ls
Catalina jaspic-providers.xml tomcat-users.xml
catalina.policy jaspic-providers.xsd tomcat-users.xsd
catalina.properties logging.properties web.xml
context.xml server.xml
[root@localhost conf]# vim tomcat-users.xml
// 末尾加三行
[root@localhost conf]# tail -5 tomcat-users.xml
-->
<role rolename="admin-gui"/> // 加这一行
<role rolename="manager-gui"/> // 加这一行
<user username="tomcat" password="tomcat" roles="manager-gui, admin-gui"/> // 加这一行
</tomcat-users>
[root@localhost conf]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# ls
ROOT amu docs examples host-manager manager
[root@localhost webapps]# cd manager/
[root@localhost manager]# ls
META-INF css index.jsp xform.xsl
WEB-INF images status.xsd
[root@localhost manager]# vim META-INF/context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.91\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> // 加入本机网段
<Manager sessionAttributeValueClassNameFilter="java\.lang\
// 重启服务
[root@localhost manager]# /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 manager]# 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 [::]:22 [::]:*
[root@localhost manager]# /usr/local/tomcat/bin/startup.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 manager]# 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 [::]:*
web页面访问
账户和密码都是tomcat
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5615.html