Você está na página 1de 22

Copyright © 2006

Pakistan Software Export Board (G) Limited


Ministry of Information Technology
Government of Pakistan

Printing
Artland Communications, Lahore. September 2006

Published by
Pakistan Software Export Board

The Funding Agency


This open source toolkit is funded by the Open Source Resource Center (OSRC) project of
the Pakistan Software Export Board (PSEB). PSEB is the entity within Government charged
with the task of enhancing exports of software and IT enabled services (ITES) from Pakistan.
PSEB is a guarantee limited company totally owned and funded by the Government of
Pakistan. Any questions or comments about this toolkit may be directed to PSEB Islamabad
at 92-51-111-333-666 or through e-mail at osrc@pseb.org.pk.

Disclaimer
This toolkit is published by the PSEB for members of the IT industry and the public-at-large.
The toolkit’s compilers, or the editor, are not responsible, in any way possible, for the
errors/omissions of this toolkit. The OSRC does not accept any liability for any direct and
consequential use of this toolkit or its contents. The contents of this toolkit may be distributed
only subject to the terms and conditions set forth in the Open Publication License v 1.0 or
later. The latest version is presently available at http://opencontent.org/openpub/

i
TABLE OF CONTENTS
INTRODUCTION...............................................................................................................................................1
JBOSS: AN INTRODUCTION..............................................................................................................2
1. JBOSS INSTALLATION..................................................................................................................................3
1.1. JDK Installation.............................................................................................................................3
1.2. Environment Variables..................................................................................................................3
1.3. JBoss jar installation.....................................................................................................................3
1.4. Uninstalling JBoss jar-installation................................................................................................4
1.5. Tar installation..............................................................................................................................5
2. JBOSS ADMINISTRATION..............................................................................................................................5
2.1. JBoss - initial directory structure .................................................................................................5
2.2. Starting and Stopping JBoss..........................................................................................................5
2.3. JDBC Datasources........................................................................................................................5
2.4. Creating mysql datasource............................................................................................................5
2.5. mysql-ds.xml .................................................................................................................................5
2.6. Web applications ..........................................................................................................................6
2.7. Getting datasource.........................................................................................................................6
2.8. Deploy web applications in JBoss.................................................................................................6
3. REFERENCES..............................................................................................................................................7
JBOSS –TOMCAT BUNDLE WITH APACHE INTEGRATION.....................................................8
1. INSTALL APACHE WEB SERVER.....................................................................................................................9
2. INSTALL JBOSS APPLICATION SERVER.............................................................................................................9
3. INTEGRATION WITH APACHE ALREADY INSTALLED............................................................................................9
4. CLUSTERING AND LOAD BALANCING.............................................................................................................12
4.1. Optional steps..............................................................................................................................15
5. REFERENCES............................................................................................................................................15
APACHE TOMCAT .............................................................................................................................16
1. INTRODUCTION TO TOMCAT........................................................................................................................17
2. INSTALLING TOMCAT................................................................................................................................17
3. TOMCAT DIRECTORIES AND FILES.................................................................................................................18
3.1. Directory Layout for Web Applications.......................................................................................18
4. EXAMPLE APPLICATION.............................................................................................................................18
4.1. Static Deployment........................................................................................................................18
5. APACHE WITH TOMCAT.............................................................................................................................19
5.1. Step-by-step Guide to Integrating Apache with Tomcat..............................................................19
5.1.1. Apache not already installed ............................................................................................................19
5.1.2. Already Installed Apache.................................................................................................................19
5.1.3. Installing mod_jk.............................................................................................................................19
5.1.4. Configuring mod_jk for Apache 2....................................................................................................20
5.1.5. Testing the configuration with an example setup..............................................................................20
6. REFERENCES............................................................................................................................................21

i
Introduction

This open source toolkit has been developed by the Open Source Resource Center (OSRC),
a project of the Ministry of Information Technology (MoIT). This toolkit contains step-by-step
manuals related to open source applications for databases, application servers, desktop
applications, office productivity suites, Enterprise Resource Planning (ERP) and Customer
Relationship Management (CRM) software, and open source desktop applications for the
Microsoft Windows platform. A set of CDs, including some Linux distributions and other
applications, forms an integral part of this open source toolkit.

I would like to thank the OSRC team, including Mr. Abubakar Shoaib, Mr. Iftikhar Ahmad, Mr.
Muhammad Hammmad, Mr. Muazzam Ali, Mr. Sher Shah Farooq, and Mr. Qandeel Aslam,
who have compiled this toolkit; and Miss Seema Javed Amin, who has edited it. The OSRC
would especially wish to thank PSEB’s Director (Projects) Mr. Nasir Khan Afridi, Former
Project Manger(OSRC) Mr. Osman Haq and Ministry of Information Technology's Member
(IT) Mr. M. Tariq Badsha for their generous moral support, without which this toolkit would
never have been completed.

This is the first edition of this toolkit, and the OSRC hopes to continue to improve it with the
help of your feedback and comments.

Sufyan Kakakhel
Open Source Resource Center,
Pakistan Software Export Board,
2nd Floor, ETC, Agha Khan Road, F-5,
Islamabad, Pakistan.
Ph: +92-51-9208748
Fax: +92-51-9204075
Email: skakakhel@pseb.org.pk
http://www.osrc.org.pk

Open Source Software Training Toolkit 1


JBoss: An Introduction

Application Servers Configuration 2


The JBoss server is available in two formats - binaries and code. The binaries versions are
available in zip and tar.gz formats. The .jar format is also available for JBoss installation.

JDK is a prerequisite for running JBoss and for .jar version installation. Ensure that the
JAVA_HOME variable is set in environment variables, and that you have installed the latest
version of JDK. JBoss 4.0.x and JDK 1.5.x versions have been selected for the purposes of
this tutorial. Download the latest versions from http://java.sun.com/ and http://www.jboss.org
respectively.

1. JBoss Installation

1.1. JDK Installation


1) Check for other Java sdk’s
$ rpm -qa | grep java
2) Uninstall if any other sdk is already installed on your system
$ rpm -e gcc-java-3.2.3-20 --nodeps
3) Download jdk-1_5_0_06-linux-i586.bin from http://java.sun.com/
4) Install JDK
$ jdk-1_5_0_06-linux-i586.bin

1.2. Environment Variables


Edit file profile gedit /etc/profile and add the following lines using the paths where JDK and
Apache Ant is installed. Apache Ant is a build tool from Jakarta Apache that may be helpful in
many cases. For more information about Apache Ant, please visit its official website at
http://ant.apache.org/.

---------------------------------------------------
CLASSPATH=.
PATH=$PATH:/opt/jdk1.5.0_06/bin:/opt/apache-ant-1.6.5/bin:
export CLASSPATH PATH

JAVA_HOME=/opt/jdk1.5.0_06/
export JAVA_HOME
---------------------------------------------------

Save the profile file and load this file by using the following command:

$ source /etc/profile

1.3. JBoss jar installation


1) Type command java jar jboss-4.0.3SP1- installer.jar
The Language screen appears:

Open Source Software Training Toolkit 3


2) Press OK.

After selecting the language, the installation folder options and JBoss default server
configuration options will appear. You have to select the default server and configuration for
JBoss jar installation for this tutorial.

1.4. Uninstalling JBoss jar-installation


1) Enter into the JBoss directory
2) Enter into the Uninstaller directory
3) Type command java -jar uninstaller.jar

1.5. Tar installation


1) Download jboss-4.0.3SP1.tar.gz from http://www.jboss.org
2) Extract this file to your location of choice
Let say /opt/
3) Type command tar xvfz jboss-4.0.3SP1.tar.gz
4) This will make jboss-4.0.3SP1 directory
Your tar installation is complete.
5) Tar installation contains the following JBoss configurations:
Jboss all
Jboss default

