Bristle Software Apache Tomcat Tips

This page is offered as a service of Bristle Software, Inc.  New tips are sent to an associated mailing list when they are posted here.  Please send comments, corrections, any tips you'd like to contribute, or requests to be added to the mailing list, to tips@bristle.com.

Table of Contents:

  1. Installing Tomcat
  2. Deploying A Webapp
  3. Auto-Reloading A Webapp
  4. Auto-Reloading All Webapps
  5. Auto-Reloading Webapps More Often

Details of Tips:

  1. Installing Tomcat

    Last Updated: 7/22/2009
    Applies to: Tomcat 5.0.28+

    To install Tomcat, download it from:

    http://tomcat.apache.org/
    and follow the installation instructions there.  Be sure to change the password of the admin Web apps.

    Since Tomcat is written in Java, you may not have to run a setup program.  You can simply unzip the zip file into a desired directory and invoke it via the provided shell scripts and/or Windows batch files.  However, if you want it to run as a Unix daemon, or as a Windows service, it may be easier to run a setup program.

    Here are the steps I have taken on various platforms:

    Mac OS X

    1. Unzip the downloaded zip file into /usr/local/apache/jakarta-tomcat-5.0.28
    2. Create a symbolic link to the unzipped folder, so that I can easily switch between multiple versions of Tomcat:
      % ln -s apache/jakarta-tomcat-5.0.28 /usr/local/tomcat
    3. Set the owner, group, and access rights of the unzipped files to allow access by the user that will be used to run Tomcat:
      % chown -R fred /usr/local/tomcat/*
      % chgrp -R staff /usr/local/tomcat/*
      % chmod -R 755 /usr/local/tomcat/*
    4. setenv CATALINA_HOME /usr/local/tomcat
    5. setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
    6. Edit the file:
      /usr/local/tomcat/conf/tomcat-users.xml
      adding the lines:
      <role rolename="manager"/>
      <role rolename="admin"/>
      <user username="admin" password="some_good_password" roles="admin,manager"/>
    7. Run the shell script /usr/local/tomcat/bin/startup.sh
    8. Connect to Tomcat from a Web browser via the URL:
      http://localhost:8080
    9. Point and click to interact with the various admin and sample applications, and to read all the Tomcat docs.
    10. This leaves it running as user "fred" and not starting automatically at re-boot, which is fine on this development laptop.

    Red Hat Linux

    1. Unzip the downloaded zip file into /usr/local/apache/jakarta-tomcat-5.0.28
    2. Create a symbolic link to the unzipped folder, so that I can easily switch between multiple versions of Tomcat:
      % ln -s apache/jakarta-tomcat-5.0.28 /usr/local/tomcat
    3. Set the owner, group, and access rights of the unzipped files to allow access by the user that will be used to run Tomcat:
      % chown -R fred /usr/local/tomcat/*
      % chgrp -R staff /usr/local/tomcat/*
      % chmod -R 755 /usr/local/tomcat/*
    4. setenv CATALINA_HOME /usr/local/tomcat
    5. setenv JAVA_HOME /usr/java/j2sdk1.4.2_05
    6. Edit the file:
      /usr/local/tomcat/conf/tomcat-users.xml
      adding the lines:
      <role rolename="manager"/>
      <role rolename="admin"/>
      <user username="admin" password="some_good_password" roles="admin,manager"/>
    7. Run the shell script /usr/local/tomcat/bin/startup.sh
    8. Connect to Tomcat from a Web browser via the URL:
      http://localhost:8080
    9. Point and click to interact with the various admin and sample applications, and to read all the Tomcat docs.
    10. Later, since this was a server, I created a user and group "tomcat", changed the ownership of the files, and configured it to start automatically via that user at reboot, using the typical /etc/rc.d files.

    Windows

    1. Unzip the downloaded zip file into C:\Apps\Apache\jakarta-tomcat-5.0.28
    2. set CATALINA_HOME=C:\Apps\Apache\jakarta-tomcat-5.0.28
    3. set JAVA_HOME=C:\Apps\Sun\Java\j2sdk1.4.2_05
    4. Edit the file:
      C:\Apps\Apache\jakarta-tomcat-5.0.28\conf\tomcat-users.xml
      adding the lines:
      <role rolename="manager"/>
      <role rolename="admin"/>
      <user username="admin" password="some_good_password" roles="admin,manager"/>
    5. Run the batch file C:\Apps\Apache\jakarta-tomcat-5.0.28\bin\startup.bat
    6. Connect to Tomcat from a Web browser via the URL:
      http://localhost:8080
    7. Point and click to interact with the various admin and sample applications, and to read all the Tomcat docs.
    8. This leaves it running as the current user, and not starting automatically at re-boot, which is fine on this development laptop.

    Fedora Linux

    1. Download, install and configure Tomcat via the "yum" command:
      % yum install tomcat5
      % yum install tomcat5-webapps
      % yum install tomcat5-admin-webapps
    2. Create a symbolic link to the installed folder, so that I can easily switch between multiple versions of Tomcat, and can use the same /usr/local/tomcat path to refer to the Tomcat folder on all my different computers:
      % ln -s /usr/share/tomcat5 /usr/local/tomcat
    3. setenv CATALINA_HOME /usr/local/tomcat
    4. setenv JAVA_HOME /usr/lib/jvm/java
    5. Edit the file:
      /usr/local/tomcat/conf/tomcat-users.xml
      adding the lines:
      <role rolename="manager"/>
      <role rolename="admin"/>
      <user username="admin" password="some_good_password" roles="admin,manager"/>
    6. Run the shell script /etc/init.d/tomcat5 start
    7. Connect to Tomcat from a Web browser via the URL:
      http://localhost:8080
    8. Point and click to interact with the various admin and sample applications, and to read all the Tomcat docs.
    9. The yum install took care of all of the server details, creating a user and group "tomcat", setting the ownership of the files, and creating the typical /etc/rc.d auto-startup files. All I had to do was rename the symbolic link to tell it run the startup file at re-boot:
      % mv /etc/rc.d/rc4.d/K20tomcat5 /etc/rc.d/rc4.d/S89tomcat5

    --Fred

  2. Deploying A Webapp

    Last Updated: 1/7/2006
    Applies to: Tomcat 5.0.28+

    There are several ways to deploy a Webapp in Tomcat:

    1. Create a context configuration file for the Webapp in the Tomcat directory:

          .../conf/Catalina/localhost

      with a name like:

          mywebapp.xml

      and contents like: 

          <Context  docBase="/home/fred/bristle/webapps/mywebapp/web" 
                          path="/mywebapp"
          ></Context>

      pointing to the directory where the webapp resides.

    2. Create such a context configuration file for the Webapp in the Tomcat directory:

          .../webapps

    3. Create an apps.xml context configuration file for multiple apps:

         .../conf/Catalina/localhost/apps.xml

      with contents like: 

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <webapps>
            <Context  docBase="/home/fred/bristle/webapps/mywebapp/web" 
                            path="/mywebapp"
            ></Context>
            <Context  docBase="/home/fred/bristle/webapps/otherapp/web" 
                            path="/otherapp"
            ></Context>
          </webapps>

    4. Add a similar <Context> element to the main Tomcat server.xml file:

          .../conf/server.xml

    5. Copy the entire contents of the directory where the webapp resides to the Tomcat directory:

          .../webapps

    6. Copy the Webapp WAR file to the directory:

          .../webapps

    --Fred

  3. Auto-Reloading A Webapp

    Last Updated: 1/7/2006
    Applies to: Tomcat 5.0.28+

    To cause Tomcat to check for and load updated code for a Webapp, edit the <Context> element of the context configuration file, setting the reloadable attribute of the Context element to "true", as:

            <Context  docBase="/home/fred/bristle/webapps/mywebapp/web" 
                            path="/mywebapp"
                            reloadable="true"
            >
            </Context>

    --Fred

  4. Auto-Reloading All Webapps

    Last Updated: 1/7/2006
    Applies to: Tomcat 5.0.28+

    To cause Tomcat to check for and load updated code for all Webapps by default, add a <DefaultContext> element to the main Tomcat server.xml file:

            .../conf/server.xml

    setting the reloadable attribute of the Default Context element to "true", as:

            <DefaultContext reloadable="true"/>

    --Fred

  5. Auto-Reloading Webapps More Often

    Last Updated: 1/7/2006
    Applies to: Tomcat 5.0.28+

    To cause Tomcat to more frequently (every 10 seconds in this case) check for and load code for a Webapp, add a <Loader> element to the <Context> element of the context configuration file, as:

            <Context  docBase="/home/fred/bristle/webapps/mywebapp/web" 
                            path="/mywebapp"
                            reloadable="true">
                <Loader checkInterval="10" />
            </Context>

    You may not want to do this for a production server, since it wastes unnecessary CPU cycles in an environment where files do not change often, but it's very handy on a development server.

    --Fred

©Copyright 2006-2021, Bristle Software, Inc.  All rights reserved