Accessibility
Icon or Spacer
   

Running Servlets on Linux

by Tom Reilly

It's no secret that Linux and Apache make an excellent platform for deploying almost any kind of web site. However, it might not be as well known that you can replace those old workhorse CGI scripts with powerful Java servlets on Linux as well. Since these Java servlets are executed on an up and running Java Virtual Machine (instead of a whole new process like CGI), you will find that servlets will not only execute faster but that your web sites can do more with less hardware (or scale better with the same hardware). Using JRun, it is possible to get started using Java Servlets on Linux after a couple of easy steps.

Obtaining a JDK

The first thing you'll need is a JDK. A very solid port of Sun's JDK for Linux can be obtained at:

http://www.blackdown.org

A list of Blackdown mirrors that have the latest JDK (1.1.7B) can be found here:

http://www.blackdown.org/java-linux/mirrors.html

The only trick in obtaining the JDK is whether to get the libc5 or the glibc version. If you have a recent system (like RedHat 5.*) you'll need the glibc version. An older system would use the libc5 version. If you are not sure, here's an excerpt from Blackdown's distribution that may help:

Installation

Installation of the Linux JDK is trivial, but you have to get the version of the JDK that matches your environment. (If you are not on an x86 based Linux system, skip to the bottom of this section). For the x86 processor family, there are two flavors; the version you get depends on your environment. The versions are known as "glibc" and "libc5", and reflect the type of C runtime library that's installed on your machine. Generally, you should get the glibc version if your machine is running glibc, but libc5 should work acceptably as well, if you have a recent (say, past April 1, 1998) version of the glibc library installed on your machine (RedHat 5.0 by default comes with an older version of glibc, you need to get the 2.0.7-7 version from RedHat to win).

To discover which kind of system you have, (remember, these instructions are for x86 based Linux only):

ls -l /lib/libc.so.*

What you are looking for is lines of the form

/lib/libc.so.<num>

If all you see are lines where <num> is 5, then you have libc5. If you have a line where <num> is 6, then you have glibc, and you should get the glibc version. An additional check is to look in /lib for libdl.so.<num>: if at least one <num> here is 2, then you definitely have a glibc system and you should get the glibc JDK (although I believe the libc version will also work for you).

Setting up Apache

Once you have the JDK in place, you will need to make sure you have Apache set up correctly. If your Linux distribution happens to be RedHat 5.2 and you just want to get on to running Servlets, you can skip this section as it comes with Apache pre-configured for use with DSO (Dynamic Shared Object) modules.

Apache needs to make use of the JRun Apache module and this can be achieved via a DSO module or by compiling the module into your Apache server binary. Compiling the JRun module into the Apache binary is not described here as it is a more complicated process and if used will require you to recompile and reinstall Apache every time we release an updated JRun Apache module. Compiling it may result in a slightly faster startup time for each Apache child process (remember Apache is a multi-process web server), but this cost will be amortized over the number of requests each Apache child handles. Generally, performance tuning should be done after an analysis of the system has been done and compiling the JRun module is only recommended if you have ascertained that your machine is spending too much time creating new Apache processes (which would be better addressed by raising Apache's MaxRequestsPerChild setting anyway).

The only exception to this rule is if you for some reason have to use an older 1.2 version of Apache (upgrading to 1.3 is highly recommended). These earlier versions did not support DSO modules and so you will have to compile the module into the Apache binary. Please see the README.apache file in the JRun distribution for details on how to do this.

Building Apache with DSO support is fairly simple. After obtaining the latest source from your closest Apache mirror (http://www.apache.org/mirrors) you will need to unpack the distribution (ie 'tar -xzf apache_1.3.4.tar.gz'). Then cd into the newly created Apache source directory (it will be named something like apache_1.3.4) and configure apache like so:

$ ./configure --prefix=/usr/local/apache_dso \
--enable-rule=SHARED_CORE \
--enable-module=so 

After this completes run 'make' then 'make install' and that's it. Note that the prefix directory is where apache will be installed and is completely up to you, if you don't specify it '/usr/local/apache' will be used.

Obtaining and Installing JRun

For the final step you will need to visit http://www.livesoftware.com to obtain the distribution for the latest version of JRun. The JRun installation is available as a compressed (gzip and compress) tar archive that installs via the command line, just run the install.sh script after unpacking it.

During installation, both the command line and GUI installers will ask you for your Apache conf directory after you select Apache from the available servers list. This is so that we can put the necessary configuration lines in your httpd.conf file. After typing this in you will be asked to specify whether or not you are using DSO and which Apache version you have. Unfortunately Apache 1.3.4 uses a different module format than the previous 1.3 version and you this has to be specified correctly for JRun to work.

Once this install/configuration process is done go ahead and start/restart Apache and then start JRun, like so:

/path/to/JRun/jsm-default/jsmctl start

Then you can access some of the sample servlets (source is located in /path/to/JRun/servlets), for example:

http://localhost/servlet/SnoopServlet

Starting JRun at Boot Time

After you have retooled your site to use servlets you will no doubt want to have JRun start at boot time along with your web server. Here's a little script you can put in your /etc/rc.d/init.d directory.

View script

You can setup the run level links yourself or use the appropriate tool that come with your Linux distribution. Astute readers will notice it does a source on '/etc/jrun.conf' you can change the local of this is you want but here's what it should look like:

JRUNDIR=/path/to/JRun
JSMS=default

This will cause the startup script to execute:

'/path/to/JRun/jsm-default/jsmctl start'

If you have more than one JSM then you will want to add them to the JSMS variable with a space between them.

Conclusion

Now you are all set to get rid of all those old CGI scripts and re-write them in Java. If you are new to Java you'll want to visit the JavaSoft (TM) website (http://java.sun.com) to see all the things you can do with Java, for instance you will want to check out JDBC if you need database access in your servlets. Also, be sure to check out the Allaire Web site for mailing lists and newsgroups that may be useful. Enjoy!