How to Install Tomcat  (Follow if Jenkins is Executing on Windows Only)


STEP 0: Create a Directory to Keep all your Works

I shall assume that you have created a directory called "c:\myWebProject" (for Windows) or "~\myWebProject" (for Mac OS X) in your earlier exercises. Do it otherwise. This step is important; otherwise, you will be out-of-sync with this article and will not be able to find your files later.


STEP 1: Download and Install Tomcat

For Windows

  1. Goto http://tomcat.apache.org ⇒ Under "Tomcat 9.0.{xx} Released", where {xx} is the latest update number ⇒ Click "Download" ⇒ Under "9.0.{xx}" ⇒ Binary Distributions ⇒ Core ⇒ "zip" (e.g., "apache-tomcat-9.0.{xx}.zip", about 11 MB).

  2. UNZIP the downloaded file into your project directory "c:\myWebProject". Tomcat shall be unzipped into directory "c:\myWebProject\apache-tomcat-9.0.{xx}".

  3. For EASE OF USE, we shall shorten and rename this directory to "c:\myWebProject\tomcat".

Take note of Your Tomcat Installed Directory. Hereafter, I shall refer to the Tomcat installed directory as <TOMCAT_HOME>.

For Mac OS X

  1. Goto http://tomcat.apache.org ⇒ Under "Tomcat 9.0.{xx} Released", where {xx} is the latest update number ⇒ Click "Download" ⇒ Under "9.0.{xx}"⇒ Binary distribution ⇒ Core ⇒ "tar.gz" (e.g., "apache-tomcat-9.0.{xx}.tar.gz", about 10.5 MB).

  2. To install Tomcat:

    1. Double-click the downloaded tarball (e.g., "apache-tomcat-9.0.{xx}.tar.gz") to expand it into a folder (e.g., "apache-tomcat-9.0.{xx}").

    2. Move the extracted folder (e.g., "apache-tomcat-9.0.{xx}") to your project directory "~/myWebProject".

    3. For EASE OF USE, we shall shorten and rename this folder to "tomcat", i.e., "~/myWebProject/tomcat".

Take note of Your Tomcat Installed Directory. Hereafter, I shall refer to the Tomcat installed directory as <TOMCAT_HOME>.


Tomcat's Sub-Directories

Take a quick look at the Tomcat installed directory. It contains the these sub-directories:


STEP 2: Create an Environment Variable JAVA_HOME

(For Windows)

You need to create an environment variable (system variable available to all applications) called "JAVA_HOME", and set it to your JDK installed directory.

Follow the steps HERE!

(For Mac OS)

Skip this step. No need to do anything.


STEP 3: Configure the Tomcat Server

The Tomcat configuration files, in XML format, are located in the "conf" sub-directory of your Tomcat installed directory, e.g. "c:\myWebProject\tomcat\conf" (for Windows) or "~/myWebProject/tomcat/conf" (for Mac OS X). The important configuration files are:

  1. server.xml

  2. web.xml

  3. context.xml

Make a BACKUP of the configuration files before you proceed!!!

Step 3(a) "conf\server.xml" - Set the TCP Port Number

Use a programming text editor (e.g., Sublime Text, Atom) to open the configuration file "server.xml".

The default TCP port number configured in Tomcat is 8080, you may choose any number between 1024 and 65535, which is not used by existing applications. We shall choose 9999 in this article. (For production server, you should use port 80, which is pre-assigned to HTTP server as the default port number.)

Locate the following lines (around Line 69) that define the HTTP connector, and change port="8080" to port="9999".

<!-- A "Connector" represents an endpoint by which requests are received
      and responses are returned. Documentation at :
      Java HTTP Connector: /docs/config/http.html
      Java AJP  Connector: /docs/config/ajp.html
      APR (HTTP/AJP) Connector: /docs/apr.html
      Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="9090" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />


STEP 4: Start Tomcat Server

The Tomcat's executable programs and scripts are kept in the "bin" sub-directory of the Tomcat installed directory.

Start Server

For Windows

I shall assume that Tomcat is installed in "c:\myWebProject\tomcat". Launch a CMD shell and issue:

c:                           // Change drive
cd \myWebProject\tomcat\bin  // Change directory to your Tomcat's binary directory
startup                      // Run startup.bat to start tomcat server

For Mac OS

I assume that Tomcat is installed in "~/myWebProject/tomcat". To start the Tomcat server, open a new "Terminal" and issue:

cd ~/myWebProject/tomcat/bin  // Change directory to your Tomcat's binary directory
./catalina.sh run             // Run catalina.sh to start tomcat server

A new Tomcat console window appears (with Java's coffee-cup logo as icon). Study the messages on the console. Look out for the Tomcat's port number. Double-check that Tomcat is running on port 9090 is configured.

Error messages will be sent to this console. System.out.println() issued by your Java servlets will also be sent to this console.

............
............
xxxxx INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-9999"]
xxxxx INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
xxxxx INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1325] ms


Shutdown Server

For Windows

You can shut down the tomcat server by either:

  1. Press Ctrl-C on the Tomcat console; OR

  2. Run "<TOMCAT_HOME>\bin\shutdown.bat" script. Open a new "cmd" and issue:

    c:                           // Change the current drive
    cd \myWebProject\tomcat\bin  // Change directory to your Tomcat's binary directory
    shutdown                     // Run shutdown.bat to shutdown the server

For Mac OS

To shut down the Tomcat server:

  1. Press Control-C (NOT Command-C) on the Tomcat console; OR

  2. Run the "<TOMCAT_HOME>/bin/shutdown.sh" script. Open a new "Terminal" and issue:

    cd ~/myWebProject/tomcat/bin  // Change current directory to Tomcat's bin directory
    ./shutdown.sh                 // Run shutdown.sh to shutdown the server

WARNING: You MUST properly shutdown the Tomcat. DO NOT kill the CAT by pushing the window's "CLOSE" button.