一、安装前的准备和相关说明
下载JDK,tomcat
我下载的是
apache-tomcat-6.0.24.tar.gz
jdk-6u18-linux-i586-rpm.bin
查看系统版本
[root@www ~]# uname -a
Linux www.digican-rfid.com 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
[root@www ~]#
相关说明:
网站目录为:/data/webapps
log目录为:/data/logs
二、安装JDK
[root@www ~]# cd /software
[root@www software]# chmod +x jdk-6u18-linux-i586-rpm.bin
[root@www software]# ./jdk-6u18-linux-i586-rpm.bin
[root@www software]# vi /etc/profile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在export PATH前增加以下内容
JAVA_HOME=/usr/java/jdk1.6.0_18
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
CATALINA_HOME=/usr/local/tomcat6
PATH=$PATH:/usr/local/tomcat6/bin/
在export PATH 的后面增加以下内容
JAVA_HOME CLASSPATH CATALINA_HOME
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@www software]# set 查看输出中有没有java的变量
[root@www software]# java -version 测试java
三、安装tomcat
[root@www software]# chmod +x apache-tomcat-6.0.24.tar.gz
[root@www software]# cd /usr/local
[root@www local]# tar -xzvf /software/apache-tomcat-6.0.24.tar.gz
[root@www local]# ln -s apache-tomcat-6.0.24 tomcat6
[root@www local]# cd tomcat6
[root@www tomcat6]# vi /etc/profile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在export PATH前增加以下内容
CATALINA_HOME=/usr/local/tomcat6
CATALINA_BASE=/usr/local/tomcat6
PATH=$PATH:/usr/local/tomcat6/bin/
在export PATH 的后面增加以下内容
CATALINA_HOME CATALINA_BASE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@www tomcat6]# set 查看输出中有没有java的变量
[root@www tomcat6]# startup.sh 启动tomcat
[root@www tomcat6]# netstat -ln | grep 80 要保证出现下面一行
tcp 0 0 :::8080 :::* LISTEN
在浏览器里输入 http://localhsot:8080 应该会出来tomcat的标志
[root@www tomcat6]# shutdown.sh 停止tomcat
[root@www tomcat6]# vi conf/server.xml 更改以下几行
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Engine name="Catalina" defaultHost="www.digican-rfid.com"> 更改默认主机
<Host name="www.digican-rfid.com" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/webapps/company" reloadable="true" caseSensitive="false" debug="0"/> 更改主机头和网站目录
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /> 更改端口
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@www tomcat6]# vi conf/tomcat-users.xml 更改以下几行
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<user username="tomcat" password="admin" roles="admin,manager"/> 增加tomcat管理用户
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@www tomcat6]# mkdir -pv /data/logs
[root@www tomcat6]# cp -R /usr/local/tomcat6/webapps /data
[root@www tomcat6]# rm -rf /usr/local/tomcat6/webapps
[root@www tomcat6]# ln -s /data/webapps /usr/local/tomcat6/webapps
[root@www tomcat6]# startup.sh 启动tomcat
[root@www tomcat6]# netstat -ln | grep 80 要保证出现下面一行
tcp 0 0 :::80 :::* LISTEN
在浏览器里输入 http://localhsot 应该会出来自定义的网站
[root@www tomcat6]# shutdown.sh 停止tomcat
[root@www tomcat6]#
tomcat安装完成
四、设置tomcat开机自启动
以下文章参考了网上的文章,但经过自己试验和整理。
linux自动启动程序的方法很多,但是有时某些程序要设置环境变量的就会引起麻烦。
tomcat启动时需要设置JAVA_HOME,CLASSPATH等环境变量,
很多文章说设置好环境变量,把/path/startup.sh放到rc.local中就可以实现自动启动了,
但是实际操作发现,很多的linux版本因为安全原因,rc.local中忽略了export命令,也就是说在这里设置的export都是无效的,同时,放在rc.local中默认会以root用户运行,只要在远程运行一个jsp文件就可能会删除系统文件,是极不安全的方式。
以下方法经过我的验证,可以实现。
在Linux下,Tomcat可以作为一个守护进程来启动以及停止,这个必须借助于项目commons-daemon中的jsvc工具。Tomcat安装完后就带有这个工具的源码{tomcat}/bin/jsvc.tar.gz。按照下列命令安装这个工具:
[root@www ~]# cd /usr/local/tomcat6/bin/
[root@www bin]# tar zxvf /usr/local/tomcat6/bin/jsvc.tar.gz
[root@www bin]# cd jsvc-src
[root@www jsvc-src]# chmod +x configure
[root@www jsvc-src]# ./configure --with-java=/usr/java/jdk1.6.0_18/
[root@www jsvc-src]# make
[root@www jsvc-src]# ./jsvc -help
jsvc工具本身带了一个脚本(在native目录下)用来启动和停止Tomcat守护进程,但是在试验中发现该脚本无法设置为自动启动,显示的错误信息是:tomcat 服务不支持 chkconfig,因此修改了此脚本,可以从此处下载:tomcat.txt
程序代码:(网上大部分代码有错误,经修改运行OK)
[root@www jsvc-src]# tar -xzvf bin/jsvc.tar.gz
[root@www jsvc-src]# cp /usr/local/tomcat6/bin/jsvc-src/native/Tomcat5.sh /etc/rc.d/init.d/tomcatd
[root@www jsvc-src]# useradd tomcat
[root@www jsvc-src]# chown -R tomcat /usr/local/tomcat6
[root@www jsvc-src]# vi /etc/rc.d/init.d/tomcatd
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/sh
##############################################################################
#
# 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:
#
# <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
# <Connector className="org.apache.catalina.connector.http.HttpConnector"
# port="80" minProcessors="5" maxProcessors="75"
# enableLookups="true" redirectPort="8443"
# acceptCount="10" debug="0" connectionTimeout="60000"/>
#
# That is for Tomcat-6.0.x (Apache Tomcat/6.0)
#
#
#chkconfig:345 87 13
#description: Tomcat Daemon
#以上两行是chkconfig 用的,如果没有,chkconfig 就不支持此服务
# processname: jsvc
# pidfile: /var/run/jsvc.pid
prog=tomcat
# Adapt the following lines to your configuration
JAVA_HOME=/usr/java/jdk1.6.0_18
CATALINA_HOME=/usr/local/tomcat6
DAEMON_HOME=$CATALINA_HOME/bin
TOMCAT_USER=tomcat
# for multi instances adapt those lines.
TMP_DIR=/tmp
CATALINA_OPTS=
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=$CATALINA_HOME
#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(){
echo -n "Starting $prog: "
#
# Start Tomcat
#
# $DAEMON_HOME/src/native/unix/jsvc \
$DAEMON_HOME/jsvc-src/jsvc \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Djava.io.tmpdir=$TMP_DIR \
-pidfile $PID_FILE \
-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
;;
*)
echo "Usage $0 start/stop"
exit 1;;
esac
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@www ~]# chmod +x /etc/rc.d/init.d/tomcatd
来验证守护进程是否可以正常启动和停止。
[root@www ~]# /etc/rc.d/init.d/tomcat start|stop 启动或停止tomcat
或
[root@www ~]# service tomcat start|stop
接下来就是把这个脚本设置成系统启动时自动执行,系统关闭时自动停止,使用如下命令:
[root@www ~]# chkconfig --add tomcatd
重新启动系统后可以启动的过程中看到Starting Tomcat ..... [OK]的信息,这样就完成设置过程。