Originally Published: Thursday, 29 March 2001 Author: Robert Eksten
Published to: enhance_articles_sysadmin/Sysadmin Page: 1/1 - [Printable]

System Administration of Apache / Tomcat

Learn all about the pros and cons of JSP web applications at the sysadmin level, including installation and configuration of Apache /Tomcat. This article will give you the basics on object-oriented development efforts, making scaling of your Web site a simple process. Jump into the world of Java Servlets and JSP with Linux!

   Page 1 of 1  

If your web development efforts have become disorganized or overly complex, servlets and Java Server Pages can help with good support and high quality tools for Linux. Both technologies also help by offering an object-oriented approach that promotes a modularized development effort. Using such an approach allows you to scale from small to enterprise size using the same codebase.

Tomcat is a servlet container and Java Server Pages (JSP) engine. It is open-source software produced by the members of the Jakarta Project, a project of the Apache Software Foundation. The Jakarta Project is open to any interested developers. For more information see the website.

If you aren't familiar with servlets, they are the Java equivalent of Common Gateway Interface (CGI), with an essential difference. Servlets run "in-process". That is, they are loaded by the HTTP daemon and held "in-process", waiting to be used and/or reused. As such, they are much more efficient and resource friendly than traditional CGI scripts. Because of the nature of acting "in-process", servlets can essentially be thought of as modules that extend the functionality of a web server.

Java Server Pages (JSP) is a highly accessible technology to extend the functionality of the Apache webserver. One of the unique aspects of Java Server Pages is that they are compiled into servlets, and interpreted at run-time as Java bytecode. If the site is tagged as reloadable, the servlet will be dynamically recompiled when changes are made to the site's JSP files.

Although Tomcat can be run as a stand-alone servlet container and JSP engine with the ability to serve HTTP, there are several reasons why you would want to have Tomcat run as an add-on to Apache. The most compelling is the amount of development effort that already exists in Apache. There are many good reasons why it has such a high market share. By using Tomcat as an Apache add-on, you gain the benefits of using existing code side-by-side with JSP and servlets.

The next part of this article will describe how to set up an Apache / Tomcat server environment. If you'd like to follow along, please make sure that you have a recent Java Developer's Kit (JDK) properly installed and configured on your system.

Setting up an Apache / Tomcat Installation

Assuming that do not have Apache installed, or you do not have Apache 1.3.6 or greater (Tomcat requires Apache version 1.3.6 or greater), the first step to setting up this environment is to download the source, or depending on your preference, the package containing the Apache webserver.

If you decide to install Apache from source, the current version available from the apache.org website is 1.3.19. According to the website, several security fixes are contained in the current release, so upgrading would be wise. To install the source distribution, unpack the distribution file, cd into the newly created directory and run the following command:

./configure --prefix=/usr/local/httpd

Use the prefix tag to set the installation path for Apache. Note that it's up to you as to what path to use. Other valid locations would be /var/httpd, or /usr/local/apache. I use /usr/local/httpd. Next, run this command:

make ; make install

These two commands will make the software and install the binaries and associated files.

Package Installation

Installing the software using package management software offers some benefits that can make for easier administration of the system. Primarily, the package contents can be more easily tracked and maintained, and for the most part the software is pre-configured.

Since my system is RPM (Red Hat Package Manager) based, I can offer the following information about RPM packages. If your system does not use RPM packages, then you may find more information outside the scope of this article.

In general, RPM packages can be installed from the command line using the following syntax:

rpm -Uvh .rpm

For example, on my SuSE system, the RPM file is apache-1.3.17-0.i386.rpm. It would be installed using:

rpm -Uvh apache-1.3.17-0.i386.rpm

You can download the appropriate file for your system.

One step you may consider before installing any RPM package is to view its information, and the files it will install.

rpm -qpi .rpm rpm -qpl .rpm | less

These commands print the package information and a list of files the package will install.

Installing Tomcat

Tomcat can be downloaded as either source code or binaries that can be found at the project website and its mirrors. To save time, I recommend downloading the binary distribution.

The steps for installing the binary package are straightforward. First move the distribution file, which -- as of this writing -- is jakarta-tomcat-3.2.1.tar.gz to the directory where you'd like to install Tomcat. Unpack the distribution using the following shell command:

tar -xzvf jakarta-tomcat-3.2.1.tar.gz

This creates a directory named jakarta-tomcat-3.2.1. I rename the directory tomcat, and place it under the /usr/local hierarchy, i.e., /usr/local/tomcat.

At this point, Tomcat is installed in "stand-alone" mode. There are some simple configuration steps to be completed. Although Tomcat serves HTTP content in stand-alone mode, Tomcat can be better utilized as a web server add-on.

