Technology Sharing

[Detailed explanation of Tomcat directory] Details you need to know about Tomcat

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

I hope this article can give you inspiration and inspiration~
If you think the article is helpful to you,Like + Follow + Favorite Support the blogger.

Opening remarks

Tomcat is still active in the daily life of developers, but do we have a deeper understanding of it, or is it enough to just know how to install, configure and start it? This article is my own summary and record, I hope there is something you need;

insert image description here

1. Basic environment description

Considering environmental factors, everyone should appropriately compare their own hardware and software environment analysis. Please read the hardware and software environment carefully.

1.1 Hardware Environment

Windows 11 专业版

1.2 Software Environment

development tools:Tomcat 9.0

2. Tomcat's file structure

The file directory structure is mainly divided into: bin, conf, lib, logs, temp, webapps, work. The file directory structure in Tomcat is relatively clear, and each directory has its own特定的功能and用途

insert image description here

2.1 bin directory

The bin directory contains Tomcat startup and shutdown script files; here are a few重要的文件We must know;

  • Files ending with .sh: startup and shutdown scripts on Linux platforms or Mac.
  • Files ending with .bat: startup and shutdown scripts on the Windows platform.
  • catalina.sh: Tomcat's core startup script, in which you can set JVM parameters.
  • startup.sh and shutdown.sh: used to start and shut down the Tomcat server respectively.
  • service.bat: used to register Tomcat service on Windows.

2.1.1 startup and shutdown

forstartupandshutdownYou must be very familiar with it. We must use it to quickly start and shut down the tomcat server in our daily life. As for the suffix bat, or use sh, it depends on your operating system type;

2.1.2 Catalina

aboutCatalinaFile, he is also tomcat启动脚本,andstartupThe main difference is that the latter专注启动, while the former can also be started through配置参数To achieve other purposes;

For example, here I use catalina to start Tomcat, I would do this: entercatalina.bat start (Windows system)
insert image description here
Here istomcat的bin目录If you have configured环境变量, then you can perform this operation at any location; similarly, we can also usecatalina.bat stopTo stop the service;

So how to configure the parameters?

The usual practice is to modify the catalina file to configure the relevant parameters, and the most common parameter configuration is JVM and other related content; for example, in the catalina file, I have-Xms512malready setupJVM的初始堆内存is 512MB,-Xmx1024malready setup最大堆内存is 1024MB,-XX:+UseG1GCEnabledG1垃圾回收器-Duser.timezone=Asia/Shanghaialready setupJVM的时区

insert image description here
After modification, restart Tomcat to make the configuration take effect; here is a comparison of the difference before and after the configuration;
insert image description here
insert image description here
The program starts normally, and we can see the parameter information we configured in the console;

需要注意的是In reality, we do not不建议直接在catalina中修改This is not friendly to the upgrade and maintenance of our tomcat service, so we recommend通过setenv配置文件To configure parameters; why setenv? Let's take a look at the catalina configuration file. This section tells us that the script will make a judgment when it starts, and if this file exists, it will be automatically called; this file usually does not exist in the current tomcat directory, so we need to create one manually;
insert image description here
Create setenv (setenv.bat in Windows, setenv.sh in mac/linux), and then add configurations according to different systems; please note that the syntax is slightly different;

// mac或linux
export JAVA_OPTS="-Xms512m -Xmx1024m -XX:+UseG1GC -Duser.timezone=Asia/Shanghai"
  • 1
  • 2
// windows
set JAVA_OPTS=-Xms512m -Xmx1024m -XX:+UseG1GC -Duser.timezone=Asia/Shanghai
  • 1
  • 2

2.1.3 service.bat(Windows)

service file, which we will only see in Windows, is usually used to register Tomcat service as a system service; the main usage is to enter the bin directory and execute service.bat install + custom service name;
insert image description here
insert image description here
Then we can see in the Windows service that it is not started at this time. You can click to start it, or restart the computer. It will start automatically with the next computer restart;

2.2 conf directory

