http://tomcat.apache.org/

安装

下载版本选择

做JavaEE开发的朋友,无论是学习者还是已经工作的朋友,总是会用到Tomcat这个Servlet容器,那么大家从Tomcat官网上去下载tomcat的时候总会看到下载列表中有如下内容(下面以下载6.0.43版为例):
Binary Distributions

  • Core:
  • zip (pgp, md5)
  • tar.gz (pgp, md5)
  • 32-bit Windows zip (pgp, md5)
  • 64-bit Windows zip (pgp, md5)
  • 64-bit Itanium Windows zip (pgp, md5)
  • 32-bit/64-bit Windows Service Installer (pgp, md5)
  • Deployer:
  • zip (pgp, md5)
  • tar.gz (pgp, md5)
  • Source Code Distributions

  • tar.gz (pgp, md5)
  • zip (pgp, md5)
  • 看到这里大家知道同一个版本的Tomcat有不同的下载版本,Binary Distributions和Source Code Distributions大家应该能分清楚,分别是二进制版本和源代码本。困惑在于Binary Distributions下面又有Core和Deployer两个,这两个有什么区别?我们到底应该选择哪个呢?
    先说这两者的区别。
    Core:是Tomcat正式的二进制发布版本,一般大家做开发或者学习的时候应该下载Core下的。具体的,在linux下用,下载zip或tar.gz版本;在windows下用,下载win 32或64 zip版,或Windows Service Installer版,zip为绿色版,免安装,解压后即用,Installer版是exe安装程序,需安装后使用。
    Deployer:是基于Tomcat的web应用的发布器,就是在把写好的JavaEE应用发布到Tomcat的时候可以使用Deployer来动态的发布。所以它不是真正的Tomcat二进制版本,它只是一个用以发布基于Tomcat的Web应用的发布工具而已。因此,大家在下载的时候不应该下载这个东西,除非想动态的发布Web应用到Tomcat中去。

    version.sh 查看tomcat版本

    Tomcat 本身提供了查看版本的脚本命令:version
    在 Tomcat 的安装目录的 bin 子目录下,有两个文件:
    version.bat – Windows下的批处理脚本
    version.sh – Linux下的Shell脚本

    # ./version.sh
    Using CATALINA_BASE:   /home/aicu-tob/software/tomcat-bscc-proxy-8888
    Using CATALINA_HOME:   /home/aicu-tob/software/tomcat-bscc-proxy-8888
    Using CATALINA_TMPDIR: /home/aicu-tob/software/tomcat-bscc-proxy-8888/temp
    Using JRE_HOME:        /home/aicu-tob/software/jdk1.8.0_131
    Using CLASSPATH:       /home/aicu-tob/software/tomcat-bscc-proxy-8888/bin/bootstrap.jar:/home/aicu-tob/software/tomcat-bscc-proxy-8888/bin/tomcat-juli.jar
    Server version: Apache Tomcat/9.0.0.M6
    Server built:   May 11 2016 21:43:59 UTC
    Server number:  9.0.0.0
    OS Name:        Linux
    OS Version:     3.10.0-327.el7.x86_64
    Architecture:   amd64
    JVM Version:    1.8.0_131-b11
    JVM Vendor:     Oracle Corporation
    

    Mac 安装 Tomcat

    http://tomcat.apache.org/ 下载 Binary Distributions -> Core -> zip 版最新 Tomcat apache-tomcat-9.0.16.zip
    将解压后的文件夹 apache-tomcat-9.0.16 拖到 ~/Library/ 目录(即 用户名/资源库 目录),执行 bin/startup.sh 启动Tomcat
    若脚本没有执行权限需要先添加可执行权限
    chmod a+x startup.sh
    chmod a+x catalina.sh
    打开 http://localhost:8080 能看到 Tomcat 欢迎页面即安装启动成功。

    Mac Brew 安装 tomcat

    查看可用的 tomcat 安装包
    brew search tomcat

    安装 tomcat
    brew install tomcat
    安装 tomcat@9
    brew install tomcat@9

    Intel Mac 安装目录:
    /usr/local/Cellar/tomcat/9.0.37/libexec
    /usr/local/Cellar/tomcat/10.0.6/libexec

    M1 Mac 安装目录:
    /opt/homebrew/Cellar/tomcat@9/9.0.69
    /opt/homebrew/Cellar/tomcat/10.0.17

    看安装是否成功:

    catalina -h
    Using CATALINA_BASE:   /usr/local/Cellar/tomcat/9.0.37/libexec
    Using CATALINA_HOME:   /usr/local/Cellar/tomcat/9.0.37/libexec
    Using CATALINA_TMPDIR: /usr/local/Cellar/tomcat/9.0.37/libexec/temp
    Using JRE_HOME:        /usr/local/opt/openjdk
    Using CLASSPATH:       /usr/local/Cellar/tomcat/9.0.37/libexec/bin/bootstrap.jar:/usr/local/Cellar/tomcat/9.0.37/libexec/bin/tomcat-juli.jar
    Usage: catalina.sh ( commands ... )
    commands:
      debug             Start Catalina in a debugger
      debug -security   Debug Catalina with a security manager
      jpda start        Start Catalina under JPDA debugger
      run               Start Catalina in the current window
      run -security     Start in the current window with security manager
      start             Start Catalina in a separate window
      start -security   Start in a separate window with security manager
      stop              Stop Catalina, waiting up to 5 seconds for the process to end
      stop n            Stop Catalina, waiting up to n seconds for the process to end
      stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
      stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
      configtest        Run a basic syntax check on server.xml - check exit code for result
      version           What version of tomcat are you running?
    Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
    

    启动 tomcat
    catalina run

    http://localhost:8080/
    能看到 tomcat 介绍页

    webapp的根目录
    /usr/local/Cellar/tomcat/9.0.37/libexec/webapps

    启动关闭tomcat

    启动tomcat

    在windows下可以通过两种方式启动:

    bin/startup.bat
    bin/catalina.bat start
    

    在linix系统下可以通过下面的方式启动:

    bin/startup.sh
    bin/catalina.sh start
    

    startup.sh 和 catalina.sh

    startup.sh 就是 catalina.sh start 后台启动 tomcat
    catalina.sh run 前台启动 tomcat
    catalina.sh start 后台启动 tomcat

    启动后,通过 http://localhost:8080 可以访问tomcat本地首页。

    查看tomcat版本号

    1、tomcat 欢迎页面上看版本号
    2、curl 主页,返回结果中有版本号
    3、找到 tomcat 包下面的 /bin 目录,执行 version.sh 文件。对应的 Server number 即是tomcat所对应的版本信息

    # ./version.sh
    Using CATALINA_BASE:   /root/software/tomcat
    Using CATALINA_HOME:   /root/software/tomcat
    Using CATALINA_TMPDIR: /root/software/tomcat/temp
    Using JRE_HOME:        /opt/jdk1.8.0_101/jre
    Using CLASSPATH:       /root/software/tomcat/bin/bootstrap.jar:/root/software/tomcat/bin/tomcat-juli.jar
    Using CATALINA_OPTS:
    Picked up JAVA_TOOL_OPTIONS: -javaagent:/root/data/skywalking/skywalking-agent.jar
    Server version: Apache Tomcat/9.0.50
    Server built:   Jun 28 2021 08:46:44 UTC
    Server number:  9.0.50.0
    OS Name:        Linux
    OS Version:     4.17.11-1.el7.elrepo.x86_64
    Architecture:   amd64
    JVM Version:    1.8.0_101-b13
    JVM Vendor:     Oracle Corporation
    

    关闭tomcat

    在windows下可以通过下面方式关闭:

    bin/shutdown.bat
    bin/catalina.bat stop
    

    或者直接关闭启动窗口

    在linix下可以通过下面的方式关闭:

    bin/shutdown.sh
    bin/catalina.sh stop
    

    或者直接在终端中停止脚本

    tomcat目录结构