개요

찾아보기 편하기 위해 상용에서까지 사용할 수 있는 톰캣 환경설정 파일 및 구동 스크립트를 정리하여 포스팅 한다. 기준 버전은 톰캣 8이다.

디렉토리 구조

톰캣 바이너리를 관리하기 위한 디렉토리 구조

톰캣 카타리나 로그가 쌓일 디렉토리 구조

기본 server.xml

<?xml version='1.0' encoding='utf-8'?>
<Server port="7010" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
  </GlobalNamingResources>

  <Service name="Catalina">

    <Connector port="8300" protocol="org.apache.coyote.ajp.AjpNio2Protocol"
           URIEncoding="UTF-8" useBodyEncodingForURI="true"
           acceptorThreadCount="2" connectionTimeout="5000" />

    <Engine name="Catalina" defaultHost="www.kwangsiklee.com">
      <Host name="www.kwangsiklee.com" appBase="webapps"
        unpackWARs="false" autoDeploy="false">

     <Context path="" docBase="/home/webapps/웹앱명" reloadable="false">
     </Context>
      </Host>
    </Engine>
  </Service>
</Server>

tom1_catalina.out 심볼릭 링크 추가

$ cd /log/tomcat/
$ ln -s /home/site/www.kwangsiklee.com/tom1/tomcat/logs/catalina.out tom1_catalina.out

logging.properties

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = /log/tomcat
1catalina.org.apache.juli.FileHandler.prefix = cms3.

#2localhost.org.apache.juli.FileHandler.level = FINE
#2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
#2localhost.org.apache.juli.FileHandler.prefix = localhost.

#3manager.org.apache.juli.FileHandler.level = FINE
#3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
#3manager.org.apache.juli.FileHandler.prefix = manager.

#4host-manager.org.apache.juli.FileHandler.level = FINE
#4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
#4host-manager.org.apache.juli.FileHandler.prefix = host-manager.

#java.util.logging.ConsoleHandler.level = FINE
#java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler

구동 스크립트

#!/bin/sh
#set -x
cd /home/site/www.kwangsiklee.com
./tomcat stop tom1
sleep 2
./tomcat start tom1

sleep 120 &
timerPid=$!

tail -n0 -F --pid=$timerPid /log/tomcat/tom1_catalina.out | while read line
do
    if echo $line | grep -q ': Server startup in '; then
    # stop the timer..
    kill $timerPid > /dev/null
    else
    echo $line
    fi
done &

wait %sleep
exit 0

구동에 쓰이는 tomcat 스크립트

#!/bin/sh
#

. /home/site/www.kwangsiklee.com/env.sh

USER_NAME=lks21c
# Check User Name
IAM=`id | awk '{print substr($1, 1, index($1,")")-1 )}' | awk '{print substr($1, index($1,"(")+1 )}'`

# Check startup user validation
if [ $USER_NAME != $IAM ]
then
 echo "Startup Error : User validation is failed. This instance has been started as \"$IAM\", actual script owner is \"$USER_NAME\""
 exit
fi

SITES=`ls -d *\/|cut -d/ -f1`

export CATALINA_PID=.catalina.pid

startup() {
       CATALINA_BASE="${SITE_HOME}/$1/tomcat"

        if [ -f $CATALINA_BASE/bin/startup.sh ];
            echo "catalina $CATALINA_BASE"
          then

            if [   "tom1" = "$1"    ];
                 then
                      export JAVA_OPTS="-D${1} $CATALINA_CONFIG -Dinstname=www.kwangsiklee.com"
             fi

            cd ${CATALINA_BASE}/bin
            ./startup.sh
        fi
}

shutdown() {

       CATALINA_BASE="${SITE_HOME}/$1/tomcat"
        if [ -f $CATALINA_BASE/bin/shutdown.sh ];
          then
            echo "Shuttion server : $1"

            cd ${CATALINA_BASE}/bin
            ./shutdown.sh 3 -force
        fi

}
case "$1" in
  start)
    if [ "all" != "$2" ];
      then

       startup $2

       echo "Starting single server $2"
    else

    for x in ${SITES}
    do

       startup $x
       echo "Starting dual server: ${x}"
    done
    fi
    ;;

  stop)
    if [ "all" != "$2" ];
      then
       shutdown $2
       echo "Shutting down server $2"
    else

    for x in ${SITES}
    do
       shutdown $x

       echo "Shutting down server $2"
    done
    fi
    ;;

  *)
    echo $"Usage: $0 {start|stop,all|tom1 or tom2 or tom3 or tom4} "
    exit 1
    ;;
esac

env.sh

#!/bin/bash

export JAVA_HOME=/usr/local/java1.8
export CLASS_HOME=
export SITE_HOME=/home/site/www.kwangsiklee.com
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.

export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}

export CATALINA_CONFIG="-server -Xms256m -Xmx1024m -Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_EQUALS_IN_VALUE=true -Djava.security.egd=file:/dev/./urandom"