Installing Tomcat as a Linux Service

Linux 12 Comments

Apache Tomcat is a Java Servlet container which can be run as a standalone HTTP server. The instructions below will walk you through installing and getting Tomcat working on your Linux box. I’m doing this on a SuSE Linux server but it should be applicable to other Linux flavors like Red Hat.

Note: All the instructions below are executed using the root user account.

First, your Linux server must have Java installed because Tomcat requires it. If you don’t have Java installed already or want the latest, do the following:

  1. Get the latest JDK (currently JDK 6 update 12) from the Sun Java Website.
  2. Download the jdk bin file, give it execute permission, and run it. It will extract an RPM file.
  3. Install the JDK RPM by running this command:
    rpm -ivh jdk-6u10-linux-i586.rpm
  4. The JDK will be installed under the “/usr/java” directory (Ex: “/usr/java/jdk1.6.0_12”).

Now, you can install the latest Tomcat by doing the following:

  1. Get the latest Tomcat (currently 6.0.18) from the Apache Tomcat Website.
  2. Install Tomcat by running the following:
    cd /opt
    tar -xzf apache-tomcat-6.0.18.tar.gz
  3. The above will extract Tomcat under the “/opt” directory (Ex: “/opt/apache-tomcat-6.0.18”)
  4. Tomcat needs these environment variables to be set:
    JAVA_HOME=/usr/java/jdk1.6.0_12
    CATALINA_HOME=/opt/apache-tomcat-6.0.18

You can set the variables above in the root user environment by default or you can set these in the Tomcat startup script. You will want to do the latter if you have several versions of Java installed and wish to use Tomcat with a specific Java version.

To adjust the root user environment, do the following:

  • Under SUSE Linux 10 SP1, to set the default environment variables for root (and Tomcat), edit the “/etc/profile” file, and add the following lines to the end of the file:
    JAVA_HOME=/usr/java/jdk1.6.0_12
    JRE_HOME=/usr/java/jdk1.6.0_12
    CATALINA_HOME=/opt/apache-tomcat-6.0.18
    export JAVA_HOME JRE_HOME CATALINA_HOME
  • Optionally, if you don’t want to touch “/etc/profile”, you can create an “/etc/profile.local” which should also be loaded for the root environment; if you do this, make sure to add a second “source /etc/profile.local” to your script.
  • You only have to set JRE_HOME above if you need to override it because it was explicitly defined elsewhere. Otherwise, if JRE_HOME is not defined, Tomcat will use JAVA_HOME instead.

To adjust the Tomcat startup script to use a specific Java version and/or use a different port than the default 8080, do the following:

  • Adjust Tomcat to use the latest JDK without touching the existing environment variables.
    1. Edit the “/opt/apache-tomcat-6.0.18/bin/catalina.sh” file.
    2. At the top of the file, add these two lines:
      JAVA_HOME=/usr/java/jdk1.6.0_12
      CATALINA_HOME=/opt/apache-tomcat-6.0.18
    3. There is another environment variable JRE_HOME which will default to JAVA_HOME if it is blank. You will want to check that your default environment doesn’t set it explicitly. If it is set explicitly, you can override by adding this line to the top of the “catalina.sh” file:
      JRE_HOME=/usr/java/jdk1.6.0_12
  • If you wish to (maybe you want to run two versions of Tomcat), you can adjust Tomcat to use another port (like 7080) instead of the default 8080 by following these steps:
    1. Edit the “/opt/apache-tomcat-6.0.18/conf/server.xml” file.
    2. Replace all instances of the default port numbers with these new port numbers:
      Old Port New Port
      8005 7005
      8009 7009
      8080 7080
      8443 7443
    3. While you are in this file, you might want to enable Tomcat access logging by uncommenting the “AccessLog” value in server.xml to enable access logging in Tomcat. The access logs will be located in the “/opt/apache-tomcat-6.0.18/logs” directory (Ex: “localhost_access_log.2009-03-06.txt”).

Once you have installed Tomcat, you can start and stop it using these commands:

cd /opt/apache-tomcat-6.0.18/bin
sh startup.sh &
sh shutdown.sh &

Once Tomcat is started, test it out by launching your browser and doing the following:

  1. Browse to http://localhost:8080/ or http://localhost:7080/ if you have changed the default port to 7080.
  2. If you attempt to browse to the server from a remote machine and the server doesn’t respond, then you may need to disable the firewall.
    • Under SUSE Linux, disable the firewall by logging in as root and running “rcSuSEfirewall2 stop”. Further, to prevent the firewall from running on boot, run Yast->Firewall and change “Service Start” to Manually. If you run “chkconfig | grep firewall” afterwards, you should see that SuSEfirewall2_init and SuSEfirewall2_setup services are set to off.

Now that Tomcat is running successfully, you may wish to configure Tomcat to run on boot. One way of doing so is to configure Tomcat to be a service and registered it to be launched as part of the boot.

  1. First, create the shell script file below and name it “tomcat” (or whatever service name you want to use).
    #!/bin/sh
    #
    # /etc/init.d/tomcat
    #
    # This is the init script for starting up the
    #  Jakarta Tomcat server
    #
    # description: Starts and stops the Tomcat daemon.
    #

    tomcat=/opt/apache-tomcat-6.0.18
    startup=$tomcat/bin/startup.sh
    shutdown=$tomcat/bin/shutdown.sh

    start() {
      echo -n $"Starting Tomcat service: "
      sh $startup
      echo $?
    }

    stop() {
      echo -n $"Stopping Tomcat service: "
      sh $shutdown
      echo $?
    }

    restart() {
      stop
      start
    }

    status() {
      ps -aef | grep apache-tomcat | grep -v tomcat6 | grep -v grep
    }

    # Handle the different input options
    case "$1" in
    start)
      start
      ;;
    stop)
      stop
      ;;
    status)
      status
      ;;
    restart)
      restart
      ;;
    *)
      echo $"Usage: $0 {start|stop|restart|status}"
      exit 1
    esac

    exit 0
  2. Note that when you run a process as a service, its environment may be different from the environment you see when you log in.
    • Under SUSE Linux, you may wish to add the line below to the top of the Tomcat script to ensure that the Tomcat process will run with the environmental variables that you have set in “/etc/profile”.
      source /etc/profile
    • To see what environment a process is running under, you can use the following commands:
      ps -aef | grep tomcat
      ... you will get a process id number like 5054 ...
      cat /proc/5054/environ
  3. Copy the file to the “/etc/init.d” directory (example: “/etc/init.d/tomcat”).
  4. Give it execute permission:
    cd /etc/init.d
    chmod u+x tomcat
  5. You can test the script by running this command (which will return the Tomcat process info if it is running; otherwise, it will return blank):
    cd /etc/init.d
    ./tomcat status
  6. Add tomcat as a service by running the following:
    chkconfig --add tomcat
  7. Now that tomcat is a service, you can issue these commands from anywhere:
    service tomcat start
    service tomcat stop
    service tomcat restart
    service tomcat status
  8. Set the tomcat service to start at boot:
    chkconfig tomcat on

That is pretty much it. Your Linux server is set to automatically start Tomcat.

Note: I updated the Tomcat service script to be more bulletproof; specifically, the script will kill Tomcat if it hangs on exit (which sometimes may occur).

12 Comments