Pages

Sunday, 3 June 2012

Installing Tomcat server on ubuntu

This article assumes that java is not installed yet on ubuntu. If it is already installed in your case then please move on to the second part where it describes how to install tomcat server on ubuntu.

Java Installation 
First go to System -> Administration -> Synaptic Package Manager.  Write jdk in the quick search text field to install java on ubuntu machine as depicted under.



Mark open jdk package for the installation and click on Apply button (that is enabled once any package is marked for the installation). After clicking on Apply button it will take few minutes to complete java installation on ubuntu.

Once java is installed you might need to add it to PATH so that it can be accessed from any location within the system. Also java environment variable (i.e JAVA_HOME) is required for the tomcat installation.

For adding java to PATH go to Application -> Accessories -> Terminal and run following command that will open a file containing PATH variable in editable mode.

sudo gedit /etc/environment

Append the JAVA_HOME/bin location at the end of  PATH string like

PATH="... :/usr/lib/jvm/java-6-openjdk/bin"

Next thing is to add JAVA_HOME environment variable. The best place to add this environment variable is the .bashrc file which is a hidden file in the user home directory.

Go to user home directory press "Ctrl + H". On pressing these keys the hidden files will become visible. Open the .bashrc hidden file (that is in the user home directory) in an editor and add following line in it.

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk

Note that at this point you need to log out of the shell for the changes to take effect. After re-login open terminal and issue following command.

echo $JAVA_HOME

it should print '/usr/lib/jvm/java-6-openjdk' which means that JAVA_HOME environment variable is successfully set. After installing java and setting its environment variable we are going to install tomcat on ubuntu.

Tomcat Installation
 Go to Synaptic Package Manager again and write tomcat in the quick search text field to install tomcat on ubuntu machine as depicted in below snapshot.



Mark tomcat for installation and click on Apply button. The Synaptic Package Manager will download and install Tomcat web server within one or two minutes. Once tomcat is installed we need to add CATALINA_HOME environment variable in .bashrc file. Open .bashrc file again and add following line in it.
  
export CATALINA_HOME=/usr/local/tomcat

As a new environment variable is added in .bashrc file therefore a re-login is required for the changes to take effect.

Now you are able to start tomcat server by running following commands in the terminal.

cd /$CATALINA_HOME/bin
sh catalina.sh start

In order to auto start the tomcat server with user log-in add file first by running following command.

sudo gedit /etc/init.d/tomcat

then paste following script in it.

# 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

Next make the above script runnable with following command.

sudo chmod 755 /etc/init.d/tomcat

Finally link above script to the startup folders with a symbolic 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  

On restarting the ubuntu tomcat server should be up and fully operational.