Linux设置tomcat为系统服务

对于需要在Linux系统上部署JSP,Servlet的Web应用服务器的人来说,将Tomcat安装为系统的服务(Service)是一个很常 见的任务,这篇文章给大家一个详细的安装步骤,我们选择的系统和软件版本为:CentOS5.1(RHEL5也是一样的), Tomcat6.0.16。具体安装步骤如下:

一,到Apache下载 Tomcat6.0.16(http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16 /bin/apache-tomcat-6.0.16.tar.gz)。

二,解压缩Tomcat文件到/opt目录下(需要root用户权限)

#tar xvzf apache-tomcat-6.0.16.tar.gz

确认你的目录为/opt/apache-tomcat-6.0.16

三,测试你的tomcat是不是能够正常运行(确保JAVA_HOME环境变量设置过)

启动

#/opt/apache-tomcat-6.0.16/start.sh

停止

#/opt /apache-tomcat-6.0.16/shutdown.sh

四,将Tomcat部署为系统的服务(以jsvc方式运行)

1,解压缩jsvc包(/opt/apache-tomcat-6.0.16/bin/jsvc.tar.gz)

#tar xvzf jsvc.tar.gz

2,编译jsvc,并测试(假设jdk的安装路径:/usr/java/jdk1.6.0_06)

#cd /opt/apache-tomcat-6.0.16/bin/jsvc-src

#chmod +x configure

#.configure --with-java=/usr/java/jdk1.6.0_06

#.make

#cp jsvc ../

#cp /opt/apache-tomcat-6.0.16/bin/jsvc-src/native/Tomcat5.sh /etc/init.d/tomcat

编辑刚copy的tomcat文件

#vi /etc/init.d/tomcat

编辑完成后的内容如下:

#!/bin/sh

#

# Startup Script for Tomcat6

#

# chkconfig: - 88 14

# description: Tomcat Daemon

# processname: jsvc

# pidfile: /var/run/jsvc.pid

# config:

#

# Source function library.

. /etc/rc.d/init.d/functions

#

# Adapt the following lines to your configuration

JAVA_HOME=/usr/java/jdk1.6.0_06

CATALINA_HOME=/opt/apache-tomcat-6.0.16

DAEMON_HOME=$CATALINA_HOME/bin

TOMCAT_USER=tomcat

# for multi instances adapt those lines.

TMP_DIR=$CATALINA_HOME/temp

PID_FILE=/var/run/jsvc.pid

CATALINA_BASE=$CATALINA_HOME

CATALINA_OPTS=

CLASSPATH=\

$JAVA_HOME/lib/tools.jar:\

$CATALINA_HOME/bin/commons-daemon.jar:\

$CATALINA_HOME/bin/bootstrap.jar

start(){

echo starting tomcat ...

$DAEMON_HOME/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

}

stop(){

echo stopping tomcat ...

$DAEMON_HOME/jsvc \

-stop \

-pidfile $PID_FILE \

org.apache.catalina.startup.Bootstrap

}

status() {

ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap" | awk '{printf $1 " "}' | wc | awk '{print $2}' >/tmp/tomcat_process_count.txt

read line < /tmp/tomcat_process_count.txt

if [ $line -gt 0 ]; then

echo -n "tomcat ( pid "

ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap" | awk '{printf $1 " "}'

echo ") is running..."

else

echo "Tomcat is stopped"

fi

}

case "$1" in

start)

# Start Tomcat

start

exit $?

;;

stop)

# Stop Tomcat

stop

exit $?

;;

restart)

stop

sleep 3

start

exit $?

;;

status)

status

exit $?

;;

*)

echo "Usage: tomcat {start|stop|restart|status}"

exit 1

;;

esac
添加执行用户

#useradd tomcat

确保tomcat用户能够访问tomcat的目录

#chown -R tomcat /opt/apache-tomcat-6.0.16

#chgrp -R tomcat /opt/apache-tomcat-6.0.16

#chmod +x /etc/init.d/tomcat

测试是否能启动tomcat

#/etc/init.d/tomcat start

#/etc/init.d/tomcat stop

将tomcat部署成为自动运行的服务

#chkconfig tomcat on

启动服务

#service tomcat start

查询服务状态

#service tomcat status

停止服务

#service tomcat stop

你可能感兴趣的:(linux)