Chinaunix首页 | 论坛 | 博客
  • 博客访问: 831721
  • 博文数量: 137
  • 博客积分: 3477
  • 博客等级: 中校
  • 技术积分: 1409
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-30 21:53
文章分类

全部博文(137)

文章存档

2024年(8)

2023年(10)

2022年(4)

2021年(10)

2020年(9)

2018年(1)

2017年(1)

2014年(4)

2013年(3)

2012年(12)

2011年(24)

2010年(2)

2009年(8)

2008年(6)

2007年(34)

2006年(1)

分类: LINUX

2008-06-19 15:52:10

版权: (c) 风之舞原创作品,转载请注明来源 , Inc . 2008-06-08

写在前面的话:

RedHat Server AS4 系统下并没有自带 Tomcat 的 WEB 容器,RHEL ELS5 系统下自带的 Tomcat 容器也仅为 Tomcat5 ,版本略嫌低了一些,如何在 RHEL AS4/ELS5 系统下安装使用 Tomcat 6 呢,本文将带领大家完成一个 Tomcat6 的安装与自启动的配置过程。

    step 1:)首先,我们需要在系统中安装 Tomcat 服务器组件,在这里我选择的是 Tomcat6.0.16 。       
  • 拷贝 apache-tomcat-6.0.16.tar.gz 文件到 /usr/local 目录并展开该软件包。
          # cp apache-tomcat-6.0.16.tar.gz /usr/local
          # cd /usr/local
          # tar zxvf apache-tomcat-6.0.16.tar.gz       
  • 在 /usr/local 目录下创建 tomcat6 的链接。
          # ln -s apache-tomcat-6.0.16 tomcat6

        step 2:)创建 tomcat 用户和 tomcat 组。       
  • 创建 tomcat 用户组
          # groupadd tomcat       
  • 创建 tomcat 用户。
          # useradd -d /usr/local/tomcat6 -g tomcat -s /bin/sh tomcat

        step 3:)修改 /usr/local/apache-tomcat-6.0.16 目录及所属文件的用户和用户组属性 。
          # cd /usr/local
          # chown -R tomcat:tomcat apache-tomcat-6.0.16

        step 4:)在 /var/run 目录下创建 Tomcat6 启动进程文件 tomcat6.pid 。
          # cd /var/run
          # cat /dev/null >tomcat6.pid
          # chown tomcat:tomcat tomcat6.pid

        step 5:)在 /etc/init.d 目录下创建 Tomcat6 自启动脚本文件 tomcat6
          # cd /etc/init.d
          # vi tomcat6
          
  • tomcat6 的文件内容如下:
            #!/bin/bash
            #
            # tomcat6 This shell script takes care of starting and stopping Tomcat
            #
            # chkconfig: - 81 21
            #
            ### BEGIN INIT INFO
            # Provides: tomcat6
            # Required-Start: $network $syslog
            # Required-Stop: $network $syslog
            # Default-Start:
            # Default-Stop:
            # Description: Release implementation for Servlet 2.4 and JSP 2.0
            # 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
            #
            
            # commented out until the RHEL and FC daemon functions converge
            # Source the function library
            #if [ -r "/etc/rc.d/init.d/functions" ]; then
            #. /etc/rc.d/init.d/functions
            #fi

            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"
            else
                SU="su"
            fi

            # Get the tomcat config (use this for environment specific settings)
            TOMCAT_CFG="/usr/local/tomcat6/conf/tomcat6.conf"
            if  [  -r  "$TOMCAT_CFG" ]; then
                .  ${TOMCAT_CFG}
            fi

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

            # Define which tomcat home direct
            CATALINA_HOME="/usr/local/tomcat6"

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

            # Path to the tomcat launch script
            TOMCAT_SCRIPT="/usr/local/tomcat6/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:-/usr/local/tomcat6/logs/catalina.out}"

            RETVAL="0"

            # remove when the RHEL and FC daemon functions converge
            # (pulled from /etc/rc.d/init.d/functions)
            function checkpid()  {
                local i
                for i in $* ; do
                    if [ -d "/proc/${i}" ]; then
                        return 0
                    fi
                done
                return 1
            }

            # remove when the RHEL and FC daemon functions converge
            # (pulled from /etc/rc.d/init.d/functions)
            function echo_failure() {
                echo -en "\\033[60G"
                echo -n "[ "
                echo -n $"FAILED"
                echo -n " ]"
                echo -ne "\r"
                return 1
            }

            # remove when the RHEL and FC daemon functions converge
            # (pulled from /etc/rc.d/init.d/functions)
            function echo_success() {
                echo -en "\\033[60G"
                echo -n "[ "
                echo -n $"OK"
                echo -n " ]"
                echo -ne "\r"
                return 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 parseOptions() {
                options=""
                options="$options $(
                    awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \
                    $TOMCAT_CFG
                )"
                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 [ -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
                            echo "$NAME process already running"
                            return -1
                        else
                            echo "lock file found but no process running for"
                            echo "pid $kpid, continuing"
                        fi
                    fi
                fi
                export CATALINA_PID="/var/run/${NAME}.pid"
                touch $CATALINA_PID
                chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID
                if [ "$CATALINA_HOME" != "/usr/local/tomcat6" ]; then
                    # If CATALINA_HOME doesn't exist modify port number so that
                    # multiple instances don't interfere with each other
                    findFreePorts
                    sed -i -e "s/8005/${randomPort1}/g" -e "s/8080/${CONNECTOR_PORT}/g" \
                        -e "s/8009/${randomPort2}/g" -e "s/8443/${randomPort3}/g" \
                    ${CATALINA_HOME}/conf/server.xml
                fi
                $SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT start" >> $TOMCAT_LOG 2>&1
                RETVAL="$?"
                if [ "$RETVAL" -eq 0 ]; then
                    echo_success
                    touch /var/lock/subsys/${NAME}
                else
                    echo_failure
                fi
                echo
                return $RETVAL
            }

            function status() {
                RETVAL="1"
                if [ -f "/var/run/${NAME}.pid" ]; then
                    read kpid < /var/run/${NAME}.pid
                    if checkpid $kpid 2>&1; then
                        echo "$0 is already running (${kpid})"
                        RETVAL="0"
                    else
                        echo "$0 is running for pid $kpid"
                    fi
                else
                    pid="$(pgrep -u tomcat java)"
                    if [ -n "$pid" ]; then
                        echo "$0 running (${pid}) but no PID file exists"
                        RETVAL="0"
                    else
                        echo "$0 is stopped"
                    fi
                fi
                return $RETVAL
            }

            function stop() {
                local STOP_VERBOSE="false"
                echo -n "Stopping $TOMCAT_PROG: "
                if [ -f "/var/lock/subsys/${NAME}" ]; then
                    $SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop" >> $TOMCAT_LOG 2>&1
                    RETVAL="$?"
                    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 [ "$STOP_VERBOSE" = "true" ]; then
                                    echo -n -e "\nwaiting for processes $kpid to exit"
                                fi
                                sleep 1
                                let count="${count}+1"
                            done
                            if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
                                if [ "$STOP_VERBOSE" = "true" ]; then
                                    echo -n -e "\nkilling processes which didn't stop"
                                    echo -n -e "after "
                                    echo -n "$SHUTDOWN_WAIT seconds"
                                fi
                                kill -9 $kpid
                            fi
                            echo_success
                            if [ "$count" -gt "0" ]; then
                                echo -n -e "\n"
                            fi
                        fi
                        rm -f /var/lock/subsys/$NAME /var/run/$NAME.pid
                    else
                        echo_failure
                    fi
                fi
            }


            # See how we were called.
            case "$1" in

                    parseOptions
                    start
                    ;;
                stop)
                    parseOptions
                    stop
                    ;;
                restart)
                    parseOptions
                    stop
                    sleep 2
                    start
                    ;;
                condrestart)
                    if [ -f "/var/run/${NAME}.pid" ]; then
                        parseOptions
                        stop
                        start
                    fi
                    ;;
                status)
                    status
                    ;;
                version)
                    parseOptions
                    "${JAVA_HOME}/bin/java" \
                        -classpath "${CATALINA_HOME}/server/lib/catalina.jar" \
                            org.apache.catalina.util.ServerInfo
                    ;;
                *)
                    echo "Usage: $TOMCAT_PROG {start|stop|restart|condrestart|status|version}"
                    exit 1
            esac

            exit $RETVAL

        step 6:)将 tomcat6 自启动服务加入到系统的 xinit 服务进程中:
        # chkconfig tomcat6 on

        step 7:)在 /usr/local/tomcat6/conf 目录下创建 tomcat6 启动环境参数配置文件 tomcat6.conf ,并修改其属主关系为 tomcat 用户和 tomcat 用户组。
        # cd /usr/local/tomcat6/conf
        # vi tomcat6.conf       
  • tomcat6.conf 的文件内容如下:
            # tomcat6 service configuration file

            # you could also override JAVA_HOME here
            # Where your java installation lives
            # JAVA_HOME="/usr/lib/jvm/java"
            JAVA_HOME="/opt/ibm/java2-i386-50"
            JRE_HOME="/opt/ibm/java2-i386-50/jre"
            
            # Where your tomcat installation lives
            # That change from previous RPM where TOMCAT_HOME
            # used to be /var/tomcat.
            # Now /var/tomcat will be the base for webapps only
            CATALINA_HOME="/usr/local/tomcat6"
            JASPER_HOME="/usr/local/tomcat6"
            CATALINA_TMPDIR="/usr/local/tomcat6/temp"
            #JAVA_ENDORSED_DIRS="/usr/share/tomcat5/common/endorsed"
            
            # 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/lib
            
            # Bug 190:
            #
            # System property catalina.ext.dirs should be set to its default value
            # for ExtensionValidator to be functional.
            JAVA_OPTS="$JAVA_OPTS -Dcatalina.ext.dirs=$CATALINA_HOME/shared/lib:$CATALINA_HOME/common/lib"
            
            # What user should run tomcat
            TOMCAT_USER="tomcat"
            
            # You can change your tomcat locale here
            #LANG=en_US
            
            # Time to wait in seconds, before killing process
            SHUTDOWN_WAIT=30
            
            # Set the TOMCAT_PID location
            CATALINA_PID=/var/run/tomcat6.pid
            
            # Connector port is 8080 for this tomcat6 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)
            # Just do not forget to export them :)
          # chmod tomcat:tomcat tomcat6.conf

        step 8:)将如下内容加入到 /usr/local/tomcat6/conf/tomcat-users.xml 文件的 节中。
            
            
            
            
            
            
            
            
            

        step 9:)修改 /usr/local/tomcat6/bin/catalina.sh 文件内容如下:       
  • 在文件 /usr/local/tomcat6/bin/catalina.sh 中找到如下语句:
            # Only set CATALINA_HOME if not already set
            [ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." ; pwd`

            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
          大约在第 107 ~ 108 行的位置。       
  • 在其下面加入如下内容:
            if [ -z "$CATALINA_PID" ]; then
                export CATALINA_PID=/var/run/tomcat6.pid
            fi

        step 10:)安装 JDK (本例子中我采用的是 IBM 公司的 JDK 1.5 ,安装包的文件名为 ibm-java2-i386-sdk-5.0-6.0.i386.rpm)。       
  • 安装 IBM JDK 1.5 软件包
          # rpm -ivh ibm-java2-i386-sdk-5.0-6.0.i386.rpm       
  • 配置 JAVA 环境变量(根据您使用的 shell 不同,修改 ~/.bashrc 或 ~/.cshrc 或 ~/.profile 文件) 。
          (BASH)
          # vi .bashrc
          (环境参数内容如下)
          export JAVA_HOME=/opt/ibm/java2-i386-50
          export JAVACMD=$JAVA_HOME/bin/java
          export JRE_HOME=$JAVA_HOME/jre
          export PATH=$JAVA_HOME/bin:$PATH
          export CLASSPATH=$JAVA_HOME/lib/tools/jar:$JAVA_HOME/lib/dt.jar
          (CSH)
          # vi .cshrc
          (环境参数内容如下)
          setenv JAVA_HOME "/opt/ibm/java2-i386-50"
          setenv JAVACMD "/opt/ibm/java2-i386-50/bin/java"
          setenv JRE_HOME "/opt/ibm/java2-i386-50"
          setenv PATH "$JAVA_HOME/bin:$PATH"
          setenv CLASSPATH "$JAVA_HOME/lib/tools/jar:$JAVA_HOME/lib/dt.jar"

          至此,所有的安装工作均已经完成,重新启动机器后,tomcat6 就已经随系统而启动了。

  • 备注:
  • tomcat6 的文件权限必须为 755
  • tomcat6.conf 文件的用户和用户组的属主关系必须是 tomcat 用户和 tomcat 用户组
  • tomcat6 和 tomcat6.conf 文件我已经写好,可以直接从光盘的 tomcat6/ 目录下拷贝使用,不必再自己编写,但应该注意 tomcat6 文件应拷贝到 /etc/init.d 目录下,而 tomcat6.conf 文件应该拷贝到 /usr/local/tomca6/conf 目录下。
  • tomcat6 和 tomcat6.conf 文件中的 JAVA_HOME 环境变量可根据自己的 JDK 的安装目录自行调整,由于本例子中我使用的是 IBM 公司的 JDK 1.5 ,安装目录在 /opt/ibm/java2-i386-50 ,因此我在本例中的 JAVA_HOME 配置为 JAVA_HOME="/opt/ibm/java2-i386-50"
  • 如果经过上述操作后,您依然无法通过浏览器访问您的 tomcat6 WEB 服务器,请检查您的防火墙设置。
  • 阅读(4133) | 评论(0) | 转发(1) |
    给主人留下些什么吧!~~