22 September 2010

Manually Installing Tomcat 7 on Ubuntu

Installing Tomcat 7 on Ubuntu 9.10
I prefer the manual process of installing and upgrading software on a live server as it is less likely to break things and the reversal process is easier as well. So that's the way I choose to go;

Info
To find out which version of Ubuntu you have run the following command:
more /etc/lsb-release

On my machine the output is:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"

If you want to find the kernel details run (uname -a).
To find the linux distro you can also run (cat /etc/issue).

Install Java
To check if Java is installed run:

dpkg --get-selections | grep sun-java

which should output:
sun-java6-bin                                   install
sun-java6-jdk                                   install
sun-java6-jre                                   install

running the command (java -version) should give you the following output assuming the "java" executable is on your $PATH variable.


Output:
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Server VM (build 16.3-b01, mixed mode)

If you don't have Java installed the easiest way is run this command (sudo apt-get install sun-java6-jdk)

Tomcat 7 Installation
Download Tomcat
It's recommended to use your web browser to download Tomcat but if you know exactly which version you want and you know the url then wget would suffice.

wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.2-beta/bin/apache-tomcat-7.0.2.tar.gz

Verify download
Calculate the md5 checksum using the command (md5sum apache-tomcat-7.0.2.tar.gz) it should output:

43b5ba6aec55dd9a30957e035d0aac5f apache-tomcat-7.0.2.tar.gz

The above is just to make sure your download wasn't corrupted.

Extract Tomcat 7.0.2 Beta
To extract the content of the archive run the following command (tar xvzf apache-tomcat-7.0.2.tar.gz)

Move the tomcat folder to a location which it will sit and serve webapps. I moved it to /usr/local/tomcat, but am not sure what the best place is, maybe someone could let me know?

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

Set JAVA_HOME variable

To check if the $JAVA_HOME environment variable has been set or not run the following command (echo $JAVA_HOME) which should output:

/usr/lib/jvm/java-6-sun

Tomcat requires setting the JAVA_HOME variable. You can set it in .bashrc or startup.sh. Again not sure what the best place is.

To place it in your .bashrc file.

vi ~/.bashrc

Add the following line:

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

Logout of the shell for the change to take effect.

Startup Tomcat 7.0.2 Beta
Start tomcat by executing "startup.sh" script in the tomcat/bin folder.

Automatic Starting at boot
For a live server you need to make tomcat automatically start at boot up just in case you need to restart the server; Add a start/stop script to init.d script startup directory.

sudo vi /etc/init.d/tomcat

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

Make the script executable:

sudo chmod 755 /etc/init.d/tomcat

Link the start/stop script to the startup folders with a symbolic link.

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


You might also want to put some symbolic links in rc0 and rc6 directories.


Or alternatively let ubuntu create the symbolic links automatically by running this command:



sudo update-rc.d tomcat defaults

which outputs:
update-rc.d: warning: /etc/init.d/tomcat missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/tomcat ...
   /etc/rc0.d/K20tomcat -> ../init.d/tomcat
   /etc/rc1.d/K20tomcat -> ../init.d/tomcat
   /etc/rc6.d/K20tomcat -> ../init.d/tomcat
   /etc/rc2.d/S20tomcat -> ../init.d/tomcat
   /etc/rc3.d/S20tomcat -> ../init.d/tomcat
   /etc/rc4.d/S20tomcat -> ../init.d/tomcat
   /etc/rc5.d/S20tomcat -> ../init.d/tomcat






The difference is that it makes sure tomcat start at each run level however in practical terms if runlevel 2 is reached tomcat will start so run level 3,4 and 5 will be ignored as tomcat would already be running from when run level 2 was reached. The nice thing about this automatic install of symbolic links is that it is easy to add/remove them. Also they add the "stop" script at run level 0 and 6 which should be done!


