lunedì 10 gennaio 2011

Install Tomcat 7 on Ubuntu 12.10




Apache has officially launched version 7.0 of the servlet container for Java applications, Tomcat. Major changes in this version, you can highlight support for Servlet 3.0 and JavaServer Pages 2.2.


Install JDK
See this post: Install JDK 6 update 23 in Ubuntu 10.10

Installation

The first thing to do is download the package "apache-tomcat-7.0.6.tar.gz" from the NEXT link
http://tomcat.apache.org/download-70.cgi [tar.gz]
Now unpack it with the following command:
tar xvzf apache-tomcat-7.0.8.tar.gz

Then we let in a more appropriate directory, in our case in / usr/share/tomcat7, but can be in any directory. We do this with the command:
sudo mv apache-tomcat-7.0.8/ /usr/share/tomcat7

Now we define the environment variables JAVA_HOME and JRE_HOME. This file is in the "environment" in / etc. Command to edit the file:
sudo gedit /etc/environment

Here we record the routes where we have installed Java in my case this is as follows:

JAVA_HOME="/usr/local/jdk1.6.0_23"
JRE_HOME="/usr/local/jdk1.6.0_23/jre"
PATH="...(other path):$JAVA_HOME:$JRE_HOME"

IMPORTANT: Verify the routes where they have installed Java.

I have had some problems in defining these environment variables, as sometimes tomcat does not recognize, but a surefire way of recognizing that tomcat is to define the file paths inside "catalina.sh"located in tomcat7/bin. To modify this file use the command:
sudo gedit /usr/share/tomcat7/bin/catalina.sh

Now just insert the JAVA_HOME and JRE_HOME after the first line, so the file is as follows:
#!/bin/sh
JAVA_HOME="/usr/local/jdk1.6.0_23"
JRE_HOME="/usr/local/jdk1.6.0_23/jre"
# Licensed to the Apache Software Foundation (ASF)...
#...
#...
....

Now let's configure Tomcat users, this is done in the file "tomcat-users.xml"directory tomcat7/conf. Command to edit the file:
sudo gedit /usr/share/tomcat7/conf/tomcat-users.xml

Unlike previous versions where the administrator should own role "manager" now it should be "manager-gui"to operate on the web administration tomcat7. The file would be as follows:


<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="admin"/>

<user username="usuario" password="contrasena" roles="manager-gui,admin-gui,manager,admin,manager-script,admin-script"/>
</tomcat-users>



Now you should be all ready to try tomcat7.

First we must lift the server with the following command:
sudo /usr/share/tomcat7/bin/startup.sh

With this we get the following output on console:
Using CATALINA_BASE: /usr/share/tomcat7
Using CATALINA_HOME: /usr/share/tomcat7
Using JRE_HOME: /usr/local/jdk1.6.0_20/jre
Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar

Verify that the JRE_HOME is where we define.

Now open your web browser and type the following url:
http://127.0.0.1:8080/

So we get the following page:

If we enter the administration Tomcat Manager we click on the menu or directly at URL:
http://127.0.0.1:8080/manager/html

Here we ask the user data from previous record in mind tomcat-users.xml.

I recommend testing the sample to make sure everything works ok, they are in the section "Miscellaneous" from the side menu or at the URL:
http://127.0.0.1:8080/examples/

Commands

Start server:
sudo /usr/share/tomcat7/bin/startup.sh

Stop server:
sudo /usr/share/tomcat7/bin/shutdown.sh

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 gedit /etc/init.d/tomcat7

Now paste in the following:


# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

case $1 in
start)
sh /usr/share/tomcat7/bin/startup.sh
;;
stop)
sh /usr/share/tomcat7/bin/shutdown.sh
;;
restart)
sh /usr/share/tomcat7/bin/shutdown.sh
sh /usr/share/tomcat7/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/tomcat7

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/tomcat7 /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat7 /etc/rc2.d/S99tomcat

Tomcat should now be fully installed and operational. Enjoy!

sudo /etc/init.d/tomcat7 restart