Application Servers Configuration 4


Jboss minimal

2. JBoss Administration

2.1. JBoss - initial directory structure

jboss-4.0.3 JBOSS_HOME
/bin contains start and stop scripts
/client contains libriaries for client
/docs contains jboss documentation
/lib contains jboss library
/server contains all,default,minimal server configurations
/all all server configuration
/default default server configuration
/conf
/data
/deploy deployment directory for applications
/lib default server lib
/log
/tmp
/work
/minimal

2.2. Starting and Stopping JBoss


1) Starting ./run.sh
2) Starting with specific configuration
./run.sh -c default
3) Stopping ./shutdown.sh -s default

2.3. JDBC Datasources


1) For database connections, we have to add the datasource in JBoss
2) Creating MySQL datasource
3) Sample datasources are placed at jboss- 4.0.3SP1/docs/examples/jca
4) Copy mysql-ds.xml and paste it into jboss-4.0.3SP1/server/default/deploy
5) Edit mysql-ds.xml according to your database connection

2.4. Creating mysql datasource


1) You need MySQL connector file for mysql jdbc connections
2) Un-extract mysql-connector-java- 3.1.12.tar.gz
3) Copy /mysql-connector-java- 3.1.12/mysql-connector-java-3.1.12- bin.jar
4) Paste this JAR file to Jboss [SERVER]/jboss- 4.0.3SP1/server/default/lib

2.5. mysql-ds.xml
Unique JNDI name
<jndi-name>MySqlDS</jndi-name>

Connection URL to the database


<connection-url>jdbc:mysql://localhost:3306/mydatabase </connection-url>

Driver class
<driver-class> com.mysql.jdbc.Driver</driver-class>

Database user name


<user-name>myusername</user-name>

Database user password


<password>mypassword</password>

Open Source Software Training Toolkit 5


2.6. Web applications
Java web applications in JBoss contain one extra configuration file other than normal Java
web applications, which is jboss-web.xml. This file contains configuration details specific to
applications for JBoss.

Base directory/
.jsp (jpp file)
.html (html files)
WEB-INF/
Classes/
Lib/
Jboss-web.xml
Web.xml

2.7. Getting datasource


1) Context initCtx = new InitialContext();
2) Context envCtx = (Context) initCtx.lookup("java:");
3) DataSource ds = (DataSource) envCtx.lookup("MySqlDS");
4) conn = ds.getConnection();

2.8. Deploy web applications in JBoss


There are two ways of deploying web applications in JBoss:
1) Create directory.war and copy it into jboss-4.0.3SP1/server/default/deploy
2) Create war file of specified application and copy it into
jboss-4.0.3SP1/server/default/deploy
Command
jar -cvf sample.war *.*

3. References
• JBoss.ORG: http://www.jboss.org
• The Apache Software Foundation: http://www.apache.org/

Application Servers Configuration 6


JBoss –Tomcat Bundle with Apache
Integration

Open Source Software Training Toolkit 7


1. Install Apache web server
Install the Apache web server (HTTP server) if it is not already installed on the machine. The
latest version can be downloaded from its official website at http://www.apache.org/

2. Install JBoss application server


1) Download and install the JBoss application server, available from http://www.jboss.org/
2) JBoss is available in many different formats; download the latest version.
3) JBoss requires Java. Download J2SDK or JRE from http://java.sun.com/

3. Integration with Apache already installed


The following is a step-by-step guide to the integration of the Apache web server with JBoss,
bundled with the Tomcat application server:

1. Download the modjk module from the Apache website. Currently, the latest version is
2.1.5. Alternatively, you can download the old version 2.0.6 for Apache-Tomcat
integration only: jakarta-tomcat-connectors-jk-1.2.6-linux-fc2-i386-apache-2.0.50.so
The source code of version 2.1.5 has been used for the purposes of this tutorial.

