ubuntu9.04安装tomcat及自启动

一、安装之前
1、下载tomcat

我下载的是apache-tomcat6.0.26.tar.gz
下载地址是:http://tomcat.apache.org/
安装之前要先安张jdk,最好选择JDK5.0以上版本吧。

二、安装ing
1、解压apache-tomcat-6.0.26.tar.gz到安装目录下。
根据俺的实际情况,我把tomcat安装在/opt目录下
shell>sudo tar zxvf apache-tomcat-6.0.26.tar.gz /opt
解压之后,会在/opt下生成一个apache-tomcat-6.0.26文件夹

2、在/usr/local下建立一个软链接
shell>cd /usr/local
shell>sudo ln -s /opt/apache-tomcat-6.0.26 tomcat6

3、测试安装
运行启动脚本,测试tomcat是否安装成功
shell>sudo /usr/local/tomcat6/bin/catalina.sh
然后打开FF,用http://localhost:8080 访问tomcat。

 

三、让tomcat开机启动
要是tomcat作为一个守护程序运行,需要用到jsvc工具。tomcat自带

1、准备工作
Tomcat安装后,如果让Tomcat用root身份启动,Tomcat将会拥用root权限,这将可能给系统带来安全隐患,黑客可能利用这个来攻击我们的系统,所以我们得添加一个独立运行此服务的用户,用下面的命令:

shell>sudo useradd -d /usr/local/tomcat6 -s /usr/sbin/nologin tomcat
以上命令为系统增加一个tomcat用户,并且设置为nologin

设置用户tomcat对tomcat6的权限
shell>sudo chown -R tomcat:tomcat /usr/local/tomcat6
shell> sudo chown -R tomcat:tomcat /opt/apache-tomcat-6.0.26
2、安装jsvc
shell>cd /usr/local/tomcat6/bin
shell>sudo tar xvfz jsvc.tar.gz
shell>cd jsvc-src
shell>sudo sh support/buildconf.sh
如果成功,命令终端打印出下面report:
support/buildconf.sh: configure script generated successfully

[注:在此之前,我的系统安装了autoconf,可以使用shell>sudo apt-get install autoconf 进行安装]
为configure添加执行权限
shell>sudo chmod 755 configure
shell>sudo ./configure --with-java=/usr/lib/j2sdk1.6-sun (这是俺系统的JDK安装的位置)
shell>sudo make

okay!如果没有出现错误的话,jsvc就安装成功了!

 

3、设置启动脚本
接下来,就是把jsvc中带有的一个tomcat5.sh的一个模板复制到/etc/init.d目录下,然后根据自己的情况,进行修改。
shell>sudo cp /usr/local/tomcat6/bin/jsvc-src/native/Tomcat5.sh /etc/init.d/tomcat
上面的命令是移动文件tomcat5.sh到/etc/init.d/里面并改名为tomcat

修改/etc/init.d/tomcat文件:
shell>cd /etc/init.d
shell>sudo gedit tomcat

tomcat文件修改完如下:

#!/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:
#
#
# # port="80" minProcessors="5" maxProcessors="75"
# enableLookups="true" redirectPort="8443"
# acceptCount="10" debug="0" connectionTimeout="60000"/>
#
# That is for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
tomcat文件修改完如下:

#!/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:
#
#
# # port="80" minProcessors="5" maxProcessors="75"
# enableLookups="true" redirectPort="8443"
# acceptCount="10" debug="0" connectionTimeout="60000"/>
#
# That is for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
JAVA_HOME=/opt/jdk1.6.0_20

CATALINA_HOME=/opt/apache-tomcat-6.0.26

DAEMON_HOME=/usr/local/tomcat6/bin
TOMCAT_USER=tomcat


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


CATALINA_OPTS=
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-src/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
#
# To get a verbose JVM
#-verbose /
# To get a debug of jsvc.
#-debug /
exit $?
;;

stop)
#
# Stop Tomcat
#
$DAEMON_HOME/src/native/unix/jsvc /
-stop /
-pidfile $PID_FILE /
org.apache.catalina.startup.Bootstrap
exit $?
;;

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

[注:需要修改的地方,我都用红色标出来]
如果在其他linux系统,这样就okay了,但是在ubuntu中,还需要进行一步,就是把在rc2.d中设置一个软链接
在/etc/rc2.d目录下建立S16apache软联接到/etc/init.d/tomcat
shell>cd /etc/rc2.d
shell>ln -s /etc/init.d/apache2 S16apache
[注:rc2.d目录下的文件要以S后就数字开头,表示启动的顺序。而rc2.d中的2,表示这个目录下文件在开机时的运行级别(runlevel)为2]

四、结束
重新启动系统
shell>sudo reboot -n
启动后,就可以在FF中直接输入http://localhost:8080。如果成功,就可以看见一直可爱的kitten拉。

你可能感兴趣的:(tomcat,ubuntu,apache,permissions,express,file)