Stand-alone mode would be primarily useful for use on workstations whose users are developing JSP's and servlets.

Configuring Tomcat is a one step process. You simply set the JAVA_HOME and TOMCAT_HOME environment variables. To start and stop Tomcat a start script (startup.sh) and a stop script (shutdown.sh) are provided. Set the environment variables as follows:

export JAVA_HOME=/usr/lib/jdk1.3 export TOMCAT_HOME=/usr/local/tomcat

Then run the start script:

$TOMCAT_HOME/bin/startup.sh.

You will want to customize the scripts to reflect your system's configuration.

Setting up Tomcat as a Server Add-on

Tomcat can function as an add on to Apache by using the mod_jk module.

mod_jk is a replacement for the older Jserv module which was made specifically for the Tomcat engine. Jserv supported an older version of the servlet API, than mod_jk, and future Jserv development efforts will be merged into the mod_jk source tree.

The procedure for setting up Tomcat as an Apache add-on is relatively straightforward.

Download the mod_jk.so file and copy it to your Apache libexec directory. This location will vary depending on the system you are using. It should be in the same directory as your other Apache modules. On my SuSE system, the path is /usr/lib/apache. It could also be /usr/local/apache/libexec, or another path depending on your system.

Additional Apache Configuration

The Apache httpd.conf file must be modified to allow for interaction with Tomcat.

When Tomcat starts up, it creates a file called mod_jk.conf-auto that should be included in your httpd.conf file. Because mod_jk.conf-auto is re-written each time Tomcat starts up, you should customize this file, and save it with a different filename. Use mod_jk.conf-auto as a template for your custom mod_jk.conf file.

I add the following line to the httpd.conf file:

Include /usr/local/tomcat/conf/mod_jk.conf-custom

Tomcat Documentation Files

The JSP v1.1 and Servlet v2.2 API comes bundled with the Tomcat distribution. This covers classes, interfaces, exceptions and errors for the included packages. Several examples of JSP and servlets are also included.

Tomcat Configuration Files

Tomcat's configuration files are XML files and can be edited with any text editor.

The primary configuration file is called server.xml, and is found in the $TOMCAT_HOME/conf directory. Your primary interest with server.xml will be with adding contexts and setting up logging. Setting up logging is a matter of pointing Tomcat to the log files directory location, and setting up the level or verbosity of logging.

Setting up contexts is a matter of editing the file. A context is a Web application that lives in the Tomcat hierarchy, and it must be described from server.xml to be recognized by Tomcat. An example from the distribution's server.xml file describes the /admin context:

<Context path="/admin"
docbase="webapps/admin"
crossContext="true"
debug="0"
reloadable="true"
trusted="false"> </Context>

Note that this is a similar construct to the <Directory> tag in httpd.conf.

A quick run-down of the elements. The docbase attribute is the home directory for the Web application. The crossContext attribute determines whether other contexts can be accessed. If the reloadable flag is set to true, the Web application will dynamically recompile itself if changes are made to the application's JSP. Finally, the trusted flag determines whether or not the application can access Tomcat internal functions. This is a security issue, and trusted applications should be placed under password protection. You can create new contexts by adding to the server.xml, using other contexts as templates.

The Web applications you are pointing to from the contexts should have the following directory structure. All Web applications should start with the following directory structure:

/ WEB-INF META-INF jsp images servlets

Other Considerations

There is a lot that I have not touched on regarding Tomcat, including Web archives (a method of packaging and distributing JSP applications), the contents of the API's, or built-in functionality exposed through the Java classes. Each of these topics would serve as a basis for its own complete article. But there are a couple of other points which may be helpful to the beginning Tomcat user. First, in the realm of database connectivity, in order to access data via JDBC in a servlet, place a copy of the JDBC driver file in the $TOMCAT_HOME/lib directory. Tomcat will place the driver in its CLASSPATH when it starts up.

Also, several integrated development environments (IDE) exist for Linux that facilitate development efforts with servlets and JSP. Among the notable are NetBeans and Forte for Java, from Sun, and Visual Age for Java, from IBM. There are also some IDE's available through Sourceforge and other locations.

Conclusion

There are advantages to using servlets, and Java Server Pages, in that they can modularize and thereby enhance the development effort. Tomcat is high-quality, open-source software. As is often the case with open source, and specifically in the case of Tomcat, administration issues may be made more difficult by a relative lack of supplementary documentation in comparison with other server-side progamming technologies. Of course, the creation of further Tomcat documentation is ultimately open to the community. More information on Java Servlets and Java Server Pages can be found on Sun's website.

*Tomcat is copyrighted by The Apache Software Foundation.





   Page 1 of 1