0% found this document useful (0 votes)
949 views

Apache Tomcat

Uploaded by

api-284453517
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
949 views

Apache Tomcat

Uploaded by

api-284453517
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CONFIGURE APACHE WITH 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.

These are some of the key tomcat directories:

 /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.

What are Servlets and JSP?

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

The following gives a servlet example:


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

Deploying “Unpacked” Servlets and Java Server Pages

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.

Deploying Applications in WAR Format

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.

First download java JDK Package


Java configuration
.
# Tar –zxvf jdk-7u11-linux-i586.tar.gz
jdk1.7.0_11
go /usr make directory java
# mkdir /usr/java
# mv Desktop/ jdk1.7.0_11 /usr/java

Set java HOME PATH & Tomcat

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

Update file & Verify new settings:

2
[root@server ~]# source /etc/profile
[root@mail ~]# echo $JAVA_HOME
/usr/java

Configuring Tomcat

Download tomcat 7 below link

Use links http://www.rosehosting.com/blog/how-to-install-tomcat-7-jdk-7/

[root@mail Desktop]# unzip apache-tomcat-7.0.53.zip


[root@mail Desktop]# ll
total 240508
drwxr-xr-x 9 root root 4096 Mar 25 06:25 apache-tomcat-7.0.53
[root@mail Desktop]# cp -r apache-tomcat-7.0.53/* /usr/tomcat7/

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

Access urn http://serverip:8080

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:

Make your script executable check

[root@server ~]# cd /usr/tomcat7/bin/


[root@server bin]# chmod +x catalina.sh
[root@server bin]# chmod +x shutdown.sh
[root@server bin]# chmod +x startup.sh

Startup script of tomcat

[root@mail ~]# cat /etc/init.d/tomcat


#!/bin/bash
# description: Tomcat Start - Stop - Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/tomcat7

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 ~]#

[root@mail ~]# ls -ltr /etc/init.d/tomcat


-rwxr-xr-x 1 root root 509 Jul 16 20:10 /etc/init.d/tomcat

[root@mail ~]# chkconfig --add tomcat


[root@mail ~]# chkconfig --level 3 tomcat on
[root@mail ~]# chkconfig --list tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:off 6:off

Service tomcat start

[root@mail ~]# /etc/init.d/tomcat start


Starting Tomcat
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 ~]# /etc/init.d/tomcat status
Usage: /etc/init.d/tomcat {start|stop}
[root@mail ~]# /etc/init.d/tomcat stop
Stopping Tomcat
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

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 ~]#

Understanding Virtual Host Concept in Tomcat


Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy
multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective

Scenario:

I am going to deploy 3 website with following domain names in single tomcat

[root@mail ~]# cat /etc/hosts


127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.11.197 test.dkindia.com
192.168.11.197 live.dkindia.com
192.168.11.197 online.dyindia.com

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.

How all domain names are pointing to my Tomcat?

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

Now How Setup Virtual Host Concept?


Before going to setup the virtual host. first take look at the server.xml file in conf folder in tomcat directory.

server.xml

[root@mail ~]# vim /usr/tomcat7/conf/server.xml


<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">

<!-- SingleSignOn valve, share authentication between web applications


Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->

<!-- Access log processes all example.


Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<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" />

</Host>

<Host name="test.dkindia.com" appBase="test_webapps"


unpackWARs="true" autoDeploy="true"/>

9
<Host name="live.dkindia.com" appBase="live_webapps"
unpackWARs="true" autoDeploy="true"/>

<Host name="online.dyindia.com" appBase="online_webapps"


unpackWARs="true" autoDeploy="true"/>

Just create new webapps directory respective side via context below command

[root@mail ~]# cd /usr/tomcat7/


[root@mail tomcat7]# ll
[root@mail tomcat7]# mkdir test_webapps
[root@mail tomcat7]# mkdir live_webapps
[root@mail tomcat7]# mkdir online_webapps

Copy all same

[root@mail tomcat7]# cp -r webapps/* test_webapps/


[root@mail tomcat7]# cp -r webapps/* live_webapps/
[root@mail tomcat7]# cp -r webapps/* online_webapps/

Remove all old contain in Root Directory below commands

[root@mail tomcat7]# rm -rf test_webapps/ROOT/*


[root@mail tomcat7]# rm -rf live_webapps/ROOT/*
[root@mail tomcat7]# rm -rf onlive_webapps/ROOT/*

First context test page

[root@mail ~]# cat /usr/tomcat7/test_webapps/ROOT/index.jsp


<%@page import="java.util.Date"%>
<html>
<head>
<title>Java Curren Date and Time</title>
</head>
<body>
<h1> test.dkindia.com Current Time Is : </h1>

