のぶろぐ 30代のインフラエンジニアはこうです

(;'Д`)ハァハァ ようやく世の中が無菌室で無いことに気づいたよ

tomcat構築手順

 

tar.gzからtomcatをインストールしたので、構築手順を記載しておきます。

CATALINA_BASEとCATALINA_HOMEを分けてマルチインスタンス化、

バージョンアップの簡易化を狙った手順になってます。

 

 

rootユーザにスイッチ

  su - root

tomcat用のインストール資材用意

  http://archive.apache.org/dist/tomcat/tomcat-8/

tomcat資材の展開

  cd /usr/local/src
  tar zxvf apache-tomcat-8.0.39.tar.gz
  mv apache-tomcat-8.0.39 /usr/local/apache-tomcat-8.0.39
  ln -s apache-tomcat-8.0.39 tomcat
  groupadd -g 53 tomcat
  useradd -s /sbin/nologin -u 53 -g 53 tomcat

CATALINA_BASEディレクトリの作成

  mkdir /usr/local/apache-tomcat-base
  cd /usr/local/apache-tomcat-8.0.39
  mv conf logs temp webapps work /usr/local/apache-tomcat-base

CATALINA_BASE用の環境変数ファイル作成

  cd /usr/local/apache-tomcat-base
  mkdir bin
  cd /usr/local/apache-tomcat-base/bin
  vi setenv.sh
  ~~~~~
  export JAVA_HOME=/usr/java/latest
  export CATALINA_HOME=/usr/local/tomcat
  export CATALINA_BASE=/usr/local/apache-tomcat-base
  export CATALINA_OUT=$CATALINA_HOME/logs/catalina.out
  export CATALINA_PID=/var/log/tomcat/tomcat.pid
  export PATH=$PATH:$JAVA_HOME/bin
  export CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar:$CATALINA_BASE/classpath
  export JAVA_OPTS="-verbose:gc -Xloggc:$CATALINA_HOME/logs/gc.log -XX:+PrintGCDetails"
  export CATALINA_OPTS="-server -Xms2048m -Xmx2048m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -XX:NewSize=1024m -XX:MaxNewSize=1024m -XX:MaxTenuringThreshold=15 -XX:+HeapDumpOnOutOfMemoryError -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=17000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.net.preferiPv4Stack=true -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseParNewGC"
  ~~~~~

tomcat起動スクリプト作成
  vi /etc/init.d/tomcat
  ~~~~~
  #!/bin/bash
  #
  # Startup script for the Tomcat Servlet Container
  #
  # chkconfig: - 35 65
  # description: Tomcat is the servlet container that is used in the official \
  #              Reference Implementation for the Java Servlet and JavaServer \
  #              Pages technologies

  INST_NAME=`basename $0`
  TOMCAT_USER=tomcat
  CATALINA_HOME=/usr/local/tomcat

  . /etc/rc.d/init.d/functions
  . /usr/local/apache-tomcat-base/bin/setenv.sh

  prog=tomcat

  start() {
      echo -n $"Starting $prog: "
      daemon --user $TOMCAT_USER $CATALINA_HOME/bin/startup.sh > /dev/null
      RETVAL=$?
      if [ $RETVAL -eq 0 ]; then
          echo_success
      else
          echo_failure
      fi
      echo
      [ $RETVAL = 0 ] && touch /var/lock/subsys/$prog
      return $RETVAL
  }
  stop() {
      echo -n $"Stopping $prog: "
      daemon --user $TOMCAT_USER $CATALINA_HOME/bin/shutdown.sh > /dev/null
      RETVAL=$?
      if [ $RETVAL -eq 0 ]; then
          echo_success
      else
          echo_failure
      fi
      echo
      [ $RETVAL = 0 ] && rm -f /var/lock/subsys/$prog
      return $RETVAL
  }

  # See how we were called.
  case "$1" in
    start)
      start
      ;;
    stop)
      stop
      ;;
    restart)
      stop
      start
      ;;
    status)
      INSTANCES=`ps -aef|grep java|grep tomcat|grep org.apache.catalina.startup.Bootstrap|wc -l`
      if [ $INSTANCES -eq 0 ]; then
          #echo $prog is stopped
          echo $INST_NAME is stopped
          RETVAL=3
      else
          #if [ $INSTANCES -eq 1 ]; then
          #    echo $prog is running 1 instance...
          #else
          #    echo $prog is running $INSTANCES instances...
          #fi
          echo $INST_NAME is running instance...
          RETVAL=0
      fi
      ;;
    *)
      echo $"Usage: $prog {start|stop|restart|status|help}"
      exit 1
  esac

  exit $RETVAL


  ~~~~~
  chmod 755 setenv.sh
  chown tomcat:tomcat setenv.sh

chkconfigで登録
  chkconfig --add tomcat

/var/log側にログディレクトリを作成
  mkdir /var/log/tomcat
  rm -fr /usr/local/apache-tomcat-base/logs
  rm -fr /usr/local/tomcat/logs
  ln -s /var/log/tomcat /usr/local/apache-tomcat-base/logs
  ln -s /var/log/tomcat /usr/local/tomcat/logs
  chown -R tomcat:tomcat /usr/local/apache-tomcat-base
  chown -R tomcat:tomcat /usr/local/apache-tomcat-8.0.39
  chown -R tomcat:tomcat /var/log/tomcat

tomcat起動
  service tomcat start
 
tomcatの動作確認
  ブラウザで http://tomcatサーバIP:8080/ にアクセス

f:id:liaisondangereuse:20170113151129p:plain

 

管理ツールなど不要であれば削除
  cd /usr/local/apache-tomcat-base/webapps
  rm -rf docs examples host-manager manager