Sunday, December 28, 2008

Install Apache Tomcat on Ubuntu

Tomcat, an Open Source JSP and Servlet Container developed by the guys from the Apache software foundation. I have abandoned Tomcat for years because I was not working on projects which required a java container of it self. Working with Oracle Application server is giving you enough, however, now I am starting to code some projects which requiers a java container of itself so it was a small step to get back to my old love I have been working with for several years.

Because I will be using Tomcat at this moment still mainly for testing and coding while commuting from home to work and back I decided to install it on my virtual Ubuntu installation on my MacBook. I found out that installing Tomcat on Ubuntu is quite easy. I found a quick howto at howtogeek.com from which I followed most of the steps. Here is my fork of this howto.

1) Check your current java version.
As Tomcat is running Java code and is depending on Java you will have to have Java running on your system. Check your currently installed Java versions with the following command:

dpkg -l | grep sun

This should give you at least the following packages installed; sun-java6-bin, sun-java6-jdk and sun-java6-jre. If not you have to install Java on Ubuntu with the following command: sudo apt-get install sun-java6-jdk

Now when you check it again you should see the packages installed.

2) Install Tomcat
Installing tomcat can be done by downloading it from the tomcat website, currently this can be done from the following location: http://tomcat.apache.org/download-60.cgi
When you have downloaded Tomcat you have to unpack it and move it to /usr/local so that tomcat will be in /usr/local/tomcat/

3) Setting JAVA_HOME
One of the requirements of Tomcat is that the variable TOMCAT_HOME is set on your system and that it points to the java version you just installed. You can check this by executing env | grep JAVA_HOME which should (or should not) give you JAVA_HOME and the value of this variable. If this is not the case or the value is not pointing to the correct java version you have to change this. Lets say JAVA_HOME is not set, you have to edit .bashrc in your home directory so enter vi ~/.bashrc at the end of the file add the following: export JAVA_HOME=/usr/lib/jvm/java-6-sun

4)Startup script Tomcat
Now we would like to have tomcat to startup when you start your machine and shutdown correctly when you shutdown the machine. Create a file in /etc/init.d named tomcat and enter the following information:

# 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 we have to make this script executable sudo chmod 755 /etc/init.d/tomcat . Now the script is executable we can do a tomcat start to start it, tomcat stop to make it stop and a tomcat restart to stop and start it again. However we would like to have it start and stop automatically. For this we create a start and stop link by executing the following two commands:

sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat


Now when you open http://localhost:8080 you will see the start page of tomcat in your browser. All set and ready to go.


Post a Comment