Installing Tomcat as a Linux Service

3:39 pm Linux

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.

8 Responses
  1. rugby ashadi :

    Date: May 1, 2009 @ 1:55 am

    great article. this helped me saves time since i was a newbe. thank’s a lot.

  2. ravichandran :

    Date: June 9, 2009 @ 10:42 am

    thanks. nice document

  3. James :

    Date: March 10, 2010 @ 7:00 am

    I used step 6 forward I am using an instace I created on SUSEstudio and the tomcat6 script already exist but I could not get it work by adding S04 and K01 link using ln command. thanks for the info. now it startup automatically A+++++++++

  4. Hari Yadav :

    Date: June 23, 2010 @ 4:27 pm

    I like the article very much, specially how to add the tomcat scripts to OS service. But this is the work around only, so can anybody tell me that how to install Tomcat as service without workaround, means i don’t want to add or edit any script. Just runn the installer and service should be setup.
    Anyway article is nice so thanks again. :)

  5. Megamind :

    Date: November 12, 2010 @ 12:28 pm

    Really Gr8 ! Thanks For sharing..

  6. Robson Vaz :

    Date: March 22, 2012 @ 11:43 am

    Excelente tutorial, muito bom mesmo.

    Apenas uma dúvida.

    Como iniciar o tomcat como serviço, não sendo usuário “root”?

    Obrigado.

    Robson Vaz

  7. Chanh :

    Date: March 22, 2012 @ 12:20 pm

    Obrigado pelo elogio. Para executar o script como um usuário não-root, na parte superior do script, coloque esta linha “su -l username”. Isso vai mudar o contexto da raiz para esse usuário.

  8. David :

    Date: March 21, 2013 @ 4:20 am

    Perfect! Thank you from Barcelona!

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.