2. Install the httpd-devel rpm package if it is not already installed. Check this package by
using the command rpm -qa | grep httpd-devel. This package contains the
development tools APXS of Apache httpd development, and is required for mod_jk
recompilation.

3. Follow the steps outlined below to make the mod_jk.so module:

----------------------------------------------
$> cd native
$> ./configure --with-apxs=/usr/sbin/apxs (or wherever the apxs/apxs2 is)
$> make
$> su -c 'make install
-----------------------------------------------

4. Enter [mod_jk source]/jk/native/apache-2.0/ and locate mod_jk.so


5. Copy this module to the Apache modules directory

/usr/lib/httpd/modules/

6. Edit the Apache configuration file httpd.conf located in the directory

/etc/httpd/conf

Add the following lines at the end of the file to load module JK for Apache and JBoss-
Tomcat integration:

------------------------------------------------------
# Include mod-jk specific configuration file
Include conf/mod-jk.conf
------------------------------------------------------

7. Create file mod-jk.conf if it does not exist in the Apache conf directory

/etc/httpd/conf

8. There are two types of mod_jk configuration. The first is for the old mod_jk 2.0.6. The
second is for the new 2.1.15.

Application Servers Configuration 8


Edit the mod-jk.conf file and paste the following lines into this file.

Old mod-jk.conf 2.0.6

------------------------------------------------------

# Load mod_jk module


# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties


JkWorkersFile conf/workers.properties

# Where to put jk logs


JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]


JkLogLevel infgedit o

# Select the log format


JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicates to send SSK KEY SIZE


JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

# Mount your applications


