2010年1月5日 星期二

設置tomcat用jsvc啟動-轉

  1. 原文出處-http://blog.c1gstudio.com/archives/324
  2. http://www.eit.name/blog/read.php?424

根據tomcat官方的document說明,linux下設置開機自啟動可以使用jsvc(tomcat包裡自帶)
設置tomcat以獨立的用戶運行(防止以root運行,權限太大)

shell>sudo useradd -d /opt/tomcat -s /usr/sbin/nologin tomcat
上邊的這個命令是添加一個系統用戶tomcat,並且設置為不可登錄系統。
設置用戶tomcat 對tomcat6的訪問權限

shell>sudo chown -R tomcat:website /opt/tomcat
安裝jsvc,前提需要系統安裝了gcc(系統一般默認安裝),autoconf,libc6-dev

解壓jsvc.tar.g
shell>cd /opt/tomcat/bin
shell>sudo tar zxvf jsvc.tar.gz
準備編譯配置文件
shell>cd jsvc-src
shell>sudo sh support/buildconf.sh
如果成功,命令終端會打出:
support/buildconf.sh: configure script generated successfully
為configure添加執行權限
shell>sudo chmod 755 configure
編譯
shell>sudo ./configure –with-java=/usr/java/jdk1.6.0_10(java的安裝路徑)
shell>sudo make
如果沒有出現錯誤,jsvc就安裝成功了。
設置啟動腳本

#!/bin/sh
# chkconfig: 2345 10 90
# description: Starts and Stops the Tomcat daemon.
##############################################################################
#
# Copyright 2004 The Apache Software Foundation.
#
# Licensed 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:
#
#
#
#
# That is for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
JAVA_HOME=/usr/java/default
CATALINA_HOME=/usr/local/tomcat
DAEMON_HOME=/usr/local/tomcat/bin/jsvc
TOMCAT_USER=webadmin
JAVA_OPTS="-Xms512m -Xmx1024m -XX:PermSize=64M"

# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=/usr/local/tomcat

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)
#
# Start 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' \
$JAVA_OPTS \
$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/jsvc \
-stop \
-pidfile $PID_FILE \
org.apache.catalina.startup.Bootstrap
exit $?
;;

*)
echo "Usage tomcat.sh start/stop"
exit 1;;
esac

在tomcat文件的頭兩行的註釋語句中,需要包含chkconfig和description兩部分內容(確認不要拼寫錯誤,),否則在執行「chkconfig --add tomcat」時,會出現「tomcat服務不支持chkconfig」的錯誤提示。

chkconfig這行表示缺省啟動的運行級別以及啟動和停止的優先級,如該服務缺省不再任何運行級啟動,則以 - 代替運行級別。在tomcat中表示腳本在運行級2、3、4、5啟動,啟動優先權為10,停止優先權為90。

description行對服務進行描述,可以用 「\」 跨行註釋。

沒有留言: