がじぇったー

お金と家電とプログラミングのブログ

【AWS】Tomcat8.0.36をEC2(CentOS6)にローカルインストールする手順

こんにちわ

がじぇったー (@hackmylife7) | Twitter

です。

f:id:gadgeterkun:20200508183701j:plain

前提

  • EC2をCentOS6で立ち上げ済み
  • tomcat8.0.36をEC2にローカルインストールする
  • userとgroupはtomcat:tomcatにする

手順

インストール手順

# cd /usr/local/src
# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.36/bin/apache-tomcat-8.0.36.tar.gz
# tar -xzvf apache-tomcat-8.0.36.tar.gz
# mv apache-tomcat-8.0.36 /usr/local/
# cd ..
# mv apache-tomcat-8.0.36/ tomcat8
# chown -R tomcat:tomcat tomcat8/

起動確認

# /usr/local/tomcat8/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
# ps -ef | grep tomcat
root     23444     1 45 08:39 pts/0    00:00:03 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat8/endorsed -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start
root     23470 21925  0 08:39 pts/0    00:00:00 grep tomcat
# /usr/local/tomcat8/bin/shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
# ps -ef | grep tomcat
root     23503 21925  0 08:40 pts/0    00:00:00 grep tomcat

起動スクリプト作成

# vi /etc/rc.d/init.d/tomcat
以下内容の貼り付け
#!/bin/bash

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

CATALINA_HOME="/usr/local/tomcat8/"
CATALINA_PID="/usr/local/tomcat8//catalina.pid"
TOMCAT_USER="root"
NAME="tomcat"
RETVAL=$?

case "$1" in
start)
  if [ -f "/var/lock/subsys/${NAME}" ]; then
    echo "Tomcat is already running"
    exit 1
  fi
  echo "Starting Tomcat"
  sudo -u ${TOMCAT_USER} ${CATALINA_HOME}/bin/startup.sh
  touch /var/lock/subsys/tomcat
;;
stop)
  if [ ! -f "/var/lock/subsys/${NAME}" ]; then
    echo "Tomcat is already stopping"
    exit 1
  fi
  echo "Stopping Tomcat"
  sudo -u ${TOMCAT_USER} ${CATALINA_HOME}/bin/shutdown.sh
  rm -f /var/lock/subsys/tomcat
;;
restart)
  $0 stop
  sleep 5
  $0 start
;;
status)
  if [ -f "${CATALINA_PID}" ]; then
    read kpid < ${CATALINA_PID}
    if [ -d "/proc/${kpid}" ]; then
      echo "${NAME} (pid ${kpid}) is running..."
    else
      echo "${NAME} is not running, but PID file exists"
    fi
  else
    echo "${NAME} is stopped"
  fi
;;
*)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
;;
esac

exit ${RETVAL}
# chkconfig: 345 85 15

起動してプロセスの確認

# chmod +x /etc/rc.d/init.d/tomcat
# /etc/init.d/tomcat start
Tomcat is already running
# ps -ef | grep tomcat
root      3016     1  9 18:22 pts/0    00:00:30 /usr/java/latest/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat8/endorsed -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start
root      3872  1762  0 18:28 pts/0    00:00:00 grep tomcat

# /etc/init.d/tomcat stop
Stopping Tomcat
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar

# ps -ef | grep tomcat
root      3912  1762  0 18:28 pts/0    00:00:00 grep tomcat

自動起動の設定

# chkconfig --add tomcat
# chkconfig --list tomcat
tomcat          0:off   1:off   2:off   3:on    4:on    5:on    6:off
# sudo reboot
# # ps -ef | grep tomcat
root      1549     1 56 18:33 ?        00:00:32 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat8/endorsed -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start