Linux系统系下tomcat自启动大体有两种配置方式:
【1】直接将启动代码写到/etc/rc.local里面。此种方式tomcat将以root权限运行,存在安全隐患,推荐第二种配置方式
【2】采用tomcat自带的jsvc脚本,下文仅介绍jsvc脚本的配置方式
步骤一:创建tomcat运行用户
#useradd tomcat (创建用于运行tomcat的用户)
#usergrp tomcat (创建运行tomcat用户所在的组)
#usermod -G tomcat tomcat (将tomcat运行用户添入新建组中)
#chown -R tomcat $CATALINA_HOME (修改tomcat安装目录所属用户)
#chgrp -R tomcat $CATALINA_HOM (修改tomcat安装目录所属组)
步骤二:生成jsvc脚本
1.tomcat的安装目录的bin目录下存在一个jsvc.tar.gz或commons-daemon-native.tar.gz压缩文件,对其解压
2.执行解压目录中的configure文件
3.执行make命令即可得到jsvc脚本并将其复制到$CATALINA_HOME/bin目录下(建议在执行之前先执行makge clean命令,以避免make命令执行时报如下错误:
ar: libservice.a: Malformed archive
make[1]: *** [libservice.a] Error 1)
步骤三:将 commons-daemon-native.tar.gz解压目录\unix\native 目录 下的Tomcat5.sh脚本复制到/etc/init.d目录下并重命名为tomcat后做如下修改(注意:1.红色字体即可须修改部分 2.脚本名一般和服务器保持一致,此即为将Tomcat5.sh修改成tomcat的原因)
#!/bin/sh
##############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# Small shell script to show how to start/stop Tomcat using jsvc
# If you want to have Tomcat running on port 80 please modify the server.xml
# file:
#
# <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
# <Connector className="org.apache.catalina.connector.http.HttpConnector"
# port="80" minProcessors="5" maxProcessors="75"
# enableLookups="true" redirectPort="8443"
# acceptCount="10" debug="0" connectionTimeout="60000"/>
#
# That is for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
JAVA_HOME=/usr/java/jdk1.5.0_15 (注意:不能引用系统的环境变量)
CATALINA_HOME=/usr/local/tomcat5 (注意:不能引用系统的环境变量)
DAEMON_HOME=$CATALINA_HOME
TOMCAT_USER=tomcat (设置tomcat将以何种用户执行)
# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=$CATALINA_HOME
#CATALINA_OPTS="-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs"
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar
case "$1" in
start)
$DAEMON_HOME/bin/jsvc \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Dcatalina.base=$CATALINA_BASE \
-Djava.io.tmpdir=$TMP_DIR \
-wait 10 \
-pidfile $PID_FILE \
-outfile $CATALINA_HOME/logs/catalina.out \
-errfile '&1' \
$CATALINA_OPTS \
-cp $CLASSPATH \
org.apache.catalina.startup.Bootstrap
#
# To get a verbose JVM
#-verbose \
# To get a debug of jsvc.
#-debug \
exit $?
;;
stop)
#
# Stop Tomcat
#
$DAEMON_HOME/bin/jsvc \
-stop \
-pidfile $PID_FILE \
org.apache.catalina.startup.Bootstrap
exit $?
;;
*)
echo "Usage tomcat.sh start/stop"
exit 1;;
esac
步骤四:查看/etc/rc.d/init.d目录下是否已存所修改的脚本,若不存在则从/etc/init.d目录下复制一份
步骤五:#chkconfig --add tomcat (将tomcat添入系统自启动服务中)
注意:若报service tomcat does not support chkconfig 的错误提示,则须在脚本的最前面加上
#!/bin/bash
# chkconfig: 2345 10 90
# description: Starts and Stops the Tomcat daemon. (经测试此句绝不能少)
再运行如下命令即可
sudo /sbin/chkconfig –add tomcat
sudo /sbin/chkconfig –list
步骤六:执行命令: service tomcat start|stop|restart|status 测试tomcat服务是否可手动启动(注意:采用service启动后的tomcat无法通过$CATALINA_HOME/shutdown.sh来关 闭,必须采用service tomcat stop方式来关闭)
步骤七:重启服务器