10
<%=new Date().toLocaleString() %>
</body>
</html>

Second context test page

[root@mail ~]# cat /usr/tomcat7/online_webapps/ROOT/index.jsp


<%@page import="java.util.Date"%>
<html>
<head>
<title>Java Curren Date and Time</title>
</head>
<body>
<h1> online.dyindia.com Current Time Is : </h1>

<%=new Date().toLocaleString() %>


</body>
</html>

Third context test page

[root@mail ~]# cat /usr/tomcat7/live_webapps/ROOT/index.jsp


<%@page import="java.util.Date"%>
<html>
<head>
<title>Java Curren Date and Time</title>
</head>
<body>
<h1> live.dkindia.com Current Time Is : </h1>

<%=new Date().toLocaleString() %>


</body>
</html>
Then service restart tomcat

Test all three website

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.

Clustering, Load Balancing, and Security

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

[root@mail ~]# wget http://archive.apache.org/dist/tomcat/tomcat-


connectors/jk/binaries/linux/jk-1.2.28/i586/mod_jk-1.2.28-httpd-2.2.X.so
[root@mail ~]# mv mod_jk-1.2.28-httpd-2.2.X.so /etc/httpd/modules/mod_jk.so
[root@mail ~]# chmod +x /etc/httpd/modules/mod_jk.so
[root@mail ~]# ls -al /etc/httpd/modules/mod_jk.so

13
-rwxr-xr-x 1 root root 692060 Mar 22 2009 /etc/httpd/modules/mod_jk.so

[root@QA-WebServer conf]# vim httpd.conf

LoadModule jk_module modules/mod_jk.so

Then restart apache service

[root@dkindia ~]# /etc/init.d/httpd restart


Stopping httpd: [ OK ]
Starting httpd: [ OK ]

Conform modules loaded in apache below command

[root@dkindia ~]# httpd -M |grep -i jk_module


httpd: Could not reliably determine the server's fully qualified domain name, using
dkindia.com for ServerName
jk_module (shared)
Syntax OK
Then edit apache main configuration file

[root@dkindia ~]# vim /etc/httpd/conf/httpd.conf


<VirtualHost *:80>
ServerAdmin test.dkindia.com
DocumentRoot /opt/data
ServerName test.dkindia.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
JkMount /* worker1
</VirtualHost>

# 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"

Create one file workers.properties

[root@dkindia ~]# cat /etc/httpd/conf/workers.properties


# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=192.168.11.197 # Server-IP
worker.worker1.port=8009

14
worker.worker1.connection_pool_size=100
worker.worker1.connection_pool_timeout=10

Then restart apche & tomcat service below commands

[root@dkindia ~]# /etc/init.d/httpd restart


Stopping httpd: [ OK ]
Starting httpd: [ OK ]

[root@dkindia ~]# /etc/init.d/tomcat restart


Restarting Tomcat
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
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.

Test page http://test.dkindia.com

http://live.dkindia.com

15
http://online.dyindia.com

Tomcat Web Application Manager


[root@dkindia ~]# cd /usr/tomcat7/conf/
[root@dkindia conf]# cat tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and

16
limitations under the License.
-->

<tomcat-users>
<role rolename="manager-gui"/>
<user username="admin" password="test1234" roles="manager-gui"/>
</tomcat-users>

After that restart tomcat service

17
How to configure
tomcat cluster

First stop tomcat

[root@dkindia usr]# /etc/init.d/tomcat stop


Stopping Tomcat
Using CATALINA_BASE: /usr/tomcat7
Using CATALINA_HOME: /usr/tomcat7
Using CATALINA_TMPDIR: /usr/tomcat7/temp

18
Using JRE_HOME: /usr/java
Using CLASSPATH: /usr/tomcat7/bin/bootstrap.jar:/usr/tomcat7/bin/tomcat-juli.jar

Create new directory copy all tomcat7 folder

[root@dkindia usr]# mkdir tomcat8


[root@dkindia usr]# cp -vr tomcat7/* tomcat8
[root@dkindia usr]# cd tomcat8/
[root@dkindia tomcat8]# pwd
/usr/tomcat8

Change second tomcat instance running port in server.xml file

[root@dkindia conf]# vim server.xml


22 <Server port="9005" shutdown="SHUTDOWN">

68 Define a non-SSL HTTP/1.1 Connector on port 9080


69 -->
70 <Connector port="9080" protocol="HTTP/1.1"
71 connectionTimeout="20000"
72 redirectPort="8443" />

91 <!-- Define an AJP 1.3 Connector on port 8009 -->


92 <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

Then service tomcat restart

19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy