Apache Tomcat
Apache Tomcat
Tomcat is a Java servlet container and web server from the Jakarta project of the Apache This is an aspect of the
web that Apache’s Tomcat is very good at because Tomcat provides both Java servlet and Java Server Pages
(JSP) technologies (in addition to traditional static pages and external CGI programming). The result is that
Tomcat is a good choice for use as a web server for many applications.
Tomcat can be used stand-alone, but it is often used “behind” traditional web servers such as Apache httpd, with
the traditional server serving static pages and Tomcat serving dynamic servlet and JSP requests.
/bin - Startup, shutdown, and other scripts. The *.sh files (for Unix systems) are functional duplicates of
the *.bat files (for Windows systems). Since the Win32 command-line lacks certain functionality, there are
some additional files in here.
/conf - Configuration files and related DTDs. The most important file in here is server.xml. It is the main
configuration file for the container.
/logs - Log files are here by default.
/webapps - This is where your webapps go.
Briefly, a servlet is a Java program designed to run in a servlet container (we hope you didn’t catch that circular
definition), and a JSP is a web page that can call Java code at request time. If you’re a system administrator or
web master, you can think of JSPs as just another scripting and templating language for HTML pages; you can
learn to write JSPs that call Java objects much as you might have used objects in JavaScript. The difference is
that the Java runs on the server side, before the web page is sent to the browser. It’s more like PHP, or even
ASP. Writing Java classes such as servlets and JSP custom tags, however, is a task probablybest left to people
trained in Java programming
Application Deployment
Once you have gotten all the files of your web application ready, it is time to deploy the
application on Tomcat. This step can be done in two ways to be explained respectively in the
following parts
A web application can be deployed in Tomcat by simply copying the unpacked directory hierarchy into a subdirectory
in directory $CATALINA_HOME/webapps/, where $CATALINA_HOME is the directory where Tomcat is installed
As mentioned above, the /WEB-INF/web.xml file contains the Web Application Deployment
Descriptor for your application. As the filename extension implies, this file is an XML document,
and defines everything about your application that a server needs to know.
1
Although you can create directories and copy files using the techniques in the previous section,
there are some advantages to using the Web Application Archive packaging format described
in the servlet specification. A major benefit with Tomcat is automatic deployment: a single
WAR file can be copied directly to Tomcat’s webapps directory, and it will be automatically
available as a context, without requiring any configuration.
Creating WAR files is actually accomplished in the same way you create JAR files: through the
jar command. So, assuming you have your web application set up correctly and completely
in a directory called testapp, you can do the following:
$ cd ~/testapp
$ jar cvf ~/testapp.war .
bash-2.04$ cd testapp
bash-2.04$ jar cvf testapp.war *
When Tomcat is started, it will automatically expand the web application archive file into
its unpacked form, and execute the application that way. This approach would typically be
used to install an additional application, provided by a third party vendor or by your internal
development staff, into an existing Tomcat installation. NOTE that if you use this approach,
and wish to update your application later, you must both replace the web application archive
file AND delete the expanded directory that Tomcat created, and then restart Tomcat, in order
to reflect your changes.
Vim /etc/profile
export JAVA_HOME=/usr/java
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_BINDIR=/usr/java/bin
export CATALINA_HOME=/usr/tomcat7
2
[root@server ~]# source /etc/profile
[root@mail ~]# echo $JAVA_HOME
/usr/java
Configuring Tomcat
Start tomcat
[root@mail bin]# nohup ./catalina.sh run &
[1] 2663
[root@mail bin]# nohup: ignoring input and appending output to `nohup.out'
[root@mail bin]# tail -f nohup.out
3
After you have Tomcat running, you will soon find a need to customize its configuration. For
example, you might want to support virtual hosting.
Configuring Tomcat is done by editing files and restarting Tomcat. The main configuration
files provided with Tomcat that reside in the $CATALINA_HOME/conf directory are
server.xml
The main Tomcat configuration file.
web.xml
A servlet specification standard format configuration file for servlets and other settings
that are global to all web applications.
tomcat-users.xml
The default list of roles, users, and passwords used by Tomcat’s UserDatabaseRealm for
authentication.
Catalina. Policy
The Java 2 Standard Edition security policy file for Tomcat. We won’t cover this file in
our classes.
The first three files are well-formed XML documents, and they are parsed by Tomcat at startup.
6.1 server.xml
The following gives the major part of server.xml, which will be discussed in details in the
following sections.
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<Service name="Catalina">
<Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
<Connector port="8009"
enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />
<Engine name="Catalina" defaultHost="localhost" debug="0">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
debug="0" resourceName="UserDatabase"/>
<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true"
4
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
</Server>
13:
case $1 in
start)
echo "Starting Tomcat"
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat"
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
echo "Restarting Tomcat"
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo $"Usage: $0 {start|stop}"
5
exit 1
;;
esac
exit 0
[root@mail ~]#
6
[root@mail ~]# $CATALINA_HOME/bin/startup.sh
Using CATALINA_BASE: /usr/tomcat7
Using CATALINA_HOME: /usr/tomcat7
Using CATALINA_TMPDIR: /usr/tomcat7/temp
Using JRE_HOME: /usr/java
Using CLASSPATH: /usr/tomcat7/bin/bootstrap.jar:/usr/tomcat7/bin/tomcat-juli.jar
Tomcat started.
[root@mail ~]#
Scenario:
7
The following diagram is my outline.
Here my tomcat IP addresses 192.168.11.197. Or any IP address allocated my ISP. But it should be public IP address.
When we purchase the domain name we need to update the tomcat IP address to it. Like
8
or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux that file is located at /etc/hosts
server.xml
</Host>
9
<Host name="live.dkindia.com" appBase="live_webapps"
unpackWARs="true" autoDeploy="true"/>
Just create new webapps directory respective side via context below command
10
<%=new Date().toLocaleString() %>
</body>
</html>
http://test.dkindia.com:8080
11
http://live.dkindia.com:8080
http://online.dyindia.com:8080
12
What is mod_jk?
The mod_jk connector is an Apache HTTPD module that allows HTTPD to communicate with Apache Tomcat instances over the
AJP protocol. The module is used in conjunction with Tomcat's AJP Connector component.
About Connectors
Apache Tomcat uses Connector components to allow communication between a Tomcat instance and another party,
such as a browser, server, or another Tomcat instance that is part of the same network. For example, the HTTP
connector listens for requests over the HTTP/1.1 protocol on various TCP ports, and forwards them to the Engine
associated with processing the request.
Using the AJP connector, Apache Tomcat instances can exchange data with mod_jk enabled instances of Apache
HTTPD, using the AJP protocol. Implementations of mod_jk are also available for integration with IIS and
NES/iPlanet/Sun, but are less widely used.
About AJP
AJP, an acronymn for Apache Jserv Protocol, is a binary version of HTTP that is optimized for communication
between Apache HTTPD and Apache Tomcat over a TCP connection. The current version of the AJP protocol is 1.3,
referred to by the standard name ajp13. ajp13 extends the earlier mod_jserv and ajp12 modules, offering significant
speed improvements and SSL support.
Other than the data format, differences between the standard HTTP and AJP protocols include more persistent
connections (to avoid unnecessary socket creation) and a focus on connection reuse over a series of request/response
cycles.
However, there are still plenty of good reasons why you might want to use the two together.
HTTPD and mod Jk can be used to balance server load across multiple Tomcat instances, or divide Tomcat instances into various
namespaces, managed by HTTPD.
We are here for discussing and integration of apache and tomcat on Linux platform. Now, I am using centos 6.2 server, jdk1.7,
apache 2.2.x , tomcat7.0.29 for done this configuration.
Download
13
-rwxr-xr-x 1 root root 692060 Mar 22 2009 /etc/httpd/modules/mod_jk.so
# mod_jk configuration
JkWorkersFile "conf/workers.properties"
JkLogFile "logs/jk.log"
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
14
worker.worker1.connection_pool_size=100
worker.worker1.connection_pool_timeout=10
http://live.dkindia.com
15
http://online.dyindia.com
http://www.apache.org/licenses/LICENSE-2.0
16
limitations under the License.
-->
<tomcat-users>
<role rolename="manager-gui"/>
<user username="admin" password="test1234" roles="manager-gui"/>
</tomcat-users>
17
How to configure
tomcat cluster
18
Using JRE_HOME: /usr/java
Using CLASSPATH: /usr/tomcat7/bin/bootstrap.jar:/usr/tomcat7/bin/tomcat-juli.jar
19