Wednesday 25 May 2011

$?이게 무엇인고?

Shell Programming을 위해 웹서핑을 하다보니 예전에 모르고 넘어간 $?가 나와있었다.
아~ 이거구나~~~ 후후후 기쁘다.

다음 예제에서 $?는 명령 수행 후의 결과를 돌려주는 것을 볼 수 있다. 정상적일 경우 0, 뭔가 잘못 되었을 경우는 2를 반환하는 것을 볼 수 있다. ㅋ
# ls -ld /tmp
drwxrwxrwt 5 root root 4096 Aug 19 19:45 /tmp
[root@server01 ~]# echo $?
0 // Good command return of 0.
[root@server01 ~]# ls -l /junk
ls: /junk: No such file or directory
[root@server01 ~]# echo $?
2 // Something went wrong, there was an error, so return 2.


자세한 내용은 초보 시스템 관리자를 위한 Bash 스크립트 작성 참고

근데 나도 초보인거야???? 이구~

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

xinetd를 통하여 80포트를 8080 포트로 리다이렉트하기

일반적인 Unix계열의 시스템에서는 1024포트 이하의 포트를 사용하기 위해서는 반드시 root privileges를 가지고 있어야 한다.

문제는 tomcat과 같은 서비스는 디폴트 포트가 8080를 사용하는데, 이를 80포트로 사용하기위해서는 반드시 서비스를 root 계정으로 실행 시켜야 하는 보안 문제가 발생할 수 있다. 또한 80포트로 변경하려면 server.xml을 수정해야만 한다. 이를 수행하지 않고 수퍼 데몬인 xinetd을 통하여 port redirecting을 하도록 하겠다.

xinetd 설정 파일 생성

먼저 http와 https 포트를 tomcat에 연결하기 위해 각각의 서비스 설정 파일을 만들어야한다.
* http 설정 파일 생성
# vi /etc/xinetd.d/http
service http
{
    disable = no
    flags = REUSE
    socket_type = stream
    wait = no
    user = root
    redirect = 127.0.0.1 8080
    log_on_failure += USERID
}
* http 설정 파일 생성
# vi /etc/xinetd.d/https
service https
{
    disable = no
    flags = REUSE
    socket_type = stream
    wait = no
    user = root
    redirect = 127.0.0.1 8443
    log_on_failure += USERID
}

xinetd 재시작

# service xinetd restart
웹브라우저에서 80포트를 통해 8080로 리다이렉트 됨을 확인한다.