Installing Tomcat 6.0.x Behind Apache 2.2.x

Apache TomcatInstalling Tomcat is relatively easy as most of the work has already been done for you. These instructions pertain to OS 10.5.6 and will probably work for OS 10.5.x and 10.4.x. Going before 10.4 will probably required you to install a newer JDK. I’m going to run through this as root again. Any time you see #, this is the command line prompt. Ok, let’s get started:

1) Check Apache
Find where httpd is with “find httpd”. You are either running the default in /usr/sbin/httpd or running your own version in /usr/local/apache2/ or similar. I’m going to assume you are running in /usr/local/apache2/ and that you followed my other tutorial on installing 64-bit Apache/PHP. Now check what modules you have installed with “/usr/local/apache2/bin/httpd -l” and you should see a list of mod_* entries.

We’re going to stick with the reverse proxy and not go the mod_jakarta route (trust me, this is easier and I’m not even sure mod_jakarta is even being worked on anymore). So what we need to see in this mod_* list is mod_rewrite and mod_proxy. If you followed my previous instructions, you’ll note that mod_proxy is NOT there. We will have to recompile Apache.

2) Compile Apache
Go into /usr/local/src/httpd-2.2.11/config.nice and add the line

"--enable-proxy"

right after the line

"--enable-rewrite"

then from the command line:

# ./config.nice
# make
# make install

This will only compile/install the bits needed unless you ran “make clean” previously. The config.nice is handy as it is a record of your config options allowing you to easily make updates and recompile. So now try the “/usr/local/apache2/bin/httpd -l” command again and you should now see mod_proxy (plus some others).

3) Install Tomcat
Download the binary tarball into your /usr/local/src/ directory using “curl -O”. The current version is 6.0.18 – http://apache.inetbridge.net/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz

Once you have this, move it into the proper place:

# tar -xzvf apache-tomcat-6.0.18.tar.gz
# mv apache-tomcat-6.0.18 /usr/local/
# ln -s /usr/local/apache-tomcat-6.0.18/ /usr/local/tomcat

As I mentioned in a previous tutorial, I like to leave the version info intact and symlink from a generic name. This allows me to have different versions of tomcat installed in the same location and switch between them by changing the symlink. We now need to set the JAVA_HOME environment variable. This is most easily done in /etc/bashrc by adding the line:

export JAVA_HOME="/Library/Java/Home"

to the end of the file.

So now check to make sure Tomcat is working by executing the startup script /usr/local/tomcat/bin/startup.sh. The default port is 8080 so fire up a browser and check http://localhost:8080/. You should see a page stating you set up Tomcat successfully.

4) Secure Tomcat
Open the file /usr/local/tomcat/conf/tomcat-users.xml and uncomment everything between “tomcat-users”. You will need to add two more roles and at least change the default passwords. The “tomcat-users” stanza should look something like this:

<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="{password}" roles="tomcat,admin,manager"/>
<user username="role1" password="{password}" roles="role1"/>
<user username="both" password="{password}" roles="tomcat,role1"/>
</tomcat-users>

I believe this change will be picked up by Tomcat, but just in case, from the Tomcat bin directory run “shutdown.sh” then “startup.sh”. You should now be able to access the Admin links on the default page at localhost:8080.

5) Configure Apache
In your Apache config file – /usr/local/apache2/conf/httpd.conf – create a VirtualHost stanza for the hostname to pass on to Tomcat. Here is an example for the hostname tomcat.latko.org:

<VirtualHost tomcat.latko.org:80>
ServerName tomcat.latko.org
ServerAlias tomcat.latko.org
ServerAdmin chris [at] latko [dot] org
DocumentRoot /Library/WebServer/Documents/tomcat
#APP
ProxyPass        / http://127.0.0.1:8080/app
ProxyPassReverse / http://127.0.0.1:8080/app
#MANAGER
ProxyPass        /manager http://127.0.0.1:8080/manager/
ProxyPassReverse /manager http://127.0.0.1:8080/manager/
RewriteEngine On
RewriteRule ^(.*)$ http://127.0.0.1:8080/app/$1 [P,L]
</VirtualHost>

The app is the specific application you want running at this hostname. If there is no specific app, you can remove it from the arguments so

ProxyPass        / http://127.0.0.1:8080/app

becomes

ProxyPass        / http://127.0.0.1:8080/

For further security, you should comment out both manager lines, making it inaccessible to anyone. Now going to http://tomcat.latko.org will take you directly to your Tomcat app or to the Tomcat root we saw at http://localhost:8080.

6) Tomcat Startup Configuration
You can increase the memory allocated to Tomcat by modifying the startup.sh script and adding the following line at the top:

JAVA_OPTS="-Xms128m -Xmx512m"

To have Tomcat automatically start on boot, create a file in /Library/LaunchDaemons/ called “org.apache.tomcat.plist” with the contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>com.apache.tomcat</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/tomcat/bin/startup.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

If you have any questions about this process, I would be happy to answer them.

Hey, like this post? Why not share it with a buddy?

Leave a Comment