初识JAVA环境,3大必会内容:JRE、JDK、JVM,下面我们来说说他们的区别与特点吧。
为何用JVM
开发人员正常书写的程序代码,只能在某个平台使用,Linux和Windows软件包是互不通用的,如果把window QQ.exe上传到Linux下面则无法安装与使用。
但是有了JVM,Java环境只要书写一份Java代码,把代码放在Java虚拟机中运行,只要Java虚拟机可以运行,Java代码就可以正常运行。
Tomcat快速实战指南
| 环境准备
| JDK、Tomcat版本选型
jdk版本一般根据开发使用的版本为准,这里我们选择的是1.8.0。
jdk(Oracle官方)
rmp包安装
二进制包(软件绿色版,解压即用)
源码(编译安装)
openJDK Linux
yum(yum list |grep -i openjdk)
tomcat 9.0 8.5 8.0 7.x
apache-tomcat
| 极速部署
#oldboy-web-java-01
##jdk
#各种版本:https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/
####01 部署 jdk
[root@oldboy-web-java-01 ~]# tar xf /app/tools/jdk-8u241-linux-x64.tar.gz -C /app/
[root@oldboy-web-java-01 ~]# ln -s /app/jdk1.8.0_241/ /app/jdk
[root@oldboy-web-java-01 ~]# ll /app
total
lrwxrwxrwx 1 root root 18 Feb 7 10:26 jdk -> /app/jdk1.8.0_241/
drwxr-xr-x 7 10143 10143 245 Dec 11 18:39 jdk1.8.0_241
drwxr-xr-x 2 root root 75 Feb 7 10:13 tools
####02 java jdk 环境变量
cat >>/etc/profile<<'EOF'
export JAVA_HOME=/app/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/app/tomcat
EOF
#说明部分
##export JAVA_HOME=/app/jdk #jdk目录
##export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH #配置命令路径
##export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
##export TOMCAT_HOME=/app/tomcat #tomcat 目录
[root@oldboy-web-java-01 ~]# . /etc/profile #或者source /etc/profile
####03.检查 jdk是否部署完成
[root@oldboy-web-java-01 ~]# java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8._241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
#####04.部署 tomcat
[root@oldboy-web-java-01 ~]# tar xf /app/tools/apache-tomcat-8.5.50.tar.gz -C /app/
[root@oldboy-web-java-01 ~]# ln -s /app/apache-tomcat-8.5.50/ /app/tomcat
####05 检查 jdk+tomcat
[root@oldboy-web-java-01 ~]# /app/tomcat/bin/version.sh
Using CATALINA_BASE: /app/tomcat
Using CATALINA_HOME: /app/tomcat
Using CATALINA_TMPDIR: /app/tomcat/temp
Using JRE_HOME: /app/jdk
Using CLASSPATH: /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.50
Server built: Dec 7 2019 19:19:46 UTC
Server number: 8.5.50.0
OS Name: Linux
OS Version: 4.18.-80.11.2.el8_0.x86_64
Architecture: amd64
JVM Version: 1.8._241-b07
JVM Vendor: Oracle Corporation
Web访问 10.0.0.0:8080 进行访问与测试
| Tomcat目录结构
[root@web01 tomcat]# ll
total 92
drwxr-xr-x 2 root root 4096 Dec 16 12:01 bin
drwxr-xr-x 3 root root 198 Dec 16 12:08 conf
drwxr-xr-x 2 root root 4096 Dec 16 12:01 lib #库文件 tomcat插件
drwxr-xr-x 2 root root 197 Dec 16 12:08 logs
-rw-r--r-- 1 root root 1444 Sep 28 2015 NOTICE
-rw-r--r-- 1 root root 6741 Sep 28 2015 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 Sep 28 2015 RUNNING.txt
drwxr-xr-x 2 root root 30 Dec 16 12:01 temp
drwxr-xr-x 7 root root 81 Sep 28 2015 webapps
drwxr-xr-x 3 root root 22 Dec 16 12:08 work
小试牛刀-部署简单Java页面
#应用war包 部署到 /app/tomcat/webapps/
#tomcat 自动解压
#tomcat 自动部署
日志信息:
26-Aug-2021 10:58:13.011 信息 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployWAR 正在部署web应用程序存档文件[/app/apache-tomcat-9.0.52/webapps/memtest.war]
26-Aug-2021 10:58:13.089 信息 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployWAR web应用程序存档文件[/app/apache-tomcat-9.0.52/webapps/memtest.war]的部署已在[77]ms内完成
Tomcat管理端
| Tomcat管理端应用场景
| 极速上手指南
注意事项(tomcat 9.0)
tomcat 8.5 对管理端限制更严格;
要配置tomcat-user.xml之外;
还限制只能在本地使用127.0.0.1访问管理的;
从tomcat8.5开始,管理端默认只能通过本地使用127.0.0.1访问(类似于nginx all 127.0.0.1 ; deny all;)
默认情况下,只能从与Tomcat运行在同一台计算机上的浏览器访问管理器。如果要修改此限制,则需要编辑管理器的context.xml文件;
## 01 配置 tomcat 管理端
[root@static01 tomcat]# cat conf/tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="1" roles="manager-gui,admin-gui"/>
</tomcat-users>
## 02 重启tomcat
## 03 命令行测试
curl -u tomcat:1 http://127.0.0.1:8080/manager/status
#注意:使用127.0.0.1
## 04 开启tomcat默认的限制
/app/tomcat/webapps/host-manager/META-INF/context.xml
/app/tomcat/webapps/manager/META-INF/context.xml
[root@static01 tomcat]# sed -i.bak 's#127#\\d+#g' /app/tomcat/webapps/host-manager/META-INF/context.xml /app/tomcat/webapps/manager/META-INF/context.xml
[root@static01 tomcat]# find -name "context.xml" |xargs grep allow
./webapps/host-manager/META-INF/context.xml: allow="\d+\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
./webapps/manager/META-INF/context.xml: allow="\d+\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
| 检查结果
Tomcat日志与进程信息
| 进程信息
#ps命令查询 java进程信息
[root@oldboy-web-java-01 ~]# ps -ef |grep java
root 4112 1 1 11:43 pts/1 00:00:15 /app/jdk/bin/java -Djava.util.logging.config.file=/app/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/app/tomcat -Dcatalina.home=/app/tomcat -Djava.io.tmpdir=/app/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 4199 4175 11:59 pts/ 00:00:00 grep --color=auto java
[root@oldboy-web-java-01 ~]#
/app/tomcat/bin/startup.sh
/app/jdk/bin/java
-Djava.util.logging.config.file=/app/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djdk.tls.ephemeralDHKeySize=2048
-Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027
-Dignore.endorsed.dirs=
-classpath /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar
-Dcatalina.base=/app/tomcat
-Dcatalina.home=/app/tomcat
-Djava.io.tmpdir=/app/tomcat/temp org.apache.catalina.startup.Bootstrap
start
/app/tomcat_8081
/app/tomcat_8082
| tomcat日志
catalina.out 主日志
###catalina.out
# error 错误
# failed
# exception 异常
# startup 或 finished 启动所需的时间
# deploy
07-Feb-2020 12:03:16.039 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
07-Feb-2020 12:03:16.039 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
07-Feb-2020 12:03:16.046 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
07-Feb-2020 12:03:16.051 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
07-Feb-2020 12:03:16.093 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
07-Feb-2020 12:03:16.098 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
07-Feb-2020 12:03:16.100 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
07-Feb-2020 12:03:16.101 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
访问日志
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /tomcat.css HTTP/1.1" 200 5581
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /tomcat.png HTTP/1.1" 200 5103
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /bg-nav.png HTTP/1.1" 200 1401
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /asf-logo-wide.svg HTTP/1.1" 200 27235
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /bg-upper.png HTTP/1.1" 200 3103
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /bg-middle.png HTTP/1.1" 200 1918
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /bg-button.png HTTP/1.1" 200 713
223.104.2.165 - - [07/Feb/2020:12:10:00 +08] "GET /favicon.ico HTTP/1.1" 200 21630
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET / HTTP/1.1" 200 11215
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /tomcat.css HTTP/1.1" 200 5581
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /tomcat.png HTTP/1.1" 200 5103
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /bg-nav.png HTTP/1.1" 200 1401
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /asf-logo-wide.svg HTTP/1.1" 200 27235
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /bg-upper.png HTTP/1.1" 200 3103
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /bg-button.png HTTP/1.1" 200 713
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /bg-middle.png HTTP/1.1" 200 1918
223.104.2.165 - - [07/Feb/2020:12:10:06 +08] "GET /favicon.ico HTTP/1.1" 200 21630
223.104.2.165 - - [07/Feb/2020:12:10:11 +08] "GET / HTTP/1.1" 200 11215
223.104.2.165 - - [07/Feb/2020:12:10:11 +08] "GET /tomcat.css HTTP/1.1" 304 -
223.104.2.165 - - [07/Feb/2020:12:10:12 +08] "GET /tomcat.png HTTP/1.1" 304 -
223.104.2.165 - - [07/Feb/2020:12:10:12 +08] "GET /asf-logo-wide.svg HTTP/1.1" 304 -
223.104.2.165 - - [07/Feb/2020:12:10:12 +08] "GET /bg-nav.png HTTP/1.1" 304 -
223.104.2.165 - - [07/Feb/2020:12:10:12 +08] "GET /bg-upper.png HTTP/1.1" 304 -
223.104.2.165 - - [07/Feb/2020:12:10:12 +08] "GET /bg-button.png HTTP/1.1" 304 -
223.104.2.165 - - [07/Feb/2020:12:10:12 +08] "GET /bg-middle.png HTTP/1.1" 304 -
catalina.out和catalina.日期.log。
catalina.out在被切割后,内容不会被清空。
| Tomcat日志格式
tomcat日志格式
#修改tomcat访问日志格式
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
</Host>
</Engine>
</Service>
#server.xml
#端口部分
###8005 shutdown端口
22 行: <Server port="8005" shutdown="SHUTDOWN">
###tomcat shutdown端口 telnet/nc 连接到这个端口 输入暗号 tomcat将会关闭
###8080 http协议端口
69 <Connector port="8080" protocol="HTTP/1.1"
70 connectionTimeout="20000"
71 redirectPort="8443" />
###8009 ajp协议端口 与apache连接使用
115
116
#8009端口 是用来给 apache与tomcat进行连接使用
#现在 tomcat+nginx 可以把 这一行8009注释 提高tomcat性能
116 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
提问:tomcat默认有几个端口 及作用 ?
#tomcat管理端 相应的配置
##管理端 实际生产环境 关闭
37 <GlobalNamingResources>
38
41 <Resource name="UserDatabase" auth="Container"
42 type="org.apache.catalina.UserDatabase"
43 description="User database that can be updated and saved"
44 factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
#指定管理端 密码文件
45 pathname="conf/tomcat-users.xml" />
46 </GlobalNamingResources>
#配置 tomcat 虚拟主机的内容
Nginx tomcat
Server_name Host name 域名
root appBase 站点目录
#unpackWARs #自动解压war包
#autoDeploy 自动部署 把代码加载到jvm内存中
148 <Host name="localhost" appBase="webapps"
149 unpackWARs="true" autoDeploy="true">
150
151
153
156
157
#配置 日志
160 <Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
#日志文件 前缀是localhost_access_log #日志后缀
161 prefix="access" suffix=".log"
#日志 里面的格式 " html语言中的 双引号
#日志内容 类似于 log_format
162 pattern="%h %l %u %t "%r" %s %b" />
163
164 </Host>
#%h 客户端ip地址或者是域名
#%l (小写L) 远程用户
#%u 用户 Remote user that was authenticated (if any), else '-' (escaped if required)
#%t 时间 日期和时间
#" 双引号
#%r 请求起始行 $request
#%s $status 状态码
#%b 大小
Tomcat配置文件
[root@web03 /app/tomcat]# sed '//d;/^$/d;//d' conf/server.xml
<?xml version="1.0" encoding="UTF-8"?>
#8005端口是 shutdown端口 shutdown="" #关闭tomcat暗号
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
#tomcat 管理端配置 开始
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
#tomcat 管理端配置 结束
<Service name="Catalina">
#连接器 用户请求通过连接器 进入tomcat,然后经过tomcat处理.
#8080是tomcat默认的web服务的端口
#8443tomcat+https
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /> #配置tomcat https
# defaultHost tomcat默认的主机(网站)
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
# Host name 域名 lidao.oldboylinux.com zrlog.oldboylinux.com ..
# appBase=webapps 网站默认的站点目录(网站的代码)
# Host name="zrlog.oldboylinux.com" appBase="/code/zrlog"
# unpackWARs 自动解压war包
# autoDeploy 自动部署
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
# 配置tomcat的访问日志.
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
# 配置访问日志格式
#prefix 和suffix 日志文件名的格式
prefix="localhost_access_log" suffix=".txt"
#日志里面的内容
pattern="%h %l %u %t "%r" %s %b %D " />
</Host>
</Engine>
</Service>
</Server>
部署Tomcat多虚拟主机
| 环境准备
1、虚拟主机;2、一个网站;
#目标
浏览器访问 img.etiantian.org 显示 img 内容
浏览器访问 live.etiantian.org 显示 live 内容
浏览器访问 其他 。。。。 显示localhost默认内容
| 配置与调试
mkdir -p /data/{live,img}/ROOT/ /var/log/tomcat
echo live >/data/live/ROOT/index.jsp
echo img > /data/img/ROOT/index.jsp
#tomcat访问的时候 uri小坑
### 用户访问的域名如果包含路径(目录) tomcat会在站点目录下面匹配指定的目录然后匹配文件
curl img.etiantian.org/oldboy/index.jsp --->站点目录下面的/oldboy/index.jsp
### 用户访问的url没有路径、目录 tomcat会在站点目录下面找ROOT/内容 进行匹配
curl img.etiantian.org/index.jsp ---> 站点目录下面的/ROOT/index.jsp
#tomcat虚拟主机
#01 第1部分 设置默认的虚拟主机
<Engine name="Catalina" defaultHost="localhost">
#02 第2个部分 虚拟主机的配置部分
#Host name="域名/localhost"
#appBase="站点目录" 网站程序代码存放的目录
#/code/live
#/code/img
#/code/blog
#/code/...
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
#directory 日志目录 /var/log/tomcat/
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
#prefix="域名_access" suffix=".log"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
</Host>
<Host name="live.oldboylinux.com" appBase="/code/live"
unpackWARs="true" autoDeploy="true">
#directory 日志目录 /var/log/tomcat/
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat/"
#prefix="域名_access" suffix=".log"
prefix="llive.oldboylinux.com_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
</Host>
配置了2个虚拟主机的tomcat
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat/"
prefix="localhost_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
</Host>
<Host name="live.oldboylinux.com" appBase="/code/live"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat/"
prefix="live_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
</Host>
#重启后检查结果
[root@web03 /app/tomcat]# ll /var/log/tomcat/
total
-rw-r----- 1 root root Aug 8 12:17 live_access.2021-08-08.log
-rw-r----- 1 root root Aug 8 12:17 localhost_access.2021-08-08.log
#配置hosts解析
linux vim /etc/hosts
10.0.0.9 live.oldboylinux.com
windows : C:\Windows\System32\drivers\etc\hosts
10.0.0.9 live.oldboylinux.com
#测试 live站点
[root@web03 /app/tomcat]# mkdir /code/live/ROOT
[root@web03 /app/tomcat]# echo live.oldboylinux.com web03 /code/live/ROOT/index.jsp
live.oldboylinux.com web03 /code/live/ROOT/index.jsp
[root@web03 /app/tomcat]# echo live.oldboylinux.com web03 > /code/live/ROOT/index.jsp
[root@web03 /app/tomcat]#
[root@web03 /app/tomcat]# curl live.oldboylinux.com:8080
live.oldboylinux.com web03
#测试 默认站点
echo this is default virtual host server > /app/tomcat/webapps/ROOT/index.jsp
[root@web03 /app/tomcat]# curl 10.0.0.9:8080
this is default virtual host server
[root@web03 /app/tomcat]# curl -H Host:lidao.oldboylinux.com 10.0.0.9:8080
this is default virtual host server
[root@web03 /app/tomcat]#
| 重看-tomcat处理请求流程
用户发出http请求报文: Host: 域名 /域名+端口;
用请求到达tomcat, tomcat,connector 端口是否存在,不存在就拒绝,存在就继续处理;
tomcat继续根据用户的请求的域名匹配站点 用户请求的域名Host与Tomcat Host name进行匹配;
tomcat匹配成功,则让用户的请求到达这个站点的对应的站点目录 appBase指定的;
tomcat匹配失败,则让用户的请求到达默认的站点进行处理。
Java应用部署方式
| 部署应用方式
如果开发给你的是war包,则把war包放入到 tomcat webapps自动解压、自动部署,开发给你源代码---maven-->war包;
如果开发给你的是jar包,相当于jar包里面已经集成了tomcat。
| jar包运行
#jar运行演示
java -jar dingding-sonar-1.0-SNAPSHOT.jar --server.port=8082
| Java开源软件-war包
| 部署应用-zrlog
#01 tomcat
<Host name="zrlog.etiantian.org" appBase="/data/blog/"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tomcat/"
prefix="zrlog_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b %D "%{Referer}i" "%{User-Agent}i"" />
</Host>
#02 数据库
yum install -y mariadb-server
systemctl enable mariadb
systemctl start mariadb
#初始的配置
mysql_secure_installation
Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
... Success!
Disallow root login remotely? [Y/n] Y
... Success!
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
... Success!
Thanks for using MariaDB!
#进入数据库
##查看数据库
show databases;
##查看数据库用户信息
select user,host from mysql.user;
##创建zrlog用的数据库
MariaDB [(none)]> create database zrlog charset utf8;
Query OK, 1 row affected (.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zrlog |
+--------------------+
4 rows in set (.00 sec)
##创建用户 admin 管理所有数据库
grant all on *.* to zrlog@'localhost' identified by '1';
grant all on *.* to zrlog@'%' identified by '1';
#03 .部署应用
把代码复制到/code/zrlog
自动解压,部署
#04. 配置hosts解析
10.0.0.9 zrlog.oldboylinux.com
05: 接上面, web页面部署zrlog;
06: zrlog 连接数据库;
07: 检查 数据库内容(了解) 与用户是上传内容(必会)
select * from zrlog.log ;
find /code/zrlog/ -type f -mmin -100
#部署应用后 tomcat的日志
[WARN] 2021-06-02 11:08:46,902 com.zrlog.web.config.ZrLogConfig configPlugin - Not found lock file(/app/apache-tomcat-8.5.66/webapps/zrlog/WEB-INF/install.lock), Please visit the http://yourHostName:port/zrlog/install start installation
02-Jun-2021 11:08:47.203 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/app/apache-tomcat-8.5.66/webapps/zrlog.war] has finished in [2,682] ms
#准备数据库
create database zrlog charset utf8;
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| all | % |
| root | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | db01 |
| root | localhost |
| root | web01 |
+------+-----------+
7 rows in set (0.00 sec)
#web03 安装应用 让应用连接 数据库
web页面安装zrlog;
检查数据库连接文件和用户上传的目录:
#数据库连接
[root@web03 /app/tomcat/webapps/zrlog]# cat ./WEB-INF/db.properties
#This is a database configuration file
#Wed Jun 02 11:14:44 CST 2021
driverClass=com.mysql.cj.jdbc.Driver
user=all
password=123456
jdbcUrl=jdbc\:mysql\://172.16.1.51\:3306/zrlog?characterEncoding\=UTF-8&allowPublicKeyRetrieval\=true&useSSL\=false&serverTimezone\=GMT
#用户上传
http://10.0.0.9:8080/zrlog/attached/image/20210602/20210602111945_677.png
Tomcat 3种工作模式
| IO模型区别
| 查看当前使用的io模型
查看日志
查看管理端
| 修改IO模型
nio
protocol="org.apache.coyote.http11.Http11Nio2Protocol"
#server.xml 中 修改 8080
<Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
connectionTimeout="20000"
redirectPort="8443" />
[root@web03 /app/tomcat]# grep -ni nio2 conf/server.xml
69: <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
查看修改结果-catalina.out日志
17-Dec-2019 10:46:33.106 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio2-8080"]
查看修改结果-tomcat管理端
apr
#安装apr环境
yum -y install apr apr-devel tomcat-native
#修改8080&8009端口对应的server.xml
protocol="org.apache.coyote.http11.Http11Nio2Protocol"
把Nio2 修改为Apr
protocol="org.apache.coyote.http11.Http11AprProtocol"
17-Dec-2019 10:59:41.605 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
17-Dec-2019 10:59:41.621 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 3982 ms
来自:https://mp.weixin.qq.com/s/ZjRFCe3HWaeinC2f9tGcAg