5 comments:

  1. make sure, the unprivileged user “tomcat” (or whatever you like) exists and start it using
    sudo -u tomcat sh /usr/local/tomcat/bin/startup.sh

    You might want to chown temporary directories prior to that, especially if you already ran tomcat as root:
    cd /usr/local/tomcat/
    chown -R tomcat webapps temp logs work conf
    Actually, I don’t particularly like opening the conf directory for tomcat, but sometimes tomcat prefers to write to its configuration… You don’t need webapps to belong to tomcat if you dont autodeploy, but you definitely need logs and work.


    I start up tomcat by using djb daemontools, here comes the commands and scripts:
    useradd tomcat -g nogroup -d /java/tomcat6/ -s /bin/false -p’*’ -r
    useradd tomcatlog -g nogroup -d /tmp -s /bin/false -p’*’ -r
    cd /java/tomcat
    chown -R tomcat webapps temp logs work conf
    mkdir -p /var/service/
    cd /var/service/
    mkdir -m 1755 tomcat
    cd tomcat
    vim run
    ———————————————
    #!/bin/sh
    exec 2>&1
    export JAVA_HOME=/usr/lib/jvm/java-6-sun
    export JAVA_OPTS=”-Xmx1024M -Xms256M -server”
    export TOMCAT_HOME=/java/tomcat6
    echo “*** Starting tomcat…”
    exec setuidgid tomcat ${TOMCAT_HOME}/bin/catalina.sh run
    ———————————————–
    chmod 755 run
    mkdir -m 755 log
    chown tomcatlog:nogroup log
    cd log
    wget -O run http://qmail.jms1.net/scripts/service-any-log-run
    vim run
    ——————————————–
    VQ=”/var/qmail”
    exec env – PATH=”$VQ/bin:/usr/local/bin:/usr/bin:/bin” \
    setuidgid tomcatlog multilog t n1024 s1048576 ./main \
    ‘-*’ ‘+*ver: status:*’ =lstatus
    ————————————————
    chmod 755 run
    /etc/init.d/tomcat stop
    ln -s /var/service/tomcat /service/tomcat
    sleep 5
    svstat /service/tomcat/ /service/tomcat/log/

    ReplyDelete
  2. It took me forever to find the answer to fix autostart on ubuntu 9.04.

    Here it is:

    1. First remove old symlinks with sudo update-rc.d -f tomcat remove

    2. Execute the follwoing: sudo update-rc.d tomcat start 99 2 3 4 5 S . stop 0 1 6 .

    Credit goes to dumb.coder at http://ubuntuforums.org/showthread.php?t=1188880&highlight=tomcat+autostart

    ReplyDelete
  3. Also, from the ubuntuforums: “it took me days, but I finally realized that tomcat will only start during boot if the startup script is in /etc/rcS.d .. When the start-up script is in /etc/rc2.d – The script doesnt give any errors, and the tomcat logs do not indicate any problems .. its like the process just poofs.”

    http://ubuntuforums.org/showpost.php?p=7583672&postcount=3

    ReplyDelete
  4. There’s a better workaround so you can keep Ubuntu’s Tomcat6 (taken from http://ubuntuforums.org/showthread.php?p=8541057, by DisDis)

    In a terminal:

    1. sudo apt-get install tomcat6
    2. cd /usr/share/tomcat6
    3. sudo ln -s /var/lib/tomcat6/conf conf
    4. sudo ln -s /etc/tomcat6/policy.d/03catalina.policy conf/catalina.policy
    5. sudo ln -s /var/log/tomcat6 log
    6. sudo chmod -R 777 /usr/share/tomcat6/conf

    sudo apt-get install tomcat6 cd /usr/share/tomcat6 sudo ln -s /var/lib/tomcat6/conf conf sudo ln -s /etc/tomcat6/policy.d/03catalina.policy conf/catalina.policy sudo ln -s /var/log/tomcat6 log sudo chmod -R 777 /usr/share/tomcat6/conf

    There. Now just go to Window/Preferences/Sever/Runtime Environments, add the Apache Tomcat6 Server and use /usr/share/tomcat6 as the installation directory!

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete