2014年1月22日 星期三

Install Tomcat 7 as Linux Service

基本上在Tomcat 7裡面已經自帶了安裝成service的必要source code和shell script,但安裝到Linux上,例如:Fedora、CentOS還是需要動一點手腳。

如何compile daemon出來請參考:http://tomcat.apache.org/tomcat-7.0-doc/setup.html#Unix_daemon

只要做完第一個步驟,將compile出來的jsvc執行檔複製到$CATALINA_HOME/bin底下即可,此時在bin目錄下除了jsvc之外,還有一個重要的daemon.sh。

編輯daemon.sh增加兩行comment,如下方所示紅色部分:
daemon.sh
#!/bin/sh
#
# chkconfig: 35 90 10
# description: Start/Stop the Tomcat daemon.
#
# 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
35代表level 3和5會啟動這個service,90代表啟動順序,10代表關閉順序。

接下來切換到root身分,到/etc/init.d底下執行:
  1. ln -s $CATALINA_HOME/bin/daemon.sh tomcat
  2. chkconfig -add tomcat
這樣就已經完成service安裝,可以執行以下指令試驗看看開關Tomcat:
  1. /sbin/service tomcat start
  2. /sbin/service tomcat stop
預設service會用tomcat這個user啟動,如果沒有這個user可以先建一個,或是在$CATALINA_HOME/bin/setenv.sh裡面加入TOMCAT_USER=user name就可以了。