Tomcat installation per tarball (Linux)

Aus Tutorials
Zur Navigation springen Zur Suche springen

Noch in Bearbeitung


Voraussetzungen

Java siehe Java (Ubuntu)

Benutzer anlegen

sudo addgroup --system --no-create-home --disabled-login --group tomcat
sudo adduser --system --no-create-home --disabled-login --ingroup tomcat tomcat

Alternativ mit fixer UID:

sudo addgroup --system --no-create-home --disabled-login --uid 128 --group tomcat
sudo adduser --system --no-create-home --disabled-login --uid 128 --ingroup tomcat tomcat 

Download

Verfügbare Versionen siehe http://www.eu.apache.org/dist/tomcat/

wget http://www.eu.apache.org/dist/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz
tar -xzf apache-tomcat-7.0.69.tar.gz

Installationsverzeichnis anlegen:

sudo mkdir -p /opt/tomcat/tomcat7_0_69/

Entpacktes Tomcat-Verzeichnis verschieben und Besitzer setzen:

sudo mv apache-tomcat-7.0.69 /opt/tomcat/tomcat7_0_69/tomcat7_0_69
sudo chown -R tomcat:tomcat /opt/tomcat/tomcat7_0_69/tomcat7_0_69

Installation

Einen symbolischen Link auf das Verzeichnis mit der Version für Tomcat 7 anlegen (falls mehrere Versionen installiert sind):

cd /opt/tomcat/
sudo ln -s /opt/tomcat/tomcat7_0_69 tomcat7

Umgebungsvariablen setzen

Dazu eine Datei etc/tomcat7.conf erstellen:

sudo nano etc/tomcat7.conf

Diese Datei mit folgenden Inhalt befüllen:

# Where your java installation lives
JAVA_HOME="/opt/Oracle_Java/jdk1.8.0_92"

# Where your tomcat installation lives
CATALINA_BASE="/opt/tomcat/tomcat7_0_69/tomcat7_0_69"
CATALINA_HOME="/opt/tomcat/tomcat7_0_69/tomcat7_0_69"
JASPER_HOME="/opt/tomcat/tomcat7_0_69/tomcat7_0_69"
CATALINA_TMPDIR="/opt/tomcat/tomcat7_0_69/tomcat7_0_69/temp"

# Set the TOMCAT_PID location
CATALINA_PID="$CATALINA_HOME/bin/catalina.pid"

# What user should run tomcat
TOMCAT_USER="tomcat"

# Connector port is 8080 for this tomcat7 instance
CONNECTOR_PORT="8080"

Konfiguration

Tomcat Benutzer anlegen

Dazu in der Datei $CATALINA_HOME/conf/tomcat-users.xml folgende Zeilen zwischen <tomcat-users> und </tomcat-users> einfügen:

<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<role rolename="tomcat"/>
<user username="tomcat" password="tomcat" roles="tomcat,manager-gui,admin-gui"/>

Zusätzlich bei Tomcat 9

Manager für alle Addressen erlauben

sudo nano /opt/tomcat/tomcat9_0_0/conf/Catalina/localhost/manager.xml
<Context privileged="true" antiResourceLocking="false" docBase="${catalina.home}/webapps/manager">
    <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>

Als Service mit SysVinit

Eine Konfigurationsdatei /etc/init.d/tomcat7 anlegen:

sudo nano /etc/init.d/tomcat7

Die Datei /etc/init.d/tomcat7 mit folgenden Inhalt befüllen (die Basis für dieses Skript stammt von https://gist.github.com/miglen/5590986):

#!/bin/bash
#
# description: Apache Tomcat init script

# Get the tomcat config (use this for environment specific settings)
if [ -z "${TOMCAT_CFG}" ]; then
        TOMCAT_CFG="/etc/tomcat7.conf"
fi

if [ -r "$TOMCAT_CFG" ]; then
        . $TOMCAT_CFG
fi

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                sudo -u $TOMCAT_USER $CATALINA_HOME/bin/startup.sh  > /dev/null 2>&1
        else
                echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh  > /dev/null 2>&1
        fi
        status
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]
            then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else
            echo -e "\e[00;31mTomcat is not running\e[00m"
            return 3
          fi
}

terminate() {
        echo -e "\e[00;31mTerminating Tomcat\e[00m"
        kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    sh $CATALINA_HOME/bin/shutdown.sh > /dev/null 2>&1

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}

case $1 in
        start)
          start
        ;;
        stop)
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
                status
                exit $?
        ;;
        kill)
                terminate
        ;;
        *)
                echo -e $TOMCAT_USAGE
        ;;
esac
exit 0
sudo initctl start tomcat

Mehrere Tomcat-Instanzen parallel

sudo nano /etc/tomcat9_1.conf
# Where your java installation lives
JAVA_HOME="/etc/alternatives/jre"

# Where your tomcat installation lives
CATALINA_BASE="/opt/tomcat/tomcat9_1"
CATALINA_HOME="/opt/tomcat/tomcat9_1"
JASPER_HOME="/opt/tomcat/tomcat9_1"
CATALINA_TMPDIR="/opt/tomcat/tomcat9_1/temp"

# Set the TOMCAT_PID location
CATALINA_PID="/var/run/tomcat9_1.pid"

# What user should run tomcat
TOMCAT_USER="tomcat"

# Connector port is 8080 for this tomcat6 instance
CONNECTOR_PORT="8081"
cd /opt/tomcat/tomcat9
sudo mkdir tomcat9_1
sudo cp -r tomcat9/conf/ tomcat9_1/conf/
sudo cp -r tomcat9/webapps/ tomcat9_1/webapps/
sudo mkdir tomcat9_1/logs/
sudo mkdir tomcat9_1/temp/
sudo mkdir tomcat9_1/work/
cd tomcat9_1
sudo ln -s ../tomcat9/bin bin
sudo ln -s ../tomcat9/lib lib
sudo chown -R tomcat:tomcat /opt/tomcat/tomcat9_1/
sudo chmod -R 755 /opt/tomcat/tomcat9_1/
sudo cp /opt/tomcat/tomcat9/bin/catalina.sh /opt/tomcat/tomcat9/bin/catalina9_1.sh

/opt/tomcat/tomcat9/bin/catalina9_1.sh:

# Get the tomcat config (use this for environment specific settings)
if [ -z "${TOMCAT_CFG}" ]; then
        TOMCAT_CFG="/etc/tomcat9_1.conf"
fi

if [ -r "$TOMCAT_CFG" ]; then
        . $TOMCAT_CFG
fi


# if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
#  . "$CATALINA_BASE/bin/setenv.sh"
# elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
#   . "$CATALINA_HOME/bin/setenv.sh"
# fi

conf/server.xml

Port Beschreibung
8005 Shutdown Port
8080 Connector Port
8443 Redirect Port
8009 AJP Connector Port

Load Balancer

Load Balancer mit mod_jk (Linux)

Links

http://perfect-knowhow.de/autostart-von-tomcat-6-beim-booten-eines-ubuntu-systems

https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-14-04

http://www.patrick-gotthard.de/manuelle-tomcat-installation-unter-linux

http://www.laub-home.de/wiki/Mehrere_Tomcat_Instanzen_auf_einem_Server

https://www.tech-island.com/tutorials/mehrere-tomcat-instanzen-auf-demselben-server


Zurück zu Ubuntu