Saturday, November 14, 2015

How to install Apache Tomcat server on Windows,Mac OS X


  • This installation and configuration guide is applicable to Tomcat 7 and 8, and possibly the earlier versions
  • Take note that Tomcat 8 requires JDK 1.7. It will NOT work with JDK 1.6
  • You can check your JDK version via command "javac  -version".

Basics of Apache Tomcat Server:

  • Apache Tomcat is a Java-capable HTTP server, which could execute special Java programs known as Java Servlet and Java Server Pages (JSP).
  •  Tomcat is an open-source project, under the "Apache Software Foundation"
  •  The mother site for Tomcat is http://tomcat.apache.org. 

STEP 1: Download and Install Tomcat:

For Windows:

  1. Goto http://tomcat.apache.org ⇒ Downloads ⇒ Tomcat 8.0 ⇒ "8.0.{xx}" (where {xx} is the latest upgrade number) ⇒ Binary Distributions ⇒ Core ⇒ "ZIP" package
  2. UNZIP into a directory of your choice. DO NOT unzip onto the Desktop (because its path is hard to locate)
  3. I suggest using "d:\myProject". Tomcat will be unzipped into directory "d:\myProject\apache-tomcat-8.0.{xx}".
  4. Hereafter, I shall refer to the Tomcat installed directory as <TOMCAT_HOME> (or <CATALINA_HOME> - "Catalina" is the codename for Tomcat 5 and above)

For Mac OS X:


  1. Goto http://tomcat.apache.org ⇒ Download ⇒ Tomcat 8.0 ⇒ "8.0.{xx}" (where {xx} denotes the latest upgrade number) ⇒ Binary distribution ⇒ Core ⇒ "tar.gz" package
  2. To install Tomcat:
              a) Goto "~/Downloads", double-click the downloaded tarball (e.g., "apache-tomcat-8.0.{xx}.tar.gz") to expand it into a folder (e.g., "apache-tomcat-8.0.{xx}").

              b)  Move the extracted folder (e.g., "apache-tomcat-8.0.{xx}") to "/Applications"

             c)  Hereafter, I shall refer to the Tomcat installed directory as <TOMCAT_HOME> (or <CATALINA_HOME> - "Catalina" is the codename for Tomcat 5 and above).


Step 2:   Configure Tomcat Server:
  • The Tomcat configuration files are located in the "conf" sub-directory of your Tomcat installed directory
  • e.g. "d:\myProject\tomcat\conf" (for Windows) or "/Applications/tomcat"(for Mac OS X).

There are 4 configuration XML files:
  1. server.xml
  2. web.xml
  3. context.xml
  4. tomcat-users.xml 
  • Make a BACKUP of the configuration files before you proceed.

Step 3: "conf\server.xml" - Set the TCP Port Number:


  • Use a programming text editor (e.g., NotePad++, TextPad for Windows; or gEdit, jEdit, TextEdit for Mac OS X) to open the configuration file "server.xml", under the "conf" sub-directory of Tomcat installed directory.
  • The default TCP port number configured in Tomcat is 8080
  • you may choose any number between 1024 and 65535, which is not used by an existing application
  • 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.)


<!-- A "Connector" represents an endpoint by which requests are received
      and responses are returned. Documentation at :
      Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
      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="9999" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

Step 4:  "conf\web.xml" - Enabling Directory Listing

  • Again, use a programming text editor to open the configuration file "web.xml", under the "conf" sub-directory of Tomcat installed directory.
  • We shall enable directory listing by changing "listings" from "false" to "true" for the "default" servlet
  • Locate the following lines that define the "default" servlet; and change the "listings" from "false" to "true".


<!-- The default servlet for all web applications, that serves static     -->
<!-- resources.  It processes all requests that are not mapped to other   -->
<!-- servlets with servlet mappings.                                      -->
<servlet>
  <servlet-name>default</servlet-name>
  <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
  </init-param>
  <init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

Step 5:  "conf\context.xml" - Enabling Automatic Reload:

  • We shall add the attribute reloadable="true" to the <Context> element to enable automatic reload after code changes.
  • Locate the <Context> start element, and change it to <Context reloadable="true">.