JkMount /servlet/* ajp13
JkMount /*.jsp ajp13
JkMount /*.html ajp13

------------------------------------------------------

New mod-jk.conf for 2.1.5

------------------------------------------------------

# Load mod_jk module


# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties


JkWorkersFile conf/workers.properties

# Where to put jk logs


JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]


JkLogLevel info

# Select the log format


JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicates to send SSK KEY SIZE


JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

Open Source Software Training Toolkit 9


# Mount your applications
# Edited by me JkMount /application/* loadbalancer
#JkMount /* loadbalancer

JkMount /servlet/* loadbalancer


JkMount /*.jsp loadbalancer
JkMount /*.html loadbalancer

# You can use external file for mount points.


# It will be checked for updates each 60 seconds.
# The format of the file is: /url=worker
# /examples/*=loadbalancer
#JkMountFile conf/uriworkermap.properties

# Add shared memory.


# This directive is present with 1.2.10 and
# later versions of mod_jk, and is needed for
# for load balancing to work properly
# not for old jk versions JkShmFile logs/jk.shm

# Add jkstatus for managing runtime data


<Location /jkstatus/>
JkMount status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>

------------------------------------------------------

9. Create another file named workers.properties in the directory

/etc/httpd/conf

Add the following content to this file:

old workers.properties for mod_jk 2.0.6

------------------------------------------------------
# Define list of workers that will be used
# for mapping requests
worker.list=ajp13

# Define ajp13
# modify the host as your host IP or DNS name.
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
worker.ajp13.lbfactor=1
worker.ajp13.cachesize=10

------------------------------------------------------

new workers.properties for mod_jk2.1.5

------------------------------------------------------

# Define list of workers that will be used


# for mapping requests
worker.list=loadbalancer,status

Application Servers Configuration 10


# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
worker.node1.host=[your IP or domain]
worker.node1.type=ajp13
worker.node1.lbfactor=1
ndex.jspworker.node1.cachesize=10

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1
worker.loadbalancer.sticky_session=1

#worker.list=loadbalancer
# Status worker for managing load balancer
worker.status.type=status

------------------------------------------------------

10. Enter the JBoss-Tomcat default server configuration directory located in


[jboss-4.x]/server/default/deploy/jbossweb-tomcat55.sar/META-INF/

Edit the following file:

jboss-service.xml

Change the XML tag value from False to True

------------------------------------------------------
<attribute name="UseJK">false</attribute>
changed to
<attribute name="UseJK">true</attribute>
------------------------------------------------------

Create a test web application in the


[jboss-4.x]/server/default/deploy/
directory and test whether Apache is working with JBoss.

For more information regarding web applications, please see the guide to “JBoss Installation
and Administration”.

4. Clustering and load balancing


Packing two or more JBoss application servers so that they work like a single server and
share their load is known as a cluster. The JBoss cluster contains JBoss servers called
nodes. To start JBoss in the cluster environment, type:

jboss -c all

The nodes in the cluster join in ClusterPartation Mbean in the deploy/cluster-service.xml. All
nodes that have the same ClusterPartation MBean configuration join the same cluster. If, for
example, you want to divide JBoss nodes in a network into two clusters, you can make two
different ClusterPartition MBean configurations, and each node will have one of the two
configurations, depending on which cluster it needs to join.

The primary purpose of clustering HTTP services is session state replication, which is
handled by JBoss itself when it starts the configuration. Load balancing, however, needs to be
implemented with different software.

Open Source Software Training Toolkit 11


Apache mod_jk is very good software for load balancing. To work Apache mod_jk with JBoss
load balancing, change only step 7 of the Apache JBoss integration.

Edit the already created file workers.properties in the directory

/etc/httpd/conf

Add the following content to this file:

workers.properties for mod_jk2.1.5

------------------------------------------------------

# Define list of workers that will be used


# for mapping requests
worker.list=loadbalancer,status

# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
worker.node1.host=node1.mydomain.com
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.cachesize=10

# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host= node2.mydomain.com
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.cachesize=10

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1

#worker.list=loadbalancer
# Status worker for managing load balancer
worker.status.type=status

------------------------------------------------------

Now configure the JBoss node as well, so that they work with Apache mod_jk in a load
balancing environment.

In every clustered JBoss node, specify the node element according to workers.properties.

In the JBOSS_HOME/server/all/deploy/jbossweb-tomcat50.sar/server.xml file (replace /all


with your own server name if necessary).

Locate the <Engine> element and add an attribute jvmRoute:

For first node


------------------------------------------------------
<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">
... ...
</Engine>
------------------------------------------------------

Application Servers Configuration 12


For second node

------------------------------------------------------
<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node2">
... ...
</Engine>
------------------------------------------------------

For both nodes

Enter the JBoss-Tomcat default configuration directory located in

[jboss-4.x]/server/default/deploy/jbossweb-tomcat55.sar/META-INF/index.jsp

Edit the following file:

jboss-service.xml

Change the XML tag “UseJK” value from true to false

------------------------------------------------------
<attribute name="UseJK">true</attribute>
changed to
<attribute name="UseJK">false</attribute>
-----------------------------------------------------

Enter the JBoss-Tomcat all configuration directory located in

[jboss-4.x]/server/all/deploy/jbossweb-tomcat55.sar/META-INF/

For more information regarding web applications, please see the guide to “JBoss Installation
and Administration”.

Edit the following file:

jboss-service.xml

Change the XML tag “UseJK” value from false to true

------------------------------------------------------
<attribute name="UseJK">false</attribute>
changed to
<attribute name="UseJK">true</attribute>
------------------------------------------------------

In order to test the configurations, create an application with the same directory name. Add
example.war in both nodes [jboss-4.x]/server/all/deploy/ directory and create a test.jsp file in
both to display the following content:

test.jsp on node1

<% out.print(“Node1”); %>

test.jsp on node2
<% out.print(“Node2”); %>

By accessing any node, you can see either Node 1 or Node 2 on the browser window,
confirming that the Apache load-balancing is working.

Open Source Software Training Toolkit 13


For more information regarding web applications, please see the guide to “JBoss Installation
and Administration”.

4.1. Optional steps


For session replication, add the following element in the web.xml of your web applications:

------------------------------------------------------
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<distributable/>
<!-- ... -->
</web-app>
------------------------------------------------------

Now you can further configure session replication by editing the jboss-web.xml file and use
the following replication-config tag:

------------------------------------------------------
<jboss-web>
<replication-config>
<replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-
trigger>
<replication-granularity>SESSION</replication-granularity>
</replication-config>
</jboss-web>
------------------------------------------------------

5. References
• JBoss: http://www.jboss.org/
• The Apache Software Foundation: http://www.apache.org/

Application Servers Configuration 14


Apache Tomcat

Open Source Software Training Toolkit 15


1. Introduction to Tomcat
Tomcat is a Servlet/JSP container developed and maintained by the Apache Software
Foundation. The Apache web server does not handle dynamic request pages containing JSP
or Servlet, although it can be configured to automatically forward such requests to Tomcat,
which will be discussed later in this chapter. Tomcat, unlike Apache, which is written in C/C+
+, is developed in Java, and Java is therefore a prerequisite for installing and configuring
Tomcat. This guide is based on Apache Tomcat 5.5 that implements Servelt 2.4 and
JavaServer Pages 2.0 specifications from the Java Community Process.

2. Installing Tomcat
Tomcat can be compiled in both source and binary forms. This guide will only demonstrate
installation from the binary form. The Tomcat binary distribution comes in a variety of flavors
i.e. core, deployer, embedded, administration web application, and the JDK 1.4 computability
package. The core distribution is a superset of all others and will be used throughout this
guide.

• Configure Java from http://java.sun.com/

• For Tomcat 5.5, Java 5.0 is the recommended version, which can be downloaded
from
http://192.18.108.216/ECom/EComTicketServlet/BEGIN5A43F36CC69A6135A09
EB2C09E7982E8/-
2147483648/1396678059/1/681986/681830/1396678059/2ts+/westCoastFSEND/
jdk-1.5.0_06-oth-JPR/jdk-1.5.0_06-oth-JPR:3/jdk-1_5_0_06-linux-i586-rpm.bin
• Make sure you have uninstalled other Java compilers such as gcj from the
system.
# rpm -qa |grep gcj
# rpm -qa |grep java
Use rpm -e package-name uninsall.
• Change permissions
# chmod 555 jdk-1_5_0_06-linux-i586-rpm.bin
• Run the installer
# jdk-1_5_0_06-linux-i586-rpm.bin
• Create an entry in /etc/profile so that you don’t have to write the full path
whenever Java is required.
# vi /etc/profiles
Add the following lines:

---------------------------------------
export JAVA_HOME=/usr/java/jdk1.5.0_06/bin
export PATH=$PATH:$JAVA_HOME/bin
---------------------------------------

• Log out and login again from the system.


• Download Tomcat from http://apache.seekmeup.com/tomcat/tomcat-
5/v5.5.16/bin/apache-tomcat-5.5.16.tar.gz
• Untar the package
# tar zxvf apache-tomcat-5.5.16.tar.gz
• Move the folder to /usr. This is not compulsory, and you can put it anywhere you like.
# mv apache-tomcat-5.5.16 /usr/tomcat-5.5
This directory /usr/tomcat-5.5 will now be referred to as CATALINA_HOME.
• In order to edit /etc/profile, add this path:
export CATALINA_HOME=/usr/tomcat-5.5
• Logout and login again to reflect the modified effects.

Tomcat has now been installed. Run Tomcat by running the startup script in
$CATALINA_HOME/bin directory.
# ./startup.sh

Browse to http://localhost:8080.

Application Servers Configuration 16


The Tomcat core package does not include the administration interface by default. It has to
be downloaded separately from http://tomcat.apache.org/download-55.cg. After you have
downloaded the file, unzip it and place it under $CATALINA_HOME. This will update some of
the existing directories.

Tomcat includes a username and password in a special file tomcat-users.xml under the conf
directory. In order to access the Tomcat manager and admin interface, you have to assign
these roles explicitly. Copy the following line and place it in tomcat-users.xml file in place of
tomcat user.
-------------------------------------------
<user username="tomcat" password="tomcat" roles="standard,tomcat,admin,manager"/>
-------------------------------------------

Restart Tomcat and browse to http://localhost:8080 and access the manager and admin
interface.

3. Tomcat directories and files


• /bin: This directory contains startup, shutdown and other scripts for both UNIX and
Windows systems, differentiable by .sh and .bat.
• /conf: It contains configuration files and the most important is server.xml, which is
the main configuration file.
• /logs: Log files.
• /webapps: Web applications folder.

3.1. Directory Layout for Web Applications


A web application can be defined in two forms: either in an “Unpacked” form, where each file
and directory exists as a normal filesystem layout; or in a “Packed” form known as Web
Archive or WAR form. During development, normally the former structure is used, whereas
the latter is used for deployment.

Each web application is typically deployed under a single directory known as the document
root. This directory contains HTML and JSP pages that define the startup interface of your
web application. Place your web application’s executable files in WAR format, which will be
run by Tomcat. This format is discussed below:

1. *.html, *.jsp, etc: These pages define the startup interface, and must therefore be
visible to the client and placed directly under document root.
2. /WEB-INF/web.xml: This XML file defines all the parameters and other information
about the application required to the server.
3. /WEB-INF/classes: This directory contains Java classes required by the application.
4. /WEB-INF/lib: Any jar file(s) required by the application.

4. Example Application
Web applications can be deployed either statically or dynamically. In the former, applications
can be placed in the Tomcat document root folder and the Tomcat instance is restarted to
take effect. In the latter, the Tomcat manager tool can be used to deploy applications
dynamically without restarting the server instance. In Tomcat, there are also other possible
ways to deploy web applications, such as the client deployer package. A detailed guide can
be found in the “References” section. The simplest way is to deploy applications statically.

4.1. Static Deployment


Download and run the sample example application from http://tomcat.apache.org/tomcat-5.5-
doc/appdev/sample/sALL_LOCATIample.war.

1. Download the file from the above URL and move it to $CATALINE_HOME/webapps.
2. Restart Tomcat and browse to http://localhost:8080/sample

Open Source Software Training Toolkit 17


Use # jar -xvf sample.war to extract files from sample.war and see the directory’s structure.

5. Apache with Tomcat


Tomcat runs at port 8080, whereas Apache runs at 80 by default. In order to access Tomcat,
you have to explicitly place the Tomcat port in the URL. In order to achieve transparency,
where users are not aware of how the system is running and providing services, you can
integrate both Apache and Tomcat. In this case, Apache will automatically redirect requests to
Tomcat.

5.1. Step-by-step Guide to Integrating Apache with Tomcat


Note: For the purposes of this guide, it is assumed that you are running Apache 2 or above
and Tomcat 5 or above.

5.1.1. Apache not already installed

Apache is pre-installed on most of the Linux distributions, in which case skip to the next
section. If not, then:

1) Download Apache from http://httpd.apache.org/download.cgi


2) Unpack the distribution in /usr/local. From now onwards, in this section, APACHE_DIST
will be considered as the path of the Apache distribution.
3) Run configure with the following options in the directory where you unpacked the
distribution i.e. APACHE_DIST.
$ ./configure --with-layout=Apache --prefix=/usr/local/apache2 --enable-
module=most --enable-mods-shared=most
4) Run make to compile the distribution in APACHE_DIST
$ make
5) Install Apache by running the following command in APACHE_DIST
$ make install
This will install Apache in /usr/local/apache2.

5.1.2. Already Installed Apache

In most cases, Apache is pre-installed with Linux distributions. In Fedora Core 2/3, for
example, Apache’s home location is /etc/httpd. We will refer to APACHE_HOME as its home
directory, which could be /usr/local/apache2 or /etc/httpd, depending on whether you built
Apache yourself, or used the pre-installed version.

In order to work around with the pre-installed Apache, you need to install the Apache
Extension Tool (APXS). “APXS is a tool for building and installing extension modules for the
Apache HyperText Transfer Protocol (HTTP) server.”

The following packages must be installed: httpd-devel, apr, apr-util, apr-devel, apr-util-devel
and pcre-devel. Download and install these packages from appropriate locations, possibly
httpd://rpmfind.net. Make sure that the httpd-devel matches exactly with the installed httpd.

5.1.3. Installing mod_jk

mod_jk is a connector module for Apache that communicates with Tomcat. It is recommended
that you read the documentation for the connectors at http://tomcat.apache.org/connectors-
doc/index.html.

1. Download the connector from http://tomcat.apache.org/download-connectors.cgi. JK


1.2.15 is currently the latest version.
2. Unpack the distribution.
3. Change directory to LOCATION_WHERE_UNPACKED/jk/native
4. Run configure with the following option:
# ./configure –--with-apxs=PATH_TO_APXS –--enable-EAPI
where PATH_TO_APXS is either /usr/sbin/apxs or

Application Servers Configuration 18


APACHE_HOME/bin/apxs.
Use the following command to locate the APXS path
# which apxs
5. Compile by running make
# make
6. Now copy the .so file to Apache modules folder.
# cp LOCATION_WHERE_UNPACKED/jk/native/apache-2.0/mod_jk.so
$APACHE_HOME/modules

5.1.4. Configuring mod_jk for Apache 2

1) Create the file workers.properties in APACHE_HOME/conf as:-

workers.tomcat_home=$CATALINA_HOME
workers.java_home=$JAVA_HOME
ps=/
worker.list=default

worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1

2) Edit the Apache configuration file


1. Add the following lines in LoadModules section
LoadModule jk_module modules/mod_jk.so
2. Look for the httpd port directive Listen in the config file and make sure it is 80.
3. Add the following lines before section 3, Virtual Hosts

# Mod_jk settings
JkWorkersFile "conf/workers.properties"
JkLogFile "logs/mod_jk.log"
JkLogLevel error
JkMount /jsp-examples default
JkMount /jsp-examples/* default
# End of mod_jk settings

3) Test the configuration file settings


$ apachectl configtest
The response must be Syntax Ok.

5.1.5. Testing the configuration with an example setup

1) Run Tomcat
# CATALINA_HOME/bin/catalina.sh start
2) Start the Apache service after a 10 second gap. This is to ensure that Tomcat has been
started fully.
# service httpd start
3) Open your browser of choice and point to http://localhost and http://localhost:8080 to verify
that both Apache and Tomcat are running.
4) Open http://localhost/jsp-examples to execute any example on the page. Note that port
8080 has not been specified. Now Apache will automatically redirect the request to
Tomcat.
5) You can also create your own test application. Execute the following steps:

1. Create a folder named test in CATALINA_HOME/webapps.


2. Create a subfolder: test/WEB-INF
3. Edit the Apache configuration file to tell Apache to redirect pages in test directory to

Open Source Software Training Toolkit 19


Tomcat. Add these lines before section 3:
.......................
JkMount /test default

JkMount /test/* default


...................

4. Create index.html and hello.jsp in CATALINA_HOME/webapps/test as:

index.html

<HTML>
<HEAD>
<TITLE>Say Hello!</TITLE>
</HEAD>
<BODY>
<A HREF="hello.jsp">Hello World</A>
</BODY>
</HTML>

hello.jsp

<html>
<head>
<title>Hello World! </title>
</head>
<body>
<%@ page language="java" %>
<% out.println("Hello World"); %>
</body>
</html>

6) Open http://localhost/test

6. References
• Apache Tomcat by The Apache Software Foundation: http://tomcat.apache.org/

Application Servers Configuration 20

Você também pode gostar