Sei sulla pagina 1di 36

Tomcat Server

Version 7

Presenter : Kona. Dev Reddy

CGI
Was designed in the early days of the web
has evolved into a powerful and a useful
model for server-side programming
when a web server receives a CGI request,
it needs to start a script, allow it to run, get
back the results and pass them back to the
web client.

Servlet
Run inside the environment of the web server
the browser can submit a request to the servlet for
execution
developed by Javasoft to support server-side
programming
a parallel to the CGI model
The servlets execute in the JVM loaded by the
Web Server. Its loaded only once, and live until it
is explicitly unloaded.
Results in better performance, higher speed.

Tomcat Administration

Introduction to Tomcat
Tomcat Architecture
Tomcat Directory Structure
Configuration Files
Connectors (mod_jk, mod_proxy)
Integration with Apache

Introduction to Tomcat
Apache Tomcat is a Java-capable HTTP server, which
could execute special Java programs known as Java
Servlet and Java Server Pages (JSP). It is the official
Reference Implementation (RI) for Java Servlets and
JavaServer Pages (JSP) technologies.

Tomcat can be used as web server and it runs on default


port 8080
The performance of the Tomcat is not as good as Apache
web server when ever we are using it in stand alone
mode, therefore we generally use the Apache web server
as front end and tomcat as back end.

Tomcat Evolution

Tomcat Architecture..

Tomcat Request process....


When Tomcat is started, the Java Virtual Machine (JVM)
instance in which it runs will contain a singleton Server top
level element, which represents the entire Tomcat server. A
Server will usually contain just one Service object, which is a
structural element that combines one or more Connectors (for
example, an HTTP and an HTTPS connector) that funnel
incoming requests through to a single Catalina servlet Engine.
The Engine represents the core request processing code
within Tomcat and supports the definition of multiple Virtual
Hosts within it. A virtual host allows a single running Tomcat
engine to make it seem to the outside world that there are
multiple separate domains (for example, www.ibm.com and
www.w3.ibm.com) being hosted on a single machine.

Tomcat Architecture cont


Top Level Components..
Server :
A Server represents the entire Tomcat instance, and is responsible for managing
the life cycle of its contained services. A key aspect of the Server is that it
opens a server socket on port 8005 (the default) to listen a shutdown command
(by default, this command is the text string SHUTDOWN). When this shutdown
command is received, the server gracefully shuts itself down. For security
reasons, the connection requesting the shutdown must be initiated from the
same machine that is running this instance of Tomcat.

Tomcat Architecture cont


Service :
A Service element represents the combination of one or more Connector
components that share a single Engine component for processing incoming
requests. One or more Service elements may be nested inside a Server
element.

Tomcat Architecture cont


Connectors :
A Connector is a service endpoint on which a client connects to the Tomcat
container. It serves to insulate the engine from the various communication
protocols that are used by clients, such as HTTP, HTTPS, or the Apache JServ
Protocol (AJP).
Tomcat can be configured to work in two modesStandalone or in Conjunction
with a separate web server.
In standalone mode, Tomcat is configured with HTTP and HTTPS connectors,
which make it act like a full-fledged web server by serving up static content
when requested, as well as by delegating to the Catalina engine for dynamic
content.
In conjunction mode, Tomcat plays a supporting role to a web server, such as
Apache httpd or Microsoft's IIS. The client here is the web server,
communicating with Tomcat either through an Apache module or an ISAPI DLL.
When this module determines that a request must be routed to Tomcat for
processing, it will communicate this request to Tomcat using AJP, a binary
protocol that is designed to be more efficient than the text based HTTP when
communicating between a web server and Tomcat.

Tomcat Architecture cont


Container components:
Engine:
An Engine represents a running instance of the Catalina servlet engine and
comprises the heart of a servlet container's function. There can only be one
engine within a given service. Being a true container, an Engine may contain
one or more virtual hosts as children.

Tomcat Architecture cont


Being the primary request processing component, it receives objects that
represent the incoming request and the outgoing response. Its main function is
to delegate the processing of the incoming request to the appropriate virtual
host. If the engine has no virtual host with a name matching the one to which
the request should be directed, it consults its defaultHost attribute to determine
the host that should be used.
Hosts
Hosts mimics the popular Apache virtual host concept, the Host contains a
name and an IP address. You can multiple Hosts each with its own Web
application.
A Host defines a virtual host under the Engine, which can in turn support many
Contexts (webapps).
<Host name="localhost" appBase="webapps" unpackWARs="true"
autoDeploy="true">
The default configuration define one host called localhost. The appBase
attribute defines the base directory of all the webapps, in this case,
<CATALINA_HOME>\webapps. By default, each webapp's URL is the same as its
directory name. For example, the default Tomcat installation provides four
webapps: docs, examples, host-manager and manager under the webapps
directory. The only exception is ROOT, which is identified by an empty string.
That is, its URL is http://localhost:8080/.
The unpackWARs specifies whether WAR-file dropped into the webapps directory
shall be unzipped. For unpackWARs="false", Tomcat will run the application
from the WAR-file directly, without unpacking, which could mean slower
execution.
The autoDeploy attribute specifies whether to deploy application dropped into
the webapps directory automatically.