The conf directory stores various global configuration files of the Tomcat server.

  • server.xml: Tomcat's main configuration file, which contains configuration information for components such as Service, Connector, Engine, and Host.
  • web.xml: A configuration file that complies with the Servlet specification standard. It is used to configure servlets and provide default configuration information for all Web applications.
  • tomcat-users.xml: related roles, users, passwords and other information used in Realm authentication.
  • context.xml: default configuration information for all hosts.
  • catalina.policy: Java-related security policy configuration file.
  • catalina.properties: Tomcat internal package definition and access-related control files.
  • logging.properties: Configuration information related to the Tomcat logger.

2.2.1 server.xml

In the Apache Tomcat server.xml configuration file, you can配置多个端口, but the most common includeHTTP服务的端口(The default is 8080),AJP服务的端口(the default is 8009), andSSL/TLS(即HTTPS)的端口(If enabled, the default may be 8443, but this depends on the specific configuration)
【1】http connector port

The HTTP connector is used to handle HTTP requests entering Tomcat. By default, Tomcat listens on port 8080. You can change this port by modifying the port attribute of the tag.

insert image description here
【2】AJP connector port

The AJP (Apache JServ Protocol) connector is used for communication between Tomcat and the Apache HTTP server, especially when the mod_jk module is used. By default, Tomcat listens on port 8009.

AJP目前我们其实不怎么使用,因为nginx已经可以替代它;它一开始是默认注释的,根据自己需要来开启

【3】SSL/TLS (HTTPS) connector port

If you intend to have Tomcat handle HTTPS requests, you need to configure an SSL/TLS connector. This involves specifying information such as the keystore and password for the SSL certificate.

insert image description here
这个也是默认注释的,当你需要用到https请求的时候,再去开启;certificateKeystoreFile和certificateKeystorePassword应该指向你的密钥库文件和它的密码。这些值需要根据你的实际密钥库文件和密码进行更改

2.2.2 web.xml (rarely changed)

Tomcat installation directoryconf目录middleweb.xml文件yes全局配置文件, which defines the default Servlet and MIME type mappings within the Tomcat server. Usually, this global web.xml file does not need to be frequently modified by developers, unless you need to add global Servlet mappings, filters, listeners, etc. for the entire Tomcat server;

一些配置是可以通过注解(Annotations)来完成,这减少了对web.xml文件的依赖;

2.2.3 tomcat-users.xml

<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">  
  
  <!--  
    注意:默认情况下,Tomcat Manager是禁用的,并且没有设置任何用户。  
    要启用Tomcat Manager,并为其设置用户,你需要取消以下注释,  
    并根据需要修改用户名、密码和角色。  
  -->  
  
  <role rolename="manager-gui"/>  
  <role rolename="manager-script"/>  
  <user username="tomcat" password="tomcat" roles="manager-gui"/>  
  <user username="admin" password="admin123" roles="manager-script"/>  
  
</tomcat-users>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.2.4 logging.properties

By editing this file, you can define the log level, format, output location, etc. This is very useful for monitoring and debugging Tomcat servers and their deployed applications.

# 设置日志的根级别和处理器  
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler  
  
# 设置根日志级别  
.level = INFO  
  
# 设置特定类别的日志级别  
org.apache.catalina.level = INFO  
org.apache.catalina.startup.level = FINE  
org.apache.catalina.session.level = FINE  
org.apache.catalina.util.lifecycle.level = FINE  
  
# 为不同的处理器(文件处理器和控制台处理器)设置格式和文件路径  
# 文件处理器示例  
1catalina.org.apache.juli.FileHandler.level = FINE  
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs  
1catalina.org.apache.juli.FileHandler.prefix = catalina.  
  
2localhost.org.apache.juli.FileHandler.level = FINE  
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs  
2localhost.org.apache.juli.FileHandler.prefix = localhost.  
  
3manager.org.apache.juli.FileHandler.level = FINE  
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs  
3manager.org.apache.juli.FileHandler.prefix = manager.  
  
4host-manager.org.apache.juli.FileHandler.level = FINE  
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs  
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.  
  
# 控制台处理器  
java.util.logging.ConsoleHandler.level = FINE  
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter  
  
# 设置日志文件的编码  
org.apache.juli.FileHandler.encoding = UTF-8  
  
# Java 类的日志级别  
org.apache.jasper.level = INFO  
org.apache.coyote.level = INFO
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

2.3 lib directory (basically unchanged)

Function: Stores the library files (JAR files) required for Tomcat to run.
Note: Although some older versions of Tomcat or specific configurations may store JAR files in other directories such as common/lib, server/lib, or shared/lib, modern versions of Tomcat prefer to place core library files in the lib directory.

2.4 logs directory

Function: Stores log files during Tomcat execution.
Content: includes various log information during Tomcat startup, operation and shutdown, such as catalina.out, localhost.log, etc.

insert image description here
这里catalina.2024-7-10是通过日志滚动策略生成的,用于记录Catalina容器的日常运行日志;

locahost.2024-7-10,localhost_access_log.2024-7-10分别记录了应用程序的日志信息和访问日志;

host-manager日志是监控和管理Tomcat服务器上虚拟主机的重要工具。通过分析这些日志文件,管理员可以了解虚拟主机的使用情况,以及是否存在潜在的问题或安全风险

Usually when we check logs daily, we don’t choose Catalina.log/Catalina.out/appName.log, etc.

2.5 webapps directory

Function: Tomcat's main Web publishing directory, used to store Web applications.
Content: By default, you can publish an application by placing the Web application file in this directory. Tomcat will automatically scan the application in this directory and deploy it.

The characteristics of webapps are:
[1] Automatic deployment: Tomcat will periodically scan all contents in the webapps directory. WAR 文件(for example, myapp.war) into the webapps directory, Tomcat will automatically detect this file and尝试部署它If the file is a WAR package, Tomcat will first decompress the file and then deploy the application according to the decompressed directory structure.
[2] Management: You can easily manage Web applications on Tomcat by directly operating the files in the webapps directory. For example, you can删除目录来卸载应用程序, or by替换 WAR 文件Comerenewapp.

【3】Custom deployment path:Tomcat 默认将 Web 应用程序部署在 webapps 目录下, but you can also modify the Tomcat configuration file (such as server.xml) to specify other directories as the deployment path for the application. I'd like to mention this, and I rarely see other paths defined; but it's not impossible;

<Host name="localhost"  appBase="webapps"  
        unpackWARs="true" autoDeploy="true">  
  
    <!-- 配置一个Context来指定其他目录为应用程序的部署路径 -->  
    <Context path="/myapp" docBase="/path/to/your/application" reloadable="true">  
        <!-- 这里可以添加更多的Context配置 -->  
    </Context>  
  
    <!-- 其他Host配置 -->  
  
</Host>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.6 work directory

Function: Store class files generated after JSP compilation and temporary files of Servlet.
Note: Clear the contents of this directory and restart Tomcat to clear the compiled JSP and Servlet cache.

简单的来说,work的作用就是JSP编译和缓存来提高tomcat的性能,主要体现在响应速度上;
注意需要定期清理该目录,长期tomcat的运行和更新会导致该目录越来愈大;占用大量的磁盘空间;

2.7 temp directory

Function: Store temporary files generated during the operation of Tomcat.
Note: These temporary files are usually used to support Tomcat's internal operations, such as session management, file uploads, etc.

这个目录有点类似于work目录,也是通过缓存临时文件来提高应用程序的性能

3. Finally

【1】After making relevant configurations, you often need to restart Tomcat for them to take effect;

[2] Although the temp and work directories are cache files and temporary files, you still need to make a backup when clearing them, and check whether these files are still occupied by other application processes;

【3】When shutdown is executed, it does not stop immediately, but tries to shut down Tomcat gracefully. This process may take some time to stop depending on the situation. You can check the status of the Tomcat port to determine whether Tomcat has been shut down.