70 commenti:

  1. Excelente post Diego!!
    Muy útil!

    RispondiElimina
  2. Thanks.
    Is there an apt-get equivalence?

    RispondiElimina
  3. can you explain difference between /etc/environment here and in install jdk 6 blog? Actually i think on this: JAVA_HOME="/usr/local/jdk1.6.0_23". Is it ok without bin directory.

    Sorry, i'm complete noob, so i need explaination.
    Thanks anyway

    RispondiElimina
  4. Grazie mille, Diego! Sei stato moltíssimo gentile!

    RispondiElimina
  5. FYI -- I just successfully used this and related posts to upgrade my Ubuntu 10.04 java to 1.6.0_24 and to install Tomcat 7.0.12.

    Now I'm coding/debugging/testing my new java servlet. All this on a Dell Latitude D610 with 1 gig of ram. Hums along nicely.

    Thank you for these clear instructions.

    RispondiElimina
  6. Getting Tomcat going is usually a time consuming frustration. I was fearing it to be even worse since this is the first time in years I was going to do it on an Ubuntu box. The Linux people love to shuffle Java directories around.

    Your post was extremely useful and convenient.

    Thank you very kindly for posting it.

    Steve

    RispondiElimina
  7. I needed this badly. The explanation is easy to understand. It worked for me. Thank you.

    RispondiElimina
  8. thank you for your generous post from quebec canada :) STEF

    RispondiElimina
  9. wow... very easy! i love you man...you save a lot of time mine

    RispondiElimina
  10. I don't know why but startup part didn't work for me after 1 hour of struggle I ended up running this command:
    #update-rc.d tomcat7 defaults
    the only different was the name of symbolic links:S20tomcat7 for S99tomcat and K01tomcat7 for K99tomcat.

    RispondiElimina
  11. I tried to install tomcat7 but I had some difficulties. Having read your post and the comments I have found the facts which helped me a lot and I fixed everything.

    RispondiElimina
  12. Questo commento è stato eliminato dall'autore.

    RispondiElimina
  13. "I have had some problems in defining these environment variables, as sometimes tomcat does not recognize..."
    The problem you are facing is that your /etc/sudoers is configured with
    Defaults env_reset
    One has to run your slightly modified command to use system-wide environment variables:
    sudo -E /usr/share/tomcat7/bin/startup.sh

    RispondiElimina
  14. Excellent tutorial, straight to the point. Good work.

    RispondiElimina
  15. Thanks really. I really needed to get this going. Fairly easy to understand.

    RispondiElimina
  16. Great. Now that this path was used /usr/share/, the sysaid installation is not working. And I can not remove tomcat7 anymore, because ubuntu doesnt find the package.

    RispondiElimina
  17. Don't run tomcat as root! That's not very secure. Create a special tomcat user, run tomcat as that, and you'll probably need to change the rights for some folders to that tomcat user.

    RispondiElimina
  18. Great explanation Diego!!!

    One strange thing that it happened to me and I do not know why. I installed Tomcat 7.0.22 in an Ubuntu Server 11.04. When I access Tomcat from my client laptop, an Ubuntu 10.04, via Chrome the tomcat homepage is in English, but some other tomcat pages like "List of applications" are in Spanish. I installed both system, Ubuntu server and Ubuntu desktop in English. The only Spanish thing installed is the Spanish keyboard in the Ubuntu desktop because sometimes I need to write in Spanish, but the laptop is British. So rare, isn't it?

    Any idea?

    RispondiElimina
  19. One more thing, in firefox doesn't happen. Everything is in English, so what's going on in Chorme?

    RispondiElimina
  20. Thanks for the follow through I was able to use this with JDK1.7.01 and tc7.

    RispondiElimina
  21. Possibly dumb question - but why are you writing to rc1.d and rc2.d? Wouldn't you only want tomcat to start in runlevels 3 and 5 - with the network? And is there a reason not to use update-rc.d?

    RispondiElimina
  22. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    RispondiElimina
  23. Thanks a lot, it works perfectly ! :)

    RispondiElimina
  24. This blog is really good. Good posting done. Keep it up..

    RispondiElimina
  25. Viejo, muchas gracias por tu ayuda, mas fácil no puede ser.
    Felicitaciones

    RispondiElimina
  26. Ciao Diego!
    non riesco a capire come mai nonostante il mio file /etc/environment sia:

    JAVA_HOME = "/usr/local/jdk1.6.0_31/bin/"
    JRE_HOME = "/usr/local/jdk1.6.0_31/jre"
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$JAVA_HOME:$JRE_HOME"

    poi quando faccio partire tomcat con

    sudo /usr/share/tomcat7/bin/startup.sh

    ottengo:

    /usr/share/tomcat7/bin/catalina.sh: 2: JAVA_HOME: not found
    /usr/share/tomcat7/bin/catalina.sh: 3: JRE_HOME: not found
    Using CATALINA_BASE: /usr/share/tomcat7
    Using CATALINA_HOME: /usr/share/tomcat7
    Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
    Using JRE_HOME: /usr
    Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar

    ho seguito la tua guida per installare java (nel mio caso 6.31)

    grazie in anticipo

    RispondiElimina
    Risposte
    1. hi hoping this is useful i suggest you to restart linux for allowing settings in /etc/environment taking effect

      hi

      Elimina
  27. Got that up and running with no trouble. Very nicely explained helped me out. Thank you.

    RispondiElimina
  28. Thank a lot !! It works and make it easy the installation process!!
    Best regards!!

    RispondiElimina
  29. Very good post, Diego. Thanks a lot ! :)

    RispondiElimina
  30. Thank you! Tomcat running
    ATT
    João Batista Ladchuk

    RispondiElimina
  31. Very nice post. I just stumbled upon your blog and wished to say that I have truly enjoyed surfing around your blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again soon.

    RispondiElimina
  32. Is doesn't work .......

    RispondiElimina
  33. Thanks for sharing such a nice post
    regards...

    RispondiElimina
  34. nice post, it works to me :)

    RispondiElimina
  35. Sirvió de mucho gracias capo!!!

    RispondiElimina
  36. Why not just apt-get install tomcat7? Much easier.

    RispondiElimina
  37. JAVA_HOME should not have /bin on the end, else very good

    RispondiElimina
  38. awesometown. Had me up and running in 45min. Thank you!

    RispondiElimina
  39. hi,
    I followed all the above steps. But when i tried executing startup.sh, i got the following output on the console.....
    Using CATALINA_BASE: /usr/share/tomcat7
    Using CATALINA_HOME: /usr/share/tomcat7
    Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
    Using JRE_HOME: /usr/local/jdk1.6.0_35/jre
    Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
    touch: cannot touch `/usr/share/tomcat7/logs/catalina.out': No such file or directory
    /usr/share/tomcat7/bin/catalina.sh: 390: /usr/share/tomcat7/bin/catalina.sh: cannot create /usr/share/tomcat7/logs/catalina.out: Directory nonexistent

    Please help..
    thanku

    RispondiElimina
  40. I was able to circumvent the environment variables problem (no more need to add JAVA_HOME and JRE_HOME to catalina.sh) with this start script:

    #!/bin/bash
    # Tomcat auto-start
    #
    # description: Auto-starts tomcat
    # processname: tomcat
    # pidfile: /var/run/tomcat.pid

    for line in $( cat /etc/environment ) ; do

    if [[ "$line" == *"_HOME="* ]]; then
    export $line
    fi
    done

    case $1 in
    start)
    sh /opt/apache-tomcat-7.0.30/bin/startup.sh
    ;;
    stop)
    sh /opt/apache-tomcat-7.0.30/bin/shutdown.sh
    ;;
    restart)
    sh /opt/apache-tomcat-7.0.30/bin/startup.sh
    sh /opt/apache-tomcat-7.0.30/bin/shutdown.sh
    ;;
    esac
    exit 0

    RispondiElimina
  41. I am using ubuntu 12.04 and tomcat 6.I followed all the above steps. But when i tried executing startup.sh, i got the following output on the console.....
    Using CATALINA_BASE: /usr/share/tomcat6
    Using CATALINA_HOME: /usr/share/tomcat6
    Using CATALINA_TMPDIR: /usr/share/tomcat6/temp
    Using JRE_HOME: /usr/local/java/jdk1.7.0_07/jre
    Using CLASSPATH: /usr/share/tomcat6/bin/bootstrap.jar
    ..Please help me.

    RispondiElimina
  42. I have research your web page and I got very useful and knowledgeable information from your web page. It’s really a very amazing material. You have done an outstanding job. Thank you so much for sharing about Tomcat 7 on Ubuntu.

    RispondiElimina
  43. I had a great time reading your article and I found it interesting. This is such a beautiful topic that me and my friends are talking about. Thanks for this blog, we are enlightened

    RispondiElimina
  44. It works with me using ubuntu unity 12.04. Thanks a lot.

    RispondiElimina
  45. Tnx ithel me alot... visit this site also.. you can learn more "HERE" Btw I like your site,,,. Good luck

    RispondiElimina
  46. The blog is absolutely fantastic. Lots of great information and inspiration, both of which we all need. Thanks.

    RispondiElimina
  47. why the command "sudo service tomcat7 start" not working with this setting????

    RispondiElimina
  48. Thank a lot for sharing such all this !! It works and make it easy the installation process!!
    Best Regard's,
    Ben Linus,
    Click On My Link Nitrotek.nl

    RispondiElimina
  49. Muchas Gracias por la guía, me sirvió mucho :D

    RispondiElimina
  50. you are in reality a good webmaster. The web site loading pace is incredible. It seems that you are doing any distinctive trick. Also, The contents are masterwork. you’ve performed a excellent job on this topic!

    RispondiElimina
  51. This is a excellent blog, would you be involved in doing an interview about just how you designed it? If so e-mail me!
    Ryan Gosling Drive Jacket

    RispondiElimina
  52. I just now read something simular to this on That Are i believe they may of stolen the blog?

    RispondiElimina
  53. Thank you, have recently been searching for information about this topic for long time and yours is the best discovery so far.

    RispondiElimina
  54. Over protect "The Clark Kent Superman Leather Smallville Jacket" is available in all size.

    RispondiElimina
  55. hi.. Its 3 a.m. and i was fighting with tomcat6 from last 3 hours. But thanks to you that i solved my problem by just purging tomcat6 and freshly installing tomcat7 in just 10 minutes. Thanks. (yawn)

    RispondiElimina