<Context reloadable="true">


   ......
   ......
</Context>


Step 6:  (Optional) "conf\tomcat-users.xml":

  • Enable the Tomcat's manager by adding the highlighted lines, inside the <tomcat-users> elements:
  <tomcat-users>
  <role rolename="manager-gui"/>
  <user username="manager" password="xxxx" roles="manager-gui"/>
</tomcat-users>


This enables the manager GUI app for managing Tomcat server

Step 7: Start Tomcat Server:

  • The Tomcat's executable programs and scripts are kept in the "bin" sub-directory of the Tomcat installed directory, e.g., "d:\myProject\tomcat\bin" (for Windows) or "/Applications/tomcat/bin" (for Mac OS X).

Start Server:


For Windows:

  • Launch a CMD shell. Set the current directory to "<TOMCAT_HOME>\bin", and run "startup.bat" as follows:
  • // Change the current directory to Tomcat's "bin"
  • // Assume that Tomcat is installed in "d:\myProject\tomcat"
  • > d: // Change the current drive
  • d:\> cd \myProject\tomcat\bin  // Change Directory to YOUR Tomcat's "bin" directory
  • // Start Tomcat Server
  • D:\myProject\tomcat\bin> startup

For Mac OS X:

  • I assume that Tomcat is installed in "/Applications/tomcat". To start the Tomcat server, open a new "Terminal" and issue:
  • // Change current directory to Tomcat's binary directory
  • $ cd /Applications/tomcat/bin
  • // Start tomcat server
  • $ ./catalina.sh run
  • A new Tomcat console window appears. Study the messages on the console. Look out for the Tomcat's port number (double check that Tomcat is running on port 9999). Future error messages will be send to this console. System.out.println() issued by your Java servlets will also be sent to this console.
......
......
xxx xx, xxxx x:xx:xx xx org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9999"]
xxx xx, xxxx x:xx:xx xx org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
xxx xx, xxxx x:xx:xx xx org.apache.catalina.startup.Catalina start
INFO: Server startup in 2477 ms


Step 8: Start a Client to Access the Server:

  • Start a browser (as HTTP client). Issue URL "http://localhost:9999" to access the Tomcat server's welcome page.
  • The hostname "localhost" (with IP address of 127.0.0.1) is meant for local loop-back testing inside the same machine. 
  • For users on the other machines over the net, they have to use the server's IP address or DNS domain name or hostname in the format of "http://serverHostnameOrIPAddress:9999".



  • Try issuing URL http://localhost:9999/examples to view the servlet and JSP examples. Try running some of the servlet examples.

    • (Optional) Try issuing URL http://localhost:9999/manager/html to run the Tomcat Web Manager. Enter the username and password configured earlier in tomcat-users.xml.

      Step 9:Shutdown Server


      For Windows:

      1. You can shutdown the tomcat server by either:
      2. Press Ctrl-C on the Tomcat console; or
      3. Run "<TOMCAT_HOME>\bin\shutdown.bat" script. Open a new "cmd" and issue:
      4. // Change the current directory to Tomcat's "bin"
      5. > d:                           // Change the current drive
      6. d:\> cd \myProject\tomcat\bin  // Change Directory to YOUR Tomcat's "bin" directory
      1. // Shutdown the server
      2. d:\myProject\tomcat\bin> shutdown

      For Mac OS X

      1. To shutdown the Tomcat server:
      2. Press Control-C (NOT Command-C) on the Tomcat console; or
      3. Run the "<TOMCAT_HOME>/bin/shutdown.sh" script. Open a new "Terminal" and issue:
      4. // Change current directory to Tomcat's bin directory
      5. $ cd /Applications/tomcat/bin
      1. // Shutdown the server
      2. $ ./shutdown.sh
      WARNING: You MUST properly shutdown the Tomcat. DO NOT kill the cat by pushing the window's "CLOSE" button.

      No comments:

      Post a Comment

      High Paying Jobs after Learning Python

      Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...