Windows下 如何配置TomCat

时间:2021-07-04 16:12:09

TomCat是一个标准的容器,为Servlet等组件提供了良好的运行环境;

TomCat是Apache旗下的产品,开源免费;目前最新版本是TomCat 8.0.15;

下载TomCat可以去下面的连接地址:

Apache的TomCat官网

Windows下 如何配置TomCat

官网的左边有Download的区域,可以选择自己需要的版本;

Windows下 如何配置TomCat

点击进去,在Core(内核/核心)模块,可以下载到TomCat版本;一般选择zip下载

下载完成后,就解压就可以用了,因为TomCat也是使用Java语言编写,所以只要系统里面有JRE(java运行环境),配置好环境就可以运行了;

--------------------------------------------------------------------------------------

如何配置TomCat系统环境(Windows)呢?


首先,右击计算机--属性

Windows下 如何配置TomCat

然后点击,高级系统设置

Windows下 如何配置TomCat

在高级选项卡中,选择“环境变量”

Windows下 如何配置TomCat

这时才是真正开始配置的关键;

Windows下 如何配置TomCat

=======================================================================

首先注意:

对于部分用户,只需要配置“系统变量”就行了,

但是,对于大部分用户,建议同时配置“系统变量“和”用户变量“

也就是,配置两遍;如何在“系统变量“中配置,也要在”用户变量“同样的配置;


具体步骤:


1.配置CATALINA_HOME环境变量

Windows下 如何配置TomCat

变量名是固定的,变量值是指TomCat的解压路径(根目录),

注意,变量名建议大写(我不知道小写,会不会导致不正常)

其次,变量值后面不能加“;”或者“\”,这都会导致配置失败;

Windows下 如何配置TomCat

TomCat的官方推荐,我们需要再配一个环境变量,名字叫做:CATALINA_BASE

变量值也是TomCat的解压路径(根目录)


2.配置path

在环境变量Path中,新增加“%CATALINA_HOME%\lib;%CATALINA_HOME%\bin;"

Windows下 如何配置TomCat

这是方便调用类库的;


3.配置JRE_HOME 或 JAVA_HOME

JRE_HOME就是Java安装目录下的jre目录

而JAVA_HOME就是java安装目录下的jdk目录

变量值后面不能加“;”或者“\”,这都会导致配置失败;

Windows下 如何配置TomCat

配置如下:

Windows下 如何配置TomCat

Windows下 如何配置TomCat


这样配置就基本成功了,现在只需要验证一下:

开始--运行--输入cmd,打开

Windows下 如何配置TomCat

打开cmd,输入startup.bat

Windows下 如何配置TomCat

如果配置成功的话,就会出现下面的提示,并且跳出窗口

Windows下 如何配置TomCat

打开浏览器,输入:http://localhost:8080, 看tomcat是否正常启动
关闭tomcat:

进入命令行cmd,输入shutdown.bat

==================================================================

下面是TomCat的官方配置指导:

(3.1) Set CATALINA_HOME (required) and CATALINA_BASE (optional)

The CATALINA_HOME environment variable should be set to the location of the root directory of the "binary" distribution of Tomcat.

An example was given in (2.2) above.

The Tomcat startup scripts have some logic to set this variable automatically if it is absent, based on the location of the startup script in *nix and on the current directory in Windows. That logic might not work in all circumstances, so setting the variable explicitly is recommended.

The CATALINA_BASE environment variable specifies location of the root directory of the "active configuration" of Tomcat. It is optional. It defaults to be equal to CATALINA_HOME.

Using distinct values for the CATALINA_HOME and CATALINA_BASE variables is recommended to simplify further upgrades and maintenance. It is documented in the "Multiple Tomcat Instances" section below.



(3.2) Set JRE_HOME or JAVA_HOME (required)

These variables are used to specify location of a Java Runtime Environment or of a Java Development Kit that is used to start Tomcat.

The JRE_HOME variable is used to specify location of a JRE. The JAVA_HOME variable is used to specify location of a JDK.

Using JAVA_HOME provides access to certain additional startup options that are not allowed when JRE_HOME is used.

If both JRE_HOME and JAVA_HOME are specified, JRE_HOME is used.

The recommended place to specify these variables is a "setenv" script. See below.



(3.3) Other variables (optional)

Other environment variables exist, besides the four described above. See the comments at the top of catalina.bat or catalina.sh scripts for the list and a description of each of them.

One frequently used variable is CATALINA_OPTS. It allows specification of additional options for the java command that starts Tomcat.

See the Java documentation for the options that affect the Java Runtime Environment.

See the "System Properties" page in the Tomcat Configuration Reference for the system properties that are specific to Tomcat.

A similar variable is JAVA_OPTS. It is used less frequently. It allows specification of options that are used both to start and to stop Tomcat as well as for other commands.

Note: Do not use JAVA_OPTS to specify memory limits. You do not need much memory for a small process that is used to stop Tomcat. Those settings belong to CATALINA_OPTS.

Another frequently used variable is CATALINA_PID (on *nix only). It specifies the location of the file where process id of the forked Tomcat java process will be written. This setting is optional. It will enable the following features:

 *  better protection against duplicate start attempts and
 *  allows forceful termination of Tomcat process when it does not react to the standard shutdown command.

(3.4) Using the "setenv" script (optional, recommended)

Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can be specified in the "setenv" script. The script is placed either into CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named setenv.bat (on Windows) or setenv.sh (on *nix). The file has to be readable.

By default the setenv script file is absent. If the script file is present both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is preferred.

For example, to configure the JRE_HOME and CATALINA_PID variables you can create the following script file:

On Windows, %CATALINA_BASE%\bin\setenv.bat:

  set "JRE_HOME=%ProgramFiles%\Java\jre6"
  exit /b 0

On *nix, $CATALINA_BASE/bin/setenv.sh:

  JRE_HOME=/usr/java/latest
  CATALINA_PID="$CATALINA_BASE/tomcat.pid"

The CATALINA_HOME and CATALINA_BASE variables cannot be configured in the setenv script, because they are used to locate that file.

All the environment variables described here and the "setenv" script are used only if you use the standard scripts to launch Tomcat. For example, if you have installed Tomcat as a service on Windows, the service wrapper launches Java directly and does not use the script files.