1.安装jdk,在/etc/profile中配置java环境变量。
export JAVA_HOME=/usr/local/jdk1.5.0_09
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
2.安装tomcat。将三行环境变量脚本从profile文件移到/etc/rc.d/rc.local文件末尾。因为profile文件只有在用户登录进来时才会执行。而rc.local是系统启动后自动执行的。
在rc.local文件末尾的Java环境变量设置脚本后加入启动tomcat命令:
/usr/local/tomcat5.5/bin/startup.sh
linux下tomcat随系统自动启动脚本
# !/ bin / sh
#
# chkconfig: 345 86 14
# description: manager tomcat
#
# filename: / etc / rc.d / init.d / tomcat5
#
javaDir = `ls / usr / java`
for java in $javaDir
do
export JAVA_HOME = “ /usr/java/${java} “
done
export TOMCAT_HOME = “ /usr/tomcat5 “
case $ 1 in
start) $TOMCAT_HOME / bin / startup.sh
;;
stop) $TOMCAT_HOME / bin / shutdown.sh
;;
restart) $TOMCAT_HOME / bin / shutdown.sh
$TOMCAT_HOME / bin / startup.sh
;;
* ) echo “ Usage: $0 {start|stop|restart} “
;;
esac
exit 0
Tomcat作为linux系统服务自启动设置
一. 简单的方法
最简单的方法就是通过Tomcat自带的startup.sh来自动启动,编辑/etc/rc.d/rc.local,增加:
export JDK_HOME=/usr/local/jdk
export JAVA_HOME=/usr/local/jdk
/usr/local/tomcat/bin/startup.sh
二. 复杂而专业的方法
第一个方法无法指定启动Tomcat服务的用户,存在安全隐患,另外在系统关闭时无法自动停止服务。在Linux下,Tomcat可以作为一个守护进程来启动以及停止,这个必须借助于项目commons-daemon中的jsvc工具。Tomcat安装完后就带有这个工具的源码$CATALINA_HOME/bin/jsvc.tar.gz。首先安装这个工具:
# tar zxvf jsvc.tar.gz
# cd jsvc-src
# chmod +x configure
# ./configure --with-java=/usr/jdk
# make
# ./jsvc -help //检验
完成后是无法用chkconfig --add tomcat设置为自动启动,会显示错误信息:service tomcat does not support chkconfig ,需要修改脚本$CATALINA_HOME/bin/jsvc-src/native/Tomcat.sh,并cp到/etc/rc.d/init.d。我的启动脚本如下(中间出现了几次问题,结果发现都是脚本的问题,所以一定要谨慎):
#!/bin/sh
#
# Startup Script for Tomcat5
#
# chkconfig: 345 87 13
# description: Tomcat Daemon
# processname: jsvc
# pidfile: /var/run/jsvc.pid
# config:
#
# Source function library.
. /etc/rc.d/init.d/functions
#
prog=tomcat
#
# 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:
#
#
# # port=“80“ minProcessors=“5“ maxProcessors=“75“ # enableLookups=“true“ redirectPort=“8443“ # acceptCount=“10“ debug=“0“ connectionTimeout=“60000“/> # # You need a developement version of Tomcat (Apache Tomcat/4.1-dev) # # Adapt the following lines to your configuration JAVA_HOME=/usr/local/jdk CATALINA_HOME=/usr/local/tomcat DAEMON_HOME=$CATALINA_HOME/bin/jsvc-src TOMCAT_USER=tomcat TMP_DIR=/var/tmp CATALINA_OPTS= CLASSPATH=$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/bin/commons-daemon.jar:$CATALINA_HOME/bin/bootstrap.jar start(){ echo -n $“Starting $prog: “ # # Start Tomcat # $DAEMON_HOME/jsvc -user $TOMCAT_USER -home $JAVA_HOME -Dcatalina.home=$CATALINA_HOME -Djava.io.tmpdir=$TMP_DIR -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 RETVAL=$? [ $RETVAL = 0 ] && touch /var/lock/subsys/jsvc [ $RETVAL = 0 ] && echo_success || echo_failure echo return $RETVAL } stop(){ echo -n $“Stopping $prog: “ # # Stop Tomcat # PID=`cat /var/run/jsvc.pid` kill $PID RETVAL=$? [ $RETVAL = 0 ] && rm /var/lock/subsys/jsvc [ $RETVAL = 0 ] && echo_success || echo_failure echo return $RETVAL } case “$1“ in start) start ;; stop) stop ;; restart|reload) stop start ;; *) echo “Usage $0 start/stop“ exit 1;; esac 这样便大功告成了(某些路径、用户设置需要根据自己的具体环境修改)。可能还需要用chown tomcat tomcat及chmod +x tomcat修改属主及可执行权限