sabato 15 gennaio 2011

Install Tomcat 6 on Ubuntu


If you are running Ubuntu and want to use the Tomcat servlet container, you should not use the version from the repositories as it just doesn’t work correctly. Instead you’ll need to use the manual installation process that I’m outlining here.

Before you install Tomcat you’ll want to make sure that you’ve installed Java. I would assume if you are trying to install Tomcat you’ve already installed java, but if you aren’t sure you can check with the dpkg command like so:


dpkg –get-selections | grep sun-java

This should give you this output if you already installed java:


sun-java6-bin install
sun-java6-jdk install
sun-java6-jre install

If that command has no results, you’ll want to install the latest version with this command:


sudo apt-get install sun-java6-jdk

Installation

Read my post for last JDK http://diegobenna.blogspot.com/2011/01/install-jdk-6-update-21-in-ubuntu-1010.html or follow:


sun-java6-bin install
sun-java6-jdk install
sun-java6-jre install

Now we’ll download and extract Tomcat from the apache site. You should check to make sure there’s not another version and adjust accordingly.


wget http://apache.hoxt.com/tomcat/tomcat-6/v6.0.14/bin/apache-tomcat-6.0.14.tar.gz
tar xvzf apache-tomcat-6.0.14.tar.gz

The best thing to do is move the tomcat folder to a permanent location. I chose /usr/local/tomcat, but you could move it somewhere else if you wanted to.


sudo mv apache-tomcat-6.0.14 /usr/local/tomcat

Tomcat requires setting the JAVA_HOME variable. The best way to do this is to set it in your .bashrc file. You could also edit your startup.sh file if you so chose.

The better method is editing your .bashrc file and adding the bolded line there. You’ll have to logout of the shell for the change to take effect.


vi ~/.bashrc

Add the following line:


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

At this point you can start tomcat by just executing the startup.sh script in the tomcat/bin folder.

Automatic Starting

To make tomcat automatically start when we boot up the computer, you can add a script to make it auto-start and shutdown.


sudo vi /etc/init.d/tomcat

Now paste in 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

You’ll need to make the script executable by running the chmod command:


sudo chmod 755 /etc/init.d/tomcat

The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.


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 fully installed and operational. Enjoy!


sudo /etc/init.d/tomcat restart

5 commenti:

  1. I find it easier to install Tomcat 6 from the repository. And it works with OpenJDK too. At least I have not found any problems, yet ;-).

    For a developer, you do not want to install a full server, you just want to create a Tomcat instance for each of your projects. First you need to install package tomcat6-user and not tomcat6:

    aptitude install tomcat6-user tomcat6-admin tomcat6-doc tomcat6-examples

    If you install the package tomcat6, you will have a server that by default always starts when you boot the machine.
    So you only want to install packages tomcat6 authbind maybe libapache2-mod-jk if you want to use Apache2 as a frontend for static contents.

    But to create developer instances of Tomcat 6, you just execute this command for each of them.

    export MYTCAT=~/my-test-server

    tomcat6-instance-create -p 8090 -c 8095 $MYTCAT

    This will creates an instance of Tomcat 6 in directory $MYTCAT ($HOME/my-test-server). When you start the server, it will listen to ports 8090 and 8095 instead of ports 8080 and 8005.

    This is how you start your Tomcat 6 instance:

    $MYTCAT/bin/startup.sh

    Start your web browser and connect to localhost:8090 to see your new installation.

    If you want to have the administration servlet running on your site, just create this links:

    ln -s /usr/share/tomcat6-admin/manager $MTCAT/webapps
    ln -s /usr/share/tomcat6-admin/host-manager $MYTCAT/webapps

    Then you can use the ordinary connections:
    http://localhost:8090/manager/list
    http://localhost:8090/host-manager/list

    Don't forget to create a user and add that user to the group manager in $MYTCAT/conf/tomcat-users.xml

    To get the examples, make this link.

    ln -s /usr/share/tomcat6-examples/examples $MYTCAT/webapps

    Then connect your web browser to
    http://localhost:8090/examples/

    And to be able to use the docs from Tomcat, creat this link:

    ln -s /usr/share/tomcat6-docs/docs $MYTCAT/webapps

    and try the URL http://localhost:8090/docs

    The bad thing with this is that Eclipce doesn't have a propper Tomcat implementation, and must have CATALINA_HOME and CATALINA_BASE in same directory.
    Good thing is that NetBeans got a propper installation.

    This should prob. be reported as a bug to the Eclipse package and to Eclipse.

    But the Tomcat 6 instances are easy to use from NetBeans, just set CATALINE_HOME to /usr/share/tomcat6 and CATALINA_BASE to the value of $MYTCAT

    Keep on hacking JSP and Tomcat on Debian distributions (like Ubuntu). :-)

    RispondiElimina
  2. Yes, and when you select what JDK and JRE you want to run on your machine, you should run update-java-alternatives.

    Please have a look at the man pages, man update-java-alternatives for information.

    RispondiElimina
  3. Dear ajxn, but what are the advantages of using tomcat6-user? I have a server that I use for some web applications. If a friend needs to use my server for other applications, I create a copy of tomcat6 and i change port. Your option what benefit give me?

    RispondiElimina
  4. I have download the tomcat 6 at my /home/arif/Farhan/apache-tomcat-6.0.35 folder.my java home is /usr/local/java/jdk1.7.0_07.when i am trying to startup tomcat.it shows the result below.Tomcat does not start.---
    sudo /etc/init.d/tomcat start
    Using CATALINA_BASE: /home/arif/Farhan/apache-tomcat-6.0.35
    Using CATALINA_HOME: /home/arif/Farhan/apache-tomcat-6.0.35
    Using CATALINA_TMPDIR: /home/arif/Farhan/apache-tomcat-6.0.35/temp
    Using JRE_HOME: /usr/local/java/jdk1.7.0_07/jre
    Using CLASSPATH: /home/arif/Farhan/apache-tomcat-6.0.35/bin/bootstrap.jar

    i do not know what is the problem.I am using ubuntu 12.04.Please help me.

    RispondiElimina
  5. Installing tomcat on ubuntu is need assistance and I found great help from your post.its help to accomplish this task in my Apache tomcat training in hyderabad. thank you.

    RispondiElimina