Monday 16 May 2011

CentOS에 Apache Tomcat 6 설치 하기


Installing Apache Tomcat 6 on RHEL or CentOS

현재 CentOS의 최신 버전인 5.6도 아직까지 Tomcat 5까지만 패키지로 제공하고 있다. 하지만 문제는 이 패키지가 Oracle에서 제공하는 JDK와는 잘 맞지 않는다는데 있다.
여기서는 JDK 6 와 Tomcat 6 버전을 CentOS 5.6기반에 설치하는 것을 기술하도록 한다.
절차
  1. Oracle JDK 6.0 설치(RPM버전)
  2. Apache Tomcat6 설치
  3. Apache Portable Runtime(APR) 기반 Native Library 생성
  4. Native Daemon을 통한 Tomcat6 서비스 등록

Oracle JDK 6.0 설치

Apache Tomcat 6 설치

  1. 압축 해제 및 소유권 할당
    cd /tmp
    tar -zxvf apache-tomcat-6.0.32.tar.gz -C /usr/share/
    ln -s /usr/share/apache-tomcat-6.0.32 /usr/share/tomcat6
    useradd -u 501 -M -d /usr/share/tomcat6 tomcat
    chown -R tomcat: /usr/share/apache-tomcat-6.0.32 
    chown -h tomcat: /usr/share/tomcat6
    여기서 다운로드 받은 apache-tomcat 파일은 /tmp 디렉토리에 있으며, 버전은 6.0.32를 사용하는 것으로 하겠다.

Apache Portable Runtime(APR) 기반 Native Library 생성(Optional)

  1. Native Library 빌드
    1. 압축 해제
      cd /usr/share/tomcat6/bin
      tar -zxvf tomcat-native.tar.gz
      cd tomcat-native-1.1.20-src/jni/native
    2. Dependency Package 설치
      yum install openssl-devel apr-devel -y
    3. Native Library 빌드
      ./configure --with-apr=/usr/bin --with-ssl=/usr/include/openssl
      make && make install
  2. Native Library 라이브러리 시스템 등록
    echo "/usr/local/apr/lib" > /etc/ld.so.conf.d/tomcat-native.conf
    ldconfig
    ldconfig -v |grep apr
    /usr/local/apr/lib:
            libaprutil-1.so.0 -> libaprutil-1.so.0.2.7
            libgstdataprotocol-0.10.so.0 -> libgstdataprotocol-0.10.so.0.17.0
            libapr-1.so.0 -> libapr-1.so.0.2.7
            libaprutil-1.so.0 -> libaprutil-1.so.0.2.7
            libgstdataprotocol-0.10.so.0 -> libgstdataprotocol-0.10.so.0.17.0
            libapr-1.so.0 -> libapr-1.so.0.2.7
  3. Native Library 라이브러리 Tomcat에게 알리기
    echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib\"" >> /usr/share/tomcat6/bin/setenv.sh

Tomcat 6 Service 시작 스크립트 생성

#!/bin/bash
#
# Init script file for Tomcat Server
#
# chkconfig: 2345 55 25
# description: Apache Tomcat Server
#

# Source function library.
. /etc/init.d/functions

RUN_AS_USER=tomcat
CATALINA_HOME=/usr/share/tomcat6

start() {
        echo "Starting Tomcat: "
        if [ "x$USER" != "x$RUN_AS_USER" ]; then
          su - $RUN_AS_USER -c "$CATALINA_HOME/bin/startup.sh"
        else
          $CATALINA_HOME/bin/startup.sh
        fi
        echo "done."
}
stop() {
        echo "Shutting down Tomcat: "
        if [ "x$USER" != "x$RUN_AS_USER" ]; then
          su - $RUN_AS_USER -c "$CATALINA_HOME/bin/shutdown.sh"
        else
          $CATALINA_HOME/bin/shutdown.sh
        fi
        echo "done."
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 10
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
esac

exit 0

Native Daemon용 시작 스크립트 생성(Optional)

  1. The Java portion of Commons Daemon(jsvc) 컴파일 (64Bit)
    cd /usr/share/tomcat6/bin
    tar -zxvf commons-daemon-native.tar.gz
    cd commons-daemon-1.0.5-native-src/unix
    export CFLAGS=-m64
    export LDFLAGS=-m64
    ./configure --with-java=/usr/java/default
    make
    cd ../.. (tomcat의 bin디렉토리)
    cp commons-daemon-1.0.5-native-src/unix/jsvc ./
    chown tomcat: /usr/share/tomcat6/bin/jsvc
    rm -rf commons-daemon-1.0.5-native-src
    requirements
    gcc
  2. Tomcat 6 Service 시작 스크립트 생성
    vi /etc/init.d/tomcat6
    #!/bin/sh
    #
    # Startup script for Tomcat 6.0.13, the Apache Servlet Engine
    #
    # chkconfig: 234 20 80
    # description: Tomcat 6.0.13 is the Apache Servlet Engine
    # processname: tomcat
    # pidfile: /var/run/tomcat6.pid
    #
    ##############################################################################
    
    #
    # 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.
    ##############################################################################
    #
    # Small shell script to show how to start/stop Tomcat using jsvc
    # If you want to have Tomcat running on port 80 please modify the server.xml
    # file:
    #
    #    <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
    #    <Connector className="org.apache.catalina.connector.http.HttpConnector"
    #               port="80" minProcessors="5" maxProcessors="75"
    #               enableLookups="true" redirectPort="8443"
    #               acceptCount="10" debug="0" connectionTimeout="60000"/>
    #
    # Adapt the following lines to your configuration
    JAVA_HOME=/usr/java/default
    CATALINA_HOME=/usr/share/tomcat6
    DAEMON_HOME=/usr/share/tomcat6/bin
    TOMCAT_USER=tomcat
    
    # for multi instances adapt those lines.
    TMP_DIR=/var/tmp
    PID_FILE=/var/run/jsvc.pid
    CATALINA_BASE=/usr/share/tomcat6
    
    #CATALINA_OPTS="-Djava.library.path=/usr/share/tomcat6/bin:"
    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
    
    CLASSPATH=\
    $JAVA_HOME/lib/tools.jar:\
    $CATALINA_HOME/bin/commons-daemon.jar:\
    $CATALINA_HOME/bin/bootstrap.jar
    
    case "$1" in
      start)
        #
        # Start Tomcat
        #
        $DAEMON_HOME/jsvc \
        -user $TOMCAT_USER \
        -home $JAVA_HOME \
        -Dcatalina.home=$CATALINA_HOME \
        -Dcatalina.base=$CATALINA_BASE \
        -Djava.io.tmpdir=$TMP_DIR \
        -jvm server \
        -procname Tomcat6 \
        -wait 10 \
        -pidfile $PID_FILE \
        -outfile $CATALINA_HOME/logs/catalina.out \
        -errfile '&1' \
        $CATALINA_OPTS \
        -cp $CLASSPATH \
        org.apache.catalina.startup.Bootstrap
        #
        # To get a verbose JVM
        #-verbose \
        # To get a debug of jsvc.
        #-debug \
        exit $?
        ;;
    
      stop)
        #
        # Stop Tomcat
        #
        $DAEMON_HOME/jsvc \
        -stop \
        -pidfile $PID_FILE \
        org.apache.catalina.startup.Bootstrap
        exit $?
        ;;
    
      restart)
        #
        # Restart Tomcat
        #
        $0 stop
        sleep 5
        $0 start
        ;;
    
      *)
        echo "Usage tomcat.sh start/stop"
        exit 1;;
    esac
    과부하를 발생시킬수 있는 웹 컨텍스를 실행하고자 하는 경우 이 방법을 가급적 사용하지 말기 바람

서비스 등록 및 시작

  • 서비스 등록
    chmod u+x /etc/init.d/tomcat6
    chkconfig --add tomcat6
    service tomcat6 start
  • 서비스 구동 확인
    pgrep -u tomcat -l
    11900 jsvc

No comments:

Post a Comment