The first thing you will need is a fresh copy of the ports tree. There are several ways to obtain this, all detailed in the FreeBSD handbook. I won't go into obtaining a fresh ports tree as it transcends the scope of this tutorial.
Next, we will build a native JDK 1.3 for FreeBSD. While there are patches for JDK 1.4 to build it out of the ports collection, they are currently not production quality. For the sake of stability, this tutorial will cover how to do a native build of JDK 1.3.
In order to build the JDK, you will need Linux compatibility enabled. You will need to add the following line:
To /etc/rc.conf and install the linux_base package. This can be accomplished with the following command:
pkg_add -r linux_base
Note that your system cannot be at a securelevel if you do this, because you must load kernel modules while running at a securelevel. If you get "Operation not permitted" when the linux_base package tries to load the Linux compatibility module, edit /etc/rc.conf and look for the following line:
And change YES to NO, then reboot.
Next, we will need to install a Linux JDK package. You will need to download the file j2sdk-1_3_1_08-linux-i586.bin from Sun and place it in /usr/ports/distfiles. Now cd to /usr/ports/java/linux-sun-jdk13 and type "make install". This will install the Linux JDK, which we will use to build our own native FreeBSD JDK.
To build a native JDK, you will need to manually fetch two files, the source to the JDK, and a set of patches for the JDK. After fetching these, place both in /usr/ports/distfiles. The JDK 1.3.1 sources may be found at http://wwws.sun.com/software/java2/download.html and the patchset at http://www.eyesbeyond.com/freebsddom/java/jdk13.html.
Now cd to /usr/ports/java/jdk13 and type "make install". If all goes well a native copy of the JDK will then be installed under /usr/local/jdk1.3.1. You will probably want to run "make clean" as building the JDK will use quite a bit of space. Also, at this point you can remove the Linux JDK. Run the following to remove it:
Now we will install Apache 2. This is quite simple, cd to /usr/ports/www/apache2 and type "make install". Configuration will be placed in /usr/local/etc/apache2/, edit it to your wishes.
Next we will install Tomcat. Just cd to /usr/ports/www/jakarta-tomcat41 and type "make install". This will also install other Jakarta tools like Apache Ant, a build tool which you might consider for JSP development.
The final phase of the installation is installing a connector. A connector allows Apache to act as a frontend for Tomcat and process requests for Java servlets. There are 3 connectors to consider, mod_jk2, Coyote, and mod_webapp. We will use mod_webapp because it provides the best performance and easiest configuration. The only drawback of mod_webapp is that it currently doesn't support load balancing, which is an issue that transcends the scope of this tutorial.
To build mod_webapp, fetch the file jakarta-tomcat-connectors-4.1.12-src.tar.gz and place it in /usr/ports/distfiles/mod_webapp (you will need to create the mod_webapp subdirectory). Now cd to /usr/ports/www/mod_webapp-apache2 and type "make install".
Now we need to configure Apache to use mod_webapp. Edit /usr/local/etc/apache2/httpd.conf. The first thing we need to do is ensure that the ServerName directive is valid and also contains the port number that our server listens on:
Now, look for the LoadModule section and add the following line:
LoadModule webapp_module libexec/apache2/mod_webapp.so
Now we will add configuration that tells mod_webapp how to connect to Tomcat. Place the following two lines at the end of your httpd.conf:
WebAppConnection warpConnection warp localhost:8008
WebAppDeploy example warpConnection /example
These directives take the following form:
WebAppConnection <connection name> <provider> <host:port>
WebAppDeploy <application name> <connection name> <application path>
You will want to change the application name and path to reflect where you are deploying JSP on your system. Add the following line to get information about your current mod_webapp configuration.
Now we need to configure Tomcat to listen on port 8008. The stock /usr/local/jakarta-tomcat4.1/conf/server.xml file contains a great deal of unnecessary options for our intended use, which is with mod_webapp. You will want to back it up or delete, then replace its contents with the following, remembering to change example.server.com to match the name given in your Apache configuration's ServerName field:
<Server shutdown="SHUTDOWN" debug="0">
<Connector className="org.apache.catalina.connector.warp.WarpConnector" port="8008" minProcessors="5" maxProcessors="75" enableLookups="true" scheme="http" secure="false" appBase="webapps" acceptCount="10" debug="0" />
<Engine className="org.apache.catalina.connector.warp.WarpEngine" name="example.server.com" debug="0">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="apache_log." suffix=".txt" timestamp="true" />
<Host className="org.apache.catalina.connector.warp.WarpHost" name="example.server.com" debug="0" appBase="webapps" unpackWARs="true">
<Context path="" docBase="ROOT" debug="0" />
After you have done this, start Tomcat by running:
And start Apache by running:
Now point a browser at http://yourserver/webapp-info/
If all is working information about mod_webapp should be displayed, in which case you may now begin using your FreeBSD-based Java Application Server.
About the Author:
Tony Arcieri is a Solaris system administrator by profession, working on a number of projects for the Colorado Climate Center developed in C, C++, Java, PHP, and Perl. He is also working on a number of open source projects, including libtorrent and phpkeychain.