CenOS系统中安装Tomcat7并设置为自启动服务
来自: http://www.cnblogs.com/gao241/archive/2012/09/17/2688308.html
1、 软件
a) 下载apache-tomcat-7.0.27 地址: http://apache.etoak.com/tomcat/tomcat-7/v7.0.27/bin/apache-tomcat-7.0.27.tar.gz
b) 如无特殊说明,本文档操作用户为tomcat
2、 安装
a) 解压
# tar zxvf apache-tomcat-7.0.27.zip
b) 移动到其他目录
# mv apache-tomcat-7.0.27 /home/tomcat/tomcat
c) 启动Tomcat服务
# cd /home/tomcat/tomcat/bin # ./startup.sh
d) 检查是否启动成功
在本机浏览器中访问URL: http://localhost:8080/
会出现Tomcat的欢迎页面
注:如果想远程通过浏览器访问,则在远程浏览器中输入http://<ip>:8080/
<ip> 可通过在本地机器上输入命令 ifconfig 查看
如果未能访问到tomcat的欢迎页面,需要查看本机的防火墙设置,是否打开了端口8080
e) 停止服务
# ./shutdown.sh
3、 可能出现的问题
a) Java未先安装,或未正确配置JAVA_HOME系统环境变量
b) Linux系统的防火墙8080端口未被设置为远程可访问
4、配置tomcat由tomcat用户自启动(使用jsvc)
a) 编译安装jsvc
# export CATALINA_HOME=/home/tomcat/tomcat # cd $CATALINA_HOME/bin # tar xvfz commons-daemon-native.tar.gz # cd commons-daemon-1.0.8-native-src/unix # ./configure # make # cp jsvc ../..
# vi /etc/init.d/tomcat7
添加如下脚本:
#! /bin/sh # chkconfig: 35 96 04 # description: Apache-Tomcat service script. # Version: # 2008-01-17-01 # By: # Typhoon.Free.Wolf. # Service name. SVC_NAME="tomcat7" # Lock file. LOCK_FILE="/var/lock/subsys/tfw_tomcat" # Installation prefix. APP_HOME="/home/tomcat/tomcat" # The process name to monitor. APP_PROC="java" # Authorize user. APP_USER="tomcat" SLEEP_START=5 SLEEP_STOP=$SLEEP_START # Importing functions. flg_func=0 if [ -f /etc/init.d/functions ] ; then . /etc/init.d/functions flg_func=1 elif [ -f /etc/rc.d/init.d/functions ] ; then . /etc/rc.d/init.d/functions flg_func=1 fi # Templete. # Return: # FLG_PROC_ON means process is there. # FLG_PROC_OFF means process not exists. # Other means error. FLG_PROC_ON=1 FLG_PROC_OFF=0 probe() { srch_rst=$(ps -A|grep $APP_PROC|awk '{print $4}') for rst in $srch_rst ; do if [ $APP_PROC = $rst ] ; then return $FLG_PROC_ON fi done return $FLG_PROC_OFF } # Templete. # Shows OK. # Return: echo_ok() { case $flg_func in 1) echo_success echo "" ;; *) echo "" echo " [ OK ]" ;; esac } # Templete. # Shows Passed. # Return: echo_pass() { case $flg_func in 1) echo_passed echo "" ;; *) echo "" echo " [PASSED]" ;; esac } # Templete. # Shows Failed. # Return: echo_bad() { case $flg_func in 1) echo_failure echo "" ;; *) echo "" echo " [FAILED]" ;; esac } # Templete. # Start section. # Return: _start() { probe proc=$? echo "" echo "Starting $SVC_NAME..." if (touch $LOCK_FILE) ; then echo_ok else echo_bad fi } start() { probe proc=$? echo "" echo "Starting $SVC_NAME..." if [ $FLG_PROC_OFF = $proc ] ; then if (su - $APP_USER -c "$APP_HOME/bin/startup.sh") ; then sleep $SLEEP_START if (touch $LOCK_FILE) ; then echo_ok else echo_pass echo "" fi else echo_bad fi elif [ $FLG_PROC_ON = $proc ] ; then if (touch $LOCK_FILE) ; then echo_passed echo "" else echo_bad fi fi } # Templete. # Stop section. # Return: _stop() { probe proc=$? echo "" echo "Stopping $SVC_NAME..." if (rm -Rf $LOCK_FILE) ; then echo_ok else echo_bad fi } stop() { probe proc=$? echo "" echo "Stopping $SVC_NAME..." if [ $FLG_PROC_OFF = $proc ] ; then if (rm -Rf $LOCK_FILE) ; then echo_passed echo "" else echo_bad fi elif [ $FLG_PROC_ON = $proc ] ; then if (su - $APP_USER -c "$APP_HOME/bin/shutdown.sh") ; then sleep $SLEEP_STOP if (rm -Rf $LOCK_FILE) ; then echo_ok else echo_pass echo "" fi else echo_bad fi fi } # Parse command line parameters. case $1 in start) start ;; stop) stop ;; restart) stop start ;; *) # Print help echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac exit 0
注意修改APP_HOME为tomcat实际安装路径,APP_USER为启动tomcat的用户。
c) 设置tomcat自启动服务
使用如下命令给tomcat7增加执行权限
# chmod +x tomcat7
将tomcat添加到启动服务中
# chkconfig --add tomcat7 # chkconfig --level 234 tomcat7 on
可以用如下命令确认是否配置成功
chkconfig --list tomcat7
之后就可以用service来控制tomcat的启动和停止了。
# service tomcat7 start # service tomcat7 stop