You can run Tomcat with either Sun's Java implementation or OpenJDK, and this document provides instructions for both options.
Choose a Java ImplementationBefore 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.
OpenJDKThe "main" repository for Ubuntu comes with the "openjdk" implementation, which you would install with the following command:
If you do choose OpenJDK, then you can skip the remainder of this section.
sudo apt-get install openjdk-6-jdk
Sun JavaSun'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':
Update apt to get the necessary package lists:
deb http://us.archive.ubuntu.com/ubuntu/ hardy multiverse deb-src http://us.archive.ubuntu.com/ubuntu/ hardy multiverse
Now you're ready to install Sun Java with the following command (acknowledging the license terms):
sudo apt-get update
With java in place on the slice, it's time to install Apache Tomcat.
sudo apt-get install sun-java6-jdk
Open Firewall for Port 8080If 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 TomcatDownload the latest version of Tomcat with the following command:
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:
Then move the resulting Tomcat directory to a permanent location:
tar -xzvf apache-tomcat-6.0.20.tar.gz
The scripts for controlling and interacting with Tomcat are located in the '/usr/local/tomcat/bin' directory.
sudo mv apache-tomcat-6.0.20 /usr/local/tomcat
Enable Tomcat to Start AutomaticallyNext create a tomcat "init" file:
Fill it with the following:
sudo nano /etc/init.d/tomcat
If you installed OpenJDK instead of Sun Microsystem's Java, the export JAVA_HOME line should read:
# 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
Now make the script executable:
Finally, create symbolic links in the startup folders:
sudo chmod +x /etc/init.d/tomcat
Tomcat should now be totally functional and should start automatically when the slice reboots.
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
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 TomcatStart 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/' .
SummaryInstalling 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!