MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Install | Apache Tomcat
  1. Installation and Setup
  2. Environment Configuration
  3. Starting and Stopping Tomcat
  4. Basic Web Application Example
  5. HTTP Methods Configuration (PUT and DELETE)
  6. Logging Configuration

  1. Installation and Setup
    Apache Tomcat is an open-source web server and servlet container that implements the Java Servlet specifications. It provides a Java HTTP web server environment for Java code to run.

    To use Apache Tomcat, make sure that Java is properly installed and configured.
    See the following page to install and configure Java: Install Java (Ubuntu)

    Download Apache Tomcat:
    You can download Tomcat from the official Apache website:
    http://tomcat.apache.org
    For version compatibility information, visit:
    Apache Tomcat Version Guide

    Linux Installation:
    Extract the downloaded file to your preferred installation directory. In this example, we'll use /opt/:
    $ tar -xf ~/Downloads/apache-tomcat-11.0.9.tar.gz -C /opt/
    Set appropriate permissions for the Tomcat directory:
    $ chmod -R 755 /opt/apache-tomcat-11.0.9/
    $ sudo chown -R mtitek:mtitek /opt/apache-tomcat-11.0.9/
    Create a symbolic link for easier access and future upgrades:
    $ sudo ln -s /opt/apache-tomcat-11.0.9/ /opt/apache-tomcat
    $ sudo chown -R mtitek:mtitek /opt/apache-tomcat
    Replace mtitek:mtitek with your actual user and group names.

    Verify the installation by checking the Tomcat version:
    $ /opt/apache-tomcat/bin/version.sh
    Expected output:
    Using CATALINA_BASE:   /opt/apache-tomcat
    Using CATALINA_HOME:   /opt/apache-tomcat
    Using CATALINA_TMPDIR: /opt/apache-tomcat/temp
    Using JRE_HOME:        /opt/jdk-24.0.2
    Using CLASSPATH:       /opt/apache-tomcat/bin/bootstrap.jar:/opt/apache-tomcat/bin/tomcat-juli.jar
    Using CATALINA_OPTS:   
    Server version: Apache Tomcat/11.0.9
    Server built:   Jul 1 2025 20:39:04 UTC
    Server number:  11.0.9.0
    OS Name:        Linux
    OS Version:     5.15.167.4-microsoft-standard-WSL2
    Architecture:   amd64
    JVM Version:    24.0.2+12-54
    JVM Vendor:     Oracle Corporation
  2. Environment Configuration
    To properly run Tomcat, you need to set the CATALINA_HOME environment variable, which points to the directory where Tomcat is installed. This variable is used by Tomcat's startup scripts to locate necessary files and directories.

    Linux Configuration:
    Add the following line to your shell profile file (.bashrc, .bash_profile, or .profile):
    export CATALINA_HOME=/opt/apache-tomcat
    Reload your shell profile or restart your terminal session for the changes to take effect.

    Windows Configuration:
    • Right-click on "My Computer" or "This PC"
    • Click "Properties"
    • Click the "Advanced" tab or "Advanced system settings"
    • Click the "Environment Variables" button
    • Click the "New" button (under user or system variables)
    • Set the variable name to: CATALINA_HOME
    • Set the variable value to the path where Tomcat is installed (e.g., C:\programs\apache-tomcat-7.0.12)
  3. Starting and Stopping Tomcat
    Tomcat provides simple scripts to start and stop the server. These scripts are located in the bin directory of your Tomcat installation.

    Linux:
    To start Tomcat:
    $ /opt/apache-tomcat/bin/startup.sh
    To stop Tomcat:
    $ /opt/apache-tomcat/bin/shutdown.sh
    Verifying Tomcat is Running:
    After starting Tomcat, you can verify it's running by accessing the default page in your web browser:
    http://localhost:8080

    You should see the Apache Tomcat welcome page, which confirms that Tomcat is running successfully.
  4. Basic Web Application Example
    This section demonstrates how to create a simple web application in Tomcat. Web applications in Tomcat are deployed in the webapps directory, and each application has its own subdirectory.

    Create a new folder for your web application in the webapps directory:
    Linux: Create folder "mywebapp" in "$CATALINA_HOME/webapps/"

    Inside the "mywebapp" folder, create a simple JSP file named "test1.jsp":
    Hello, world!<br />
    Current Date: <%= new java.util.Date() %>
    Access your web application by typing the following URL in your browser's address bar:
    http://localhost:8080/mywebapp/test1.jsp

    You should see output similar to:
    Hello, world!
    Current Date: Fri July 11 21:05:02 EDT 2025
    This example demonstrates how JSP pages can execute Java code (in this case, displaying the current date) and generate dynamic content.
  5. HTTP Methods Configuration (PUT and DELETE)
    By default, Tomcat restricts certain HTTP methods that can modify the structure of web applications, including PUT and DELETE methods. This is a security measure to prevent unauthorized modifications to your web applications.

    If your application requires PUT or DELETE methods and you receive a 403 (Forbidden) error when using these methods, you need to modify Tomcat's configuration.

    To enable PUT and DELETE methods, edit the global web.xml configuration file:
    Location: $CATALINA_HOME/conf/web.xml

    Find the default servlet configuration and set the "readonly" parameter to false:
    <servlet>
    <init-param>
    <param-name>readonly</param-name>
    <param-value>false</param-value>
    </init-param>
    </servlet>
    Important: You must restart Tomcat for this configuration change to take effect. This setting affects all web applications deployed on the server, so consider the security implications before making this change in production environments.
  6. Logging Configuration
    Tomcat uses Java Util Logging (JUL) for its logging framework. Proper logging configuration is essential for debugging applications and monitoring server performance.

    Logging Levels:
    Tomcat supports the following logging levels (from highest to lowest priority):
    SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL

    Configuration File:
    The main logging configuration file is located at:
    $CATALINA_HOME/conf/logging.properties
    To modify logging settings, edit this file:
    $ sudo nano /opt/apache-tomcat/conf/logging.properties
    Example Configuration:
    You can set different logging levels for specific packages or components:
    org.apache.catalina.level=CONFIG
    org.apache.tomcat.util.level=CONFIG
    
    org.glassfish.jersey.level = FINE
    This configuration sets Catalina and Tomcat utility classes to CONFIG level, while setting Jersey (if used) to FINE level for more detailed logging. Adjust these levels based on your debugging and monitoring needs.
© 2025 mtitek