1台のサーバにTomcatを複数インストールしてポートを変えて起動する

1つ目のTomcatをポート8009(デフォルト)、2つ目のTomcatをポート8010で起動します。
インスタンスは別々に起動・終了できるようにPIDファイルを出力するようにします。
RHEL6系です。

# Tomcatのインストール
[Tomcat ダウンロードページ](http://tomcat.apache.org/download-60.cgi) で最新版を確認して下さい。

    # wget http://ftp.yz.yamagata-u.ac.jp/pub/network/apache/tomcat/tomcat-6/v6.0.39/bin/apache-tomcat-6.0.39.tar.gz
    # tar zxvf apache-tomcat-6.0.39.tar.gz
## 1つ目
    # mv apache-tomcat-6.0.39/ /usr/local/tomcat
## 2つ目
    # mv apache-tomcat-6.0.39/ /usr/local/tomcat_8010

# Tomcatが起動時にPIDファイルを出力するようにする

## 1つ目
```sh:/usr/local/tomcat/bin/catalina.sh
if [ -z "$LOGGING_MANAGER" ]; then
  LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
fi

# 追加 ここから ------------
if [ -z "$CATALINA_PID" ]; then
  CATALINA_PID=/var/run/tomcat.pid
fi
# 追加 ここまで ------------

# ----- Execute The Requested Command -----------------------------------------
```

## 2つ目
```sh:/usr/local/tomcat_8010/bin/catalina.sh
if [ -z "$LOGGING_MANAGER" ]; then
  LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
fi

# 追加 ここから ------------
if [ -z "$CATALINA_PID" ]; then
  CATALINA_PID=/var/run/tomcat_8010.pid
fi
# 追加 ここまで ------------

# ----- Execute The Requested Command -----------------------------------------
```

# ポートの変更(2つ目)
```diff:/usr/local/tomcat_8010/conf/server.xml
22c22
< <Server port="8005" shutdown="SHUTDOWN">
---
> <Server port="8006" shutdown="SHUTDOWN">
#ポート8080をコメントアウト
68a69
>     <!--
71a73
>     -->
90c92
<     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
---
>     <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
```

# 起動スクリプト
## 1つ目
```sh:/etc/rc.d/init.d/tomcat
#!/bin/bash
#
# Startup script for the tomcat
#
# chkconfig: 345 80 15
# description: Tomcat is a Servlet+JSP Engine.

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

export JAVA_HOME=/usr/java/default
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/usr/local/tomcat
export CATALINA_HOME=/usr/local/tomcat
export CLASSPATH=$CLASSPATH:$CATALINA_HOME/common/lib

CATALINA_PID=/var/run/tomcat.pid

start(){
    if [ ! `pgrep -F $CATALINA_PID 2> /dev/null` ]; then
        echo "Starting tomcat"
        /usr/local/tomcat/bin/startup.sh
        touch /var/lock/subsys/tomcat
    else
        echo "tomcat allready running"
    fi
}

stop(){
    if [ `pgrep -F $CATALINA_PID 2> /dev/null` ]; then
        echo "Shutting down tomcat"
        /usr/local/tomcat/bin/shutdown.sh
        rm -f /var/run/tomcat.pid
        rm -f /var/lock/subsys/tomcat
    else
        echo "tomcat not running"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        /usr/local/tomcat/bin/catalina.sh version
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
esac

exit 0
```

## 2つ目
```sh:/etc/rc.d/init.d/tomcat_8010
#!/bin/bash
#
# Startup script for the tomcat
#
# chkconfig: 345 80 15
# description: Tomcat is a Servlet+JSP Engine.

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

export JAVA_HOME=/usr/java/default
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/usr/local/tomcat_8010
export CATALINA_HOME=/usr/local/tomcat_8010
export CLASSPATH=$CLASSPATH:$CATALINA_HOME/common/lib

CATALINA_PID=/var/run/tomcat_8010.pid

start(){
    if [ ! `pgrep -F $CATALINA_PID 2> /dev/null` ]; then
        echo "Starting tomcat_8010"
        /usr/local/tomcat_8010/bin/startup.sh
        touch /var/lock/subsys/tomcat_8010
    else
        echo "tomcat_8010 allready running"
    fi
}

stop(){
    if [ `pgrep -F $CATALINA_PID 2> /dev/null` ]; then
        echo "Shutting down tomcat_8010"
        /usr/local/tomcat_8010/bin/shutdown.sh
        rm -f /var/run/tomcat_8010.pid
        rm -f /var/lock/subsys/tomcat_8010
    else
        echo "tomcat_8010 not running"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        /usr/local/tomcat_8010/bin/catalina.sh version
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
esac

exit 0
```

# Tomcat起動
## 1つ目
    # chmod +x /etc/rc.d/init.d/tomcat
    # /etc/rc.d/init.d/tomcat start

## 2つ目
    # chmod +x /etc/rc.d/init.d/tomcat_8010
    # /etc/rc.d/init.d/tomcat_8010 start

# Tomcat 自動起動
## 1つ目
    # chkconfig --add tomcat
    # chkconfig tomcat on
    # chkconfig --list tomcat
    tomcat     0:off   1:off   2:on    3:on    4:on    5:on    6:off

## 2つ目
    # chkconfig --add tomcat_8010
    # chkconfig tomcat_8010 on
    # chkconfig --list tomcat_8010
    tomcat_8010     0:off   1:off   2:on    3:on    4:on    5:on    6:off