一、tomcat基础介绍及安装部署

时间:2024-03-09 11:37:33

一、tomcat介绍

Tomcat服务器是一个免费的开放源代码的Web应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP网页的首选。

Tomcat和Nginx、Apache(httpd)、lighttpd等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Nginx/Apache服务器。

目前Tomcat最新版本为10.0。Java容器还有resin、weblogic等。 Tomcat官网:http://tomcat.apache.org/

JDK (Java Development Kit) ,是 Java 语言的软件开发工具包(SDK)),JDK是整个java开发的核心,它包含了JAVA运行时环境Java Runtime Enviromental,JRE)和 JAVA工具,其中JRE包括(JVM+Java系统类和库);JAVA虚拟机(Java Virtual Mechinal,JVM),JVM是JRE的一部分,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的。

JAVA开发人员通过JDK(调用JAVA API)工具包,开发了JAVA程序(JAVA源码文件)之后,通过JDK中的编译程序(JAVAC)将java文件编译成JAVA字节码,在JRE上运行这些JAVA字节码,JVM解析这些字节码,映射到CPU指令集或OS的系统调用。

二、tomcat的安装部署

1、安装JDK

tomcat是基于java开发的,因此在部署tomcat之前需要先部署jdk

jdk软件可以从清华开源镜像站下载:

https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/11/jdk/x64/linux/OpenJDK11U-jdk_x64_linux_openj9_11.0.6_10_openj9-0.18.1.tar.gz

tomcat软件可以从清华开源镜像站下载:

https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.31/bin/apache-tomcat-9.0.31.tar.gz

 

第一步:下载安装jdk

wget -c https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/11/jdk/x64/linux/OpenJDK11U-jdk_x64_linux_openj9_11.0.6_10_openj9-0.18.1.tar.gz
tar -xf OpenJDK11U-jdk_x64_linux_openj9_11.0.6_10_openj9-0.18.1.tar.gz
mkdir /usr/java
mv jdk8u242-b08 /usr/java/jdk

第二步:配置java环境变量

cat >> /etc/profile.d/java.sh <<EOF
export JAVA_HOME=/usr/java/jdk
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib  
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH 
EOF

source /etc/profile

2、安装tomcat

第一步:安装部署tomcat

下载tomcat-9

wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.31/bin/apache-tomcat-9.0.31.tar.gz

tar -xf apache-tomcat-9.0.31.tar.gz

mv apache-tomcat-9.0.31 /usr/local/tomcat

第二步:启动tomcat

 
cd /usr/local/tomcat/bin
./start.sh

[root@inode3 tools]# ps -ef |grep tomcat
root  32833  1  0 05:24 ?00:02:32 /usr/java/jdk/bin/java -Djava.util.logging.config.file=/usr/local/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 /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root  33360  33343  0 05:31 ?00:00:55 /usr/local/openjdk-8/bin/java -Djava.util.logging.config.file=/usr/local/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 /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root  77943  73370  0 19:36 pts/000:00:00 grep --color=auto tomcat
[root@inode3 ~]# netstat -antlp|grep java
tcp6   0  0 127.0.0.1:8005  :::*LISTEN  78104/java  
tcp6   0  0 :::8080 :::*LISTEN  78104/java 

浏览器访问

三、tomcat的目录结构

[root@tomcat ~]# cd /usr/local/tomcat/
[root@tomcat tomcat]# tree -L 1
.
├── bin       #→用以启动、关闭Tomcat或者其它功能的脚本(.bat文件和.sh文件)
├── conf      #→用以配置Tomcat的XML及DTD文件
├── lib       #→存放web应用能访问的JAR包
├── LICENSE
├── logs      #→Catalina和其它Web应用程序的日志文件
├── NOTICE
├── RELEASE-NOTES
├── RUNNING.txt
├── temp     #→临时文件
├── webapps   #→Web应用程序根目录
└── work      #→用以产生有JSP编译出的Servlet的.java和.class文件
7 directories, 4 files

[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ll
total 20
drwxr-xr-x 14 root root 4096 Oct 5 12:09 docs         #→tomcat帮助文档
drwxr-xr-x 6 root root 4096 Oct 5 12:09 examples      #→web应用实例
drwxr-xr-x 5 root root 4096 Oct 5 12:09 host-manager  #→管理
drwxr-xr-x 5 root root 4096 Oct 5 12:09 manager       #→管理
drwxr-xr-x 3 root root 4096 Oct 5 12:09 ROOT          #→默认网站根目录

四、tomcat的启动与关闭

1、启动程序

[root@tomcat ~]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat ~]# netstat -tunlp|grep java
tcp 0 0 :::8009 :::* LISTEN 4743/java
tcp 0 0 :::8080 :::* LISTEN 4743/java

[root@tomcat ~]# ps -ef|grep [j]ava
root 4743 1 17 06:10 pts/0 00:00:03 /application/jdk/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat/endorsed -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start

2、关闭程序

[root@inode3 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/java/jdk
Using CLASSPATH:   /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

3、普通用户启动tomcat

#1、创建普通用户java
useradd -r java
#2、使用java用户启动tomcat
chown -R java.java /usr/local/apache-tomcat-8.5.42
su - java -c "/usr/local/tomcat/bin/catalina.sh start"

[root@node1 tomcat]# ps -ef |grep tomcat
java     14917     1 12 04:33 ?        00:00:02 /usr/java/default/bin/java -Djava.util.logging.config.file=/usr/local/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 /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/localtomcat/temp org.apache.catalina.startup.Bootstrap start

五、tomcat的日志

 
[root@inode3 logs]# tail -fn 10 catalina.out 
10-Mar-2020 21:57:49.363 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [450] ms
10-Mar-2020 21:57:49.363 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
10-Mar-2020 21:57:49.421 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [58] ms
10-Mar-2020 21:57:49.422 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
10-Mar-2020 21:57:49.471 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [50] ms
10-Mar-2020 21:57:49.485 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
10-Mar-2020 21:57:49.515 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1,106] milliseconds