スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

tomcatインストールと設定(起動・停止スクリプトまで)

tomcatの稼働ユーザ:グループ -> tomcat:tomcat
JAVA_HOME -> /usr/local/java
CATALINA_HOME -> /usr/local/tomcat
設定ファイル -> /etc/tomcat/tomcat.conf、/etc/sysconfig/tomcat
起動・停止スクリプト -> /etc/init.d/tomcat

になるようにインストールする。OSはRHEL6系。

■tomcatユーザ・グループ作成(idは各々の環境で適切なものに)

# groupadd -g 555 tomcat
# useradd -u 555 -g tomcat -s /bain/bash -d /usr/local/tomcat -M tomcat

■JREインストール

# ./jre-6u33-linux-x64-rpm.bin
# cd /usr/local
# ln -s /usr/java/jre1.6.0_33 java

■tomcatインストール

# tar zxf apache-tomcat-7.0.29.tar.gz
# cp -R apache-tomcat-7.0.29 /usr/.
# cd /usr/local
# ln -s /usr/apache-tomcat-7.0.29 tomcat
# chown -R tomcat:tomcat tomcat/

# cd /var/log
# mkdir tomcat

# cd /etc
# mkdir tomcat

■設定(GCなどは各々の環境に合わせて)

# vi /etc/tomcat/tomcat.conf
# Where your java installation lives
JAVA_HOME="/usr/local/java"

# Where your tomcat installation lives
CATALINA_BASE="/usr/local/tomcat"
CATALINA_HOME="/usr/local/tomcat"
JASPER_HOME="/usr/local/tomcat"
CATALINA_TMPDIR="/usr/local/tomcat/temp"

# You can pass some parameters to java here if you wish to
#JAVA_OPTS="-Xminf0.1 -Xmaxf0.3"

# Use JAVA_OPTS to set java.library.path for libtcnative.so
#JAVA_OPTS="-Djava.library.path=/usr/lib64"

# Heap
JAVA_OPTS="-server -Xmx512m -Xms512m -XX:PermSize=64m -XX:NewRatio=2 -XX:SurvivorRatio=25 -XX:+UseParallelGC -XX:ParallelGCThreads=2"

# What user should run tomcat
#TOMCAT_USER="tomcat"

# You can change your tomcat locale here
#LANG="en_US"

# Run tomcat under the Java Security Manager
#SECURITY_MANAGER="false"

# Time to wait in seconds, before killing process
SHUTDOWN_WAIT="2"

# Whether to annoy the user with "attempting to shut down" messages or not
#SHUTDOWN_VERBOSE="false"

# Set the TOMCAT_PID location
CATALINA_PID="/var/run/tomcat.pid"

# Connector port is 8080 for this tomcat instance
#CONNECTOR_PORT="8080"

# If you wish to further customize your tomcat environment,
# put your own definitions here
# (i.e. LD_LIBRARY_PATH for some jdbc drivers)

もうひとつ

# vi /etc/sysconfig/tomcat

上記tomcat.confと同内容で全てコメントアウトしておく。

■起動・停止スクリプト
# vi /etc/init.d/tomcat

#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description:
# Short-Description: start and stop tomcat
### END INIT INFO
#
# - originally written by Henri Gomez, Keith Irwin, and Nicolas Mailhot
# - heavily rewritten by Deepak Bhole and Jason Corley
#

## Source function library.
#. /etc/rc.d/init.d/functions
# Source LSB function library.
if [ -r /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
else
exit 1
fi

DISTRIB_ID=`lsb_release -i -s 2>/dev/null`

NAME="$(basename $0)"
unset ISBOOT
if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
NAME="${NAME:3}"
ISBOOT="1"
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
SU="/sbin/runuser -s /bin/sh"
else
SU="/bin/su -s /bin/sh"
fi

# Get instance specific config file
if [ -r "/etc/sysconfig/${NAME}" ]; then
. /etc/sysconfig/${NAME}
fi

# Define which connector port to use
CONNECTOR_PORT="${CONNECTOR_PORT:-8080}"

# Path to the tomcat launch script
TOMCAT_SCRIPT="${CATALINA_HOME}/bin/catalina.sh"

# Tomcat program name
TOMCAT_PROG="${NAME}"

# Define the tomcat username
TOMCAT_USER="${TOMCAT_USER:-tomcat}"

# Define the tomcat log file
TOMCAT_LOG="${TOMCAT_LOG:-/var/log/${NAME}/catalina.out}"

RETVAL="0"

# Look for open ports, as the function name might imply
function findFreePorts() {
local isSet1="false"
local isSet2="false"
local isSet3="false"
local lower="8000"
randomPort1="0"
randomPort2="0"
randomPort3="0"
local -a listeners="( $(
netstat -ntl | \
awk '/^tcp/ {gsub("(.)*:", "", $4); print $4}'
) )"
while [ "$isSet1" = "false" ] || \
[ "$isSet2" = "false" ] || \
[ "$isSet3" = "false" ]; do
let port="${lower}+${RANDOM:0:4}"
if [ -z `expr " ${listeners[*]} " : ".*\( $port \).*"` ]; then
if [ "$isSet1" = "false" ]; then
export randomPort1="$port"
isSet1="true"
elif [ "$isSet2" = "false" ]; then
export randomPort2="$port"
isSet2="true"
elif [ "$isSet3" = "false" ]; then
export randomPort3="$port"
isSet3="true"
fi
fi
done
}

function makeHomeDir() {
if [ ! -d "$CATALINA_HOME" ]; then
mkdir -p /var/log/${NAME} \
/var/cache/${NAME} \
/var/tmp/${NAME}
ln -fs /var/cache/${NAME} ${CATALINA_HOME}/work
ln -fs /var/tmp/${NAME} ${CATALINA_HOME}/temp
ln -fs /var/log/${NAME} ${CATALINA_HOME}/logs
chown ${TOMCAT_USER}:root /var/log/${NAME}
fi
}

function parseOptions() {
options=""
if [ -r "/etc/sysconfig/${NAME}" ]; then
options="$options $(
awk '!/^#/ && !/^$/ { ORS=" ";
print "export ", $0, ";" }' \
/etc/sysconfig/${NAME}
)"
fi
TOMCAT_SCRIPT="$options ${TOMCAT_SCRIPT}"
}

# See how we were called.
function start() {

echo -n "Starting ${TOMCAT_PROG}: "
if [ "$RETVAL" != "0" ]; then
log_failure_msg
return
fi
if [ -f "/var/lock/subsys/${NAME}" ]; then
if [ -f "/var/run/${NAME}.pid" ]; then
read kpid < /var/run/${NAME}.pid
# if checkpid $kpid 2>&1; then
if [ -d "/proc/${kpid}" ]; then
log_success_msg
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
echo
fi
RETVAL="0"
return
fi
fi
fi
# fix permissions on the log and pid files
export CATALINA_PID="/var/run/${NAME}.pid"
touch $CATALINA_PID 2>&1 || RETVAL="4"
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then
chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID
fi
[ "$RETVAL" -eq "0" ] && touch $TOMCAT_LOG 2>&1 || RETVAL="4"
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then
chown ${TOMCAT_USER}:${TOMCAT_USER} $TOMCAT_LOG
fi
parseOptions
if [ "$RETVAL" -eq "0" -a "$SECURITY_MANAGER" = "true" ]; then
$SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start-security ${TOMCAT_LOG}" \
>> /var/log/${NAME}/initd.log 2>&1 || RETVAL="4"
else

[ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start ${TOMCAT_LOG}" >> /var/log/${NAME}/initd.log 2>&1 || RETV
AL="4"
fi
if [ "$RETVAL" -eq "0" ]; then
log_success_msg
touch /var/lock/subsys/${NAME}
else
log_failure_msg "Error code ${RETVAL}"
fi
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
echo
fi
}

function stop() {
echo -n "Stopping ${TOMCAT_PROG}: "
if [ -f "/var/lock/subsys/${NAME}" ]; then
parseOptions
if [ "$RETVAL" -eq "0" ]; then
touch /var/lock/subsys/${NAME} 2>&1 || RETVAL="4"
[ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} stop ${TOMCAT_LOG}" >> /var/log/${NAME}/initd.out 2>&1 || RET
VAL="4"
fi
if [ "$RETVAL" -eq "0" ]; then
count="0"
if [ -f "/var/run/${NAME}.pid" ]; then
read kpid < /var/run/${NAME}.pid
until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
[ "$count" -gt "$SHUTDOWN_WAIT" ]; do
if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
echo "waiting for processes $kpid to exit"
fi
sleep 1
let count="${count}+1"
done
if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
log_warning_msg "killing processes which did not stop after ${SHUTDOWN_WAIT} seconds"
fi
kill -9 $kpid
fi
log_success_msg
fi
rm -f /var/lock/subsys/${NAME} /var/run/${NAME}.pid
else
log_failure_msg
RETVAL="4"
fi
else
log_success_msg
RETVAL="0"
fi
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
echo
fi
}

function status()
{
checkpidfile
if [ "$RETVAL" -eq "0" ]; then
log_success_msg "${NAME} (pid ${kpid}) is running..."
elif [ "$RETVAL" -eq "1" ]; then
log_failure_msg "PID file exists, but process is not running"
else
checklockfile
if [ "$RETVAL" -eq "2" ]; then
log_failure_msg "${NAME} lockfile exists but process is not running"
else
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)"
if [ -z "$pid" ]; then
log_success_msg "${NAME} is stopped"
RETVAL="3"
else
log_success_msg "${NAME} (pid $pid) is running..."
RETVAL="0"
fi
fi
fi
}

function checklockfile()
{
if [ -f /var/lock/subsys/${NAME} ]; then
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)"
# The lockfile exists but the process is not running
if [ -z "$pid" ]; then
RETVAL="2"
fi
fi
}

function checkpidfile()
{
if [ -f "/var/run/${NAME}.pid" ]; then
read kpid < /var/run/${NAME}.pid
if [ -d "/proc/${kpid}" ]; then
# The pid file exists and the process is running
RETVAL="0"
else
# The pid file exists but the process is not running
RETVAL="1"
return
fi
fi
# pid file does not exist and program is not running
RETVAL="3"
}

function usage()
{
echo "Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}"
RETVAL="2"
}

# See how we were called.
RETVAL="0"
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart|try-restart)
if [ -f "/var/run/${NAME}.pid" ]; then
stop
start
fi
;;
reload)
RETVAL="3"
;;
force-reload)
if [ -f "/var/run/${NAME}.pid" ]; then
stop
start
fi
;;
status)
status
;;
version)
${TOMCAT_SCRIPT} version
;;
*)
usage
;;
esac

exit $RETVAL

■起動・停止スクリプトから呼び出すスクリプトの編集

# vi /usr/local/tomcat/bin/catalina.sh
SLEEP=5

↓変更

SLEEP=1

450行目付近
if [ $? -gt 0 ]; then
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$CATALINA_PID" ]; then
cat /dev/null > "$CATALINA_PID"
else
echo "Tomcat stopped but the PID file could not be removed or cleared."
fi
fi
break
fi

↓コメントアウト

#if [ $? -gt 0 ]; then
# rm -f "$CATALINA_PID" >/dev/null 2>&1
# if [ $? != 0 ]; then
# if [ -w "$CATALINA_PID" ]; then
# cat /dev/null > "$CATALINA_PID"
# else
# echo "Tomcat stopped but the PID file could not be removed or cleared."
# fi
# fi
# break
#fi

482行目付近
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
echo "Tomcat was killed but the PID file could not be removed."
fi

↓コメントアウト

#rm -f "$CATALINA_PID" >/dev/null 2>&1
#if [ $? != 0 ]; then
# echo "Tomcat was killed but the PID file could not be removed."
#fi

この記事のトラックバックURL

http://ysmt.blog21.fc2.com/tb.php/357-18166b6b

mod_proxy_balancerでセッション維持
tomcatの構成はこれを前提とする。 【server.xml(「AP1」と「AP2」で共通)】 &lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="${jvmRoute}"&gt; 【「AP1」の/etc/sysconfig/tomcat...

コメント

コメントする

管理者にだけ表示を許可する

Template Designed by DW99

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。