Ubuntu Hardy - Installing Apache Tomcat
Apache Tomcat is a free and open source implementation for JavaServlets. It provides support for Java Server Pages (JSP), which power many popular web-based applications.You can run Tomcat with either Sun's Java implementation or OpenJDK, and this document provides instructions for both options.
Choose a Java Implementation
Before we begin, you must choose an implementation of the Java language. Note there are some variances between them, and you should install a version that is compatible with the application you're planning to run or write.OpenJDK
The "main" repository for Ubuntu comes with the "openjdk" implementation, which you would install with the following command:sudo apt-get install openjdk-6-jdk
If you do choose OpenJDK, then you can skip the remainder of this section.
Sun Java
Sun's licensing terms are considered non-free under the guidelines that govern inclusion in Ubuntu's "main" software repositories. So If you wish to install Sun Microsystems' implementation of Java, you must first edit your slice's 'sources.list' file.On your slice, use nano or your preferred text editor to append the following two lines to '/etc/apt/sources.list':
deb http://us.archive.ubuntu.com/ubuntu/ hardy multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ hardy multiverse
Update apt to get the necessary package lists:
sudo apt-get update
Now you're ready to install Sun Java with the following command (acknowledging the license terms):
sudo apt-get install sun-java6-jdk
With java in place on the slice, it's time to install Apache Tomcat.
Open Firewall for Port 8080
If you followed our initial setup guides for Ubuntu LTS, you'll probably need to open port 8080 in your slice's firewall. Otherwise, Tomcat will be inaccessible on your slice's public IP.Go ahead and run through the iptables section of Ubuntu LTS Setup Guide. Then add the following line to your iptables rules:
-A INPUT -p tcp --dport 8080 -j ACCEPT
Installing Apache Tomcat
Download the latest version of Tomcat with the following command:wget http://apache.mirrors.timporter.net/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.tar.gz
If you don't have wget installed, issue the command "sudo apt-get install wget" then try the download command again.
Extract the tomcat binary from the tarball with the 'tar' command:
tar -xzvf apache-tomcat-6.0.20.tar.gz
Then move the resulting Tomcat directory to a permanent location:
sudo mv apache-tomcat-6.0.20 /usr/local/tomcat
The scripts for controlling and interacting with Tomcat are located in the '/usr/local/tomcat/bin' directory.
Enable Tomcat to Start Automatically
Next create a tomcat "init" file:sudo nano /etc/init.d/tomcat
Fill it with the following:
# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid
export JAVA_HOME=/usr/lib/jvm/java-6-sun
case $1 in
start)
sh /usr/local/tomcat/bin/startup.sh
;;
stop)
sh /usr/local/tomcat/bin/shutdown.sh
;;
restart)
sh /usr/local/tomcat/bin/shutdown.sh
sh /usr/local/tomcat/bin/startup.sh
;;
esac
exit 0
If you installed OpenJDK instead of Sun Microsystem's Java, the export JAVA_HOME line should read:
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
Now make the script executable:
sudo chmod +x /etc/init.d/tomcat
Finally, create symbolic links in the startup folders:
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
Tomcat should now be totally functional and should start automatically when the slice reboots.
In the future, if you need to start, stop, or restart, you can use the following commands:
sudo /etc/init.d/tomcat start
sudo /etc/init.d/tomcat stop
sudo /etc/init.d/tomcat restart
Test and use Tomcat
Start Tomcat on your slice, and then test it by pointing a web browser to your slice's IP address. You should be greeted with Tomcat's standard "Congratulations!" page:
Note that by default, Tomcat servers content stored in '/usr/local/tomcat/webapps/ROOT/' .
Summary
Installing Tomcat is straightforward, whether you prefer OpenJDK or Sun's Java.Your slice is now equipped with a free and powerful means to host JavaServlets!
—
Greg


Article Comments:
John Hurst commented Wed Jan 27 19:32:09 UTC 2010:
Nice article.
I'd be inclined to create a "tomcat" user account to run Tomcat as, rather than running as root.
In the init.d script instead of
sh /usr/local/tomcat/bin/startup.sh
for example, you'd have something like:
su - tomcat -c /usr/local/tomcat/bin/startup.sh
and so on for other commands.
Regards
John Hurst
Eric commented Thu Mar 04 09:12:25 UTC 2010:
Thanks for the great article. One thing to note, if you came from the Ubuntu Hardy setup page linked above (http://articles.slicehost.com/2008/4/25/ubuntu-hardy-setup-page-1), you need to insert the iptables entry before the http(s) entries:
Allow Tomcat to run on port 8080
-A INPUT -p tcp --dport 8080 -j ACCEPT
Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT