Skip to main content

Apache Tomcat Tutorial

Apache Tomcat is an open-source web server and servlet container developed by the Apache Software Foundation. It is widely used to deploy Java applications and support various Java technologies, including Java Servlets, JavaServer Pages (JSP), and Java Expression Language.


1. Installing Apache Tomcat​

Step 1: Download Tomcat​

  1. Go to the Apache Tomcat download page and download the latest stable release.
  2. Extract the downloaded ZIP file to a directory of your choice.

Step 2: Setting Up Environment Variables (Optional)​

Add the bin directory of Tomcat to your system's PATH:

  • Linux/Mac: Add export PATH=$PATH:/path/to/tomcat/bin to your .bashrc or .zshrc file.
  • Windows: Add C:\path\tomcat\bin to the system PATH.

Step 3: Start Tomcat​

Navigate to the bin directory in your Tomcat installation and run:

  • Linux/Mac: ./startup.sh
  • Windows: startup.bat

Access Tomcat at http://localhost:8080.


2. Tomcat Directory Structure​

Understanding the directory structure is essential for configuring and deploying applications.

  • bin: Contains startup, shutdown scripts, and executables.
  • conf: Holds configuration files (e.g., server.xml for server configurations).
  • webapps: Default location for deploying web applications.
  • logs: Stores server log files.
  • lib: Contains libraries needed by Tomcat.

3. Configuring Tomcat​

Changing the Port​

To change the default port (8080):

  1. Open conf/server.xml.
  2. Find the line <Connector port="8080" protocol="HTTP/1.1" ... />.
  3. Change 8080 to your desired port.

Configuring the Memory Limits​

Edit the JAVA_OPTS environment variable in setenv.sh (Linux/Mac) or setenv.bat (Windows) to set the maximum memory:

export JAVA_OPTS="-Xms512m -Xmx1024m"

4. Deploying Applications on Tomcat​

WAR Deployment​

  1. Build your Java application as a WAR file (e.g., myapp.war).
  2. Copy the myapp.war file to the webapps directory.
  3. Tomcat will automatically deploy the WAR file. Access it at http://localhost:8080/myapp.

Using the Manager App​

  1. Access the Manager app at http://localhost:8080/manager/html.
  2. Login with credentials set in conf/tomcat-users.xml (e.g., manager-gui role).
  3. Use the Deploy section to upload and deploy WAR files.

5. Setting Up User Roles​

To set up user roles and access:

  1. Open conf/tomcat-users.xml.

  2. Add users with specific roles:

    <role rolename="manager-gui"/>
    <role rolename="admin-gui"/>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
  3. Restart Tomcat to apply changes.


6. Monitoring and Logging​

Accessing Logs​

Tomcat stores logs in the logs directory:

  • catalina.out: Main log file for server events and errors.
  • localhost_access_log: Logs HTTP access requests.

Enabling Access Logs​

To enable access logs, uncomment the Valve section in conf/server.xml:

<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />

7. Securing Tomcat​

Disabling Directory Listing​

  1. Open conf/web.xml.

  2. Locate the section for the default servlet and set:

    <init-param>
    <param-name>listings</param-name>
    <param-value>false</param-value>
    </init-param>

Configuring HTTPS​

To enable HTTPS, configure a connector in server.xml:

  1. Generate a keystore:

    keytool -genkey -alias tomcat -keyalg RSA -keystore keystore.jks -keysize 2048
  2. Add the HTTPS connector:

    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
    keystoreFile="conf/keystore.jks" keystorePass="your_keystore_password"/>
  3. Restart Tomcat and access https://localhost:8443.


8. Load Balancing with Tomcat​

Apache Tomcat can be used with an HTTP server for load balancing.

Example with Apache HTTP Server​

  1. Enable mod_proxy and mod_proxy_balancer modules in Apache HTTP Server.

  2. Configure the load balancer:

    <Proxy "balancer://mycluster">
    BalancerMember http://localhost:8080
    BalancerMember http://localhost:8081
    </Proxy>

    ProxyPass "/" "balancer://mycluster/"
    ProxyPassReverse "/" "balancer://mycluster/"

Summary​

This tutorial covered the basics of Apache Tomcat:

  1. Installing and starting Tomcat and understanding the directory structure.
  2. Configuring ports, memory, and user roles.
  3. Deploying applications via WAR files or the Manager app.
  4. Securing and monitoring Tomcat and enabling HTTPS.

Apache Tomcat is a robust and widely used server solution for deploying Java-based web applications, offering flexibility and scalability for a variety of use cases.

Content Review​

The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.

About the Reviewer​

For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.