Tomcat Architecture cont (Named-Based Example )

Tomcat Architecture cont ( IP-Based Example )

Tomcat Architecture cont


Nested components:
Valve
A Valve can intercept HTTP requests before forwarding them to the applications,
for pre-processing the requests. A Valve can be defined for any container, such
as Engine, Host, and Context, and Cluster.
In the default configuration, the AccessLogValve intercepts an HTTP request and
creates a log entry in the log file, as follows:
<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" />
Other valves include:
RemoteAddrValve: which blocks requests from certain IP addresses,
RemoteHostValve: which blocks request based on hostnames,
RequestDumperValve: which logs details of the requests,
SingleSignOn Valve: when placed under a <host>, allows single sign-on to
access all the webapp under the host.

Tomcat Architecture cont

Tomcat Architecture cont


Realm
A Realm is a database of user, password, and role for authentication (i.e.,
access control). You can define Realm for any container, such as Engine, Host,
and Context, and Cluster.
<Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm
className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/> </Realm>
The default configuration defines a Realm (UserDatabaseRealm) for the
Catalina Engine, to perform user authentication for accessing this engine. It
uses the JNDI name UserDatabase defined in the GlobalNamingResources.
Besides the UserDatabaseRealm, there are: JDBCRealm (for authenticating
users to connect to a relational database via the JDBC driver);
DataSourceRealm (to connect to a DataSource via JNDI; JNDIRealm (to connect
to an LDAP directory); and MemoryRealm (to load an XML file in memory).

Tomcat Architecture cont


Listeners
The Server contains several Listeners . A Listener listens and responses to specific events.
The JasperListener enables the Jasper JSP engine, and is responsible for re-compiling the JSP
pages that have been updated.
<Listener className="org.apache.catalina.core.JasperListener" />
The GlobalResourcesLifecycleListener enables the global resources, and makes possible the
use of JNDI for accessing resources such as databases.
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
Global Naming Resources
The <GlobalNamingResources> element defines the JNDI (Java Naming and Directory
Interface) resources, that allows Java software clients to discover and look up data and
objects via a name.
The default configuration defines a JNDI name called UserDatabase via the <Resource>
element , which is a memory-based database for user authentication loaded from
"conf/tomcat-users.xml".
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase" description="User database that can be updated
and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
You can define other global resource JNDI such as MySQL database to implement connection
pooling.

Server.xml

web.xml
The web.xml file format is defined in the Java Servlet Specification,* so this file
format will be usedin every servlet-conforming Java servlet container. This file
format is usedin two places in Tomcat: in the CATALINA_BASE/conf directory
and in each web application. Each time Tomcat deploys an application (during
startup or when the application is reloaded), it reads the global conf/web.xml
followedby the WEBINF/web.xml within your web application (if there is one). As
youdexpect, then, settings in the conf/web.xml file apply to all web applications,
whereas settings in a given web applications WEB-INF/web.xml apply only to
that application.

Tomcat Directory Structure...

Tomcat Directory Structure...

Bin : This directory hold some of the JAR files that are required when starting
Tomcat,

Tomcat Directory Structure...


Conf : global configuration applicable to all the
webapps.
One Policy File: catalina.policy for specifying
security policy.
Two Properties Files: catalina.properties and
logging.properties,
catalina.properties : sets the locations of the
various class loader directories. The defaults are
the common, server, and shared directories and
their subdirectories. The settings in this file
determine which classes are available to all web
applications and which classes are available to
Tomcat. In other words, these settings configure
the classpath of Tomcat and all web applications.
logging.properties : is a file that manages the
default logging levels for the Tomcat server itself.
Four Configuration XML Files
server.xml (Tomcat main configuration file),
web.xml (global web application deployment
descriptors),
context.xml (global Tomcat-specific
configuration options) and
tomcat-users.xml (a database of user,
password and role for authentication and
access control).

Tomcat Directory structure cont......

Lib : all the JAR files that the container uses are located
here, this includes Tomcat JAR's and the servlet and
JSP application programming interfaces (API's). Place
your own JAR files here if they will be used across all
your Web applications.

Tomcat Directory structure cont......

Logs : contains the engine logfile


Catalina.{yyyy-mm-dd}.log, host logfile
localhost.{yyyy-mm-dd}.log, and other
application logfiles such as manger
and host-manager. The access log
(created by the AccessLogValve) is
also kept here.

Tomcat Directory structure cont......

webapps: the default appBase - web


applications base directory of the host
localhost.

work: contains the translated servlet


source files and classes of JSP/JSF.
Organized in hierarchy of engine name
(Catalina), host name (localhost),
webapp name, followed by the Java
classes package structure.

Temp:temporary files.

Connectors (mod_jk, mod_proxy)

Most Tomcat configurations are a Apache/Tomcat setup, Apache serving up the static
content and then passing any JSP to Tomcat to process. Tomcat can be integrated with
Apache by using the JK Connector. The JK Connector uses the Apache JSserv Protocol
(AJP) for communications between Tomcat and Apache.

The AJP Connector: The AJP protocol is used for communication between Tomcat and
Apache, the software modules used on Apache are mod_jk or mod_proxy.

The major feature of this protocol are

Good performance on fast networks


Support for SSL, encryption and client certificate
Support of clustering by forwarding requests to multiple Tomcat servers

Apache Webserver

Download the Apache HTTP Server


http://httpd.apache.org/download.cgi
Extracting the source from the Apache HTTPd tarball is a simple matter of uncompressing,
and then untarring:
$ gzip -d httpd-NN.tar.gz
$ tar xvf httpd-NN.tar

(OR)

$tar -zxvf httpd-NN.tar.gz

Configure the source tree


Apache HTTPd uses libtool and autoconf to create a build environment that looks like many
other Open Source projects.
To configure the source tree using all the default options, simply type ./configure. To change
the default options, configure accepts a variety of variables and command line options.
The most important option is the location --prefix where the Apache HTTP Server is to be
installed later, because Apache HTTPd has to be configured for this location to work
correctly.
Build - Now you can build the various parts which form the Apache HTTPd package by
simply running the command:
$ make

Apache Webserver
Install : Now its time to install the package under the configured installation PREFIX (see
--prefix option above) by running:
$ make install

Starting the Apache HTTP Server


$ PREFIX/bin/apachectl -k start
$ PREFIX/bin/apachectl -k stop
apachectl -k graceful
graceful signal causes the parent process to advise the children to exit after their current
request (or to exit immediately if theyre not serving anything). The parent re-reads its
configuration files and re-opens its log files. As each child dies off the parent replaces it
with a child from the new generation of the configuration, which
begins serving new requests immediately.

Configure mod_jk connector...


To Download tomcat connectors use the following link
http://tomcat.apache.org/download-connectors.cgi
tar -xvzf tomcat-connectors-1.2.xx-src.tar.gz
cd tomcat-connectors/native
./configure --with-apxs=/usr/sbin/apxs
make
make install
it will create mod_jk.so , copy this one to apache modules
Dynamic Shared Object (DSO) Support
The Apache HTTP Server is a modular program where the administrator can choose the
functionality to include in the server by selecting a set of modules. The modules can be
statically compiled into the httpd binary when the server is built. Alternatively, modules can
be compiled as Dynamic Shared Objects (DSOs) that exist separately from the main
httpd binary file. DSO modules may be compiled at the time the server is built, or they may
be compiled and added at a later time using the Apache Extension Tool (apxs).

Connectors (mod_jk)... (worker.properties)

Create workers.properties file

here entry format is look like


worker.<name>.property=<value>
for example
worker.<name>.type=ajp13
worker.<name>.port=<ajp port>
worker.<name>.host=<tomcat ip addr>
worker.list=<name>
here
worker.list key have all workers name separated by comma.
type = here type of the worker. we use ajp13 version
port= we specify the ajp port (not http port ) of that server
host= IP address or host name of tomcat server

Connectors (mod_jk)... (worker.properties)


worker.list=balancer,stat
worker.tomcat1.type=ajp13
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.port=8010
worker.tomcat2.host=localhost
worker.tomcat3.type=ajp13
worker.tomcat3.port=8011
worker.tomcat3.host=localhost
worker.balancer.type=lb
worker.balancer.balance_workers=tomcat1,tomcat2,tomcat3
worker.stat.type=status
here this worker type is lb, ie load balancer. its special type provide by load balancer. and
another property is balance_workers to specify all tomcat instances like
tomcat1,tomcat2,tomcat3 (comma separated)
status : display useful information about how the load among the various Tomcat workers is
distributed

Configure mod_jk connector...


Now workers.properties confiuration is finished. now we need to send the all request to
balancer worker.
so modify the httpd.conf file of Apache httpd server
LoadModule

jk_module modules/mod_jk.so

JkWorkersFile conf/workers.properties
JkLogFile

logs/mod_jk.log

JkMount /status stat


JkMount / balancer
JKMount /* balancer
when one tomcat instance is failed, load balancer don't know about that instance is failed.
so its try to forward the request to all tomcat instances. If load balancer try to forward the
request to failed tomcat instance, it will not respond. so load balancer understand and
marked the state as a failed and forward the same request to another tomcat instances.
when tomcat instances recovered from failed state. even then load balancer don't know that
tomcat is ready for processing. Its still marked the state is failed. In periodic interval load
balancer checks the health status of all tomcat instances. (by default 60 sec). after
checking health status then load balancer will come to know the availability it update the
status is OK.

Configure mod_jk connector...

The last 2 lines are important.


JkMount /status stat means any request to match the /status then that
request forward to stat worker. Its status type worker. so its shows status of
load balancer.
JkMount / balancer this line matches all the request, so all request is
forward to balancer worker. In balancer worker list uses the round robin
algorithm to distribute the request to other tomcat instances.

Thanks ................

Potrebbero piacerti anche