首先我们需要为tomcat配置JAVA的环境变量, 直接写到 setclasspath.sh 文件的开头即可, 我写入的内容如下所示
#--------------------- # java path #--------------------- JAVA_HOME=/usr/lib/jvm/java-8-oracle JRE_HOME=$JAVA_HOME/jre JAVA_BIN=$JAVA_HOME/bin CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export JAVA_HOME JRE_HOME PATH CLASSPATH刚开始的时候, 在 CLASSPATH 的开始配置了当前目录.
即
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib导致运行的时候老是出错, 其主要就是运行环境找不到, 因为 tomcat 运行需要 bootstrap.jar 和 tomcat-juli.jar, 运行的时候在当前 bin 目录下找到了这两个 bin 包, 但是关闭服务的时候, 目录更改了这两个 jar 包就没有找到, 导致服务能起来, 但是却结束不了
运行环境检查
配置完 JAVA 之后, 检查运行环境是否正确
sudo sh configtest.sh我们提到了 catalina.sh 脚本其实就是一个完备的服务集合, 因此上面的命令等价于
sudo sh catalina.sh configtestconfigtest.sh 脚本其实就是运行了catalina.sh configtest 命令, 这点可以从其源码中看到
PRGDIR=`dirname "$PRG"` EXECUTABLE=catalina.sh exec "$PRGDIR"/"$EXECUTABLE" configtest "$@"如果 ERROR 的话, 说明没有配置没有问题
直接运行
sudo ./startup.sh等价于
sudo sh catalina.sh start关闭
./shutdown.sh等价于
sudo sh catalina.sh stop打开:8080
2.2.5 配置启动脚本catalina.sh 脚本可以直接作为服务来使用, 我们做一个了链接即可
cd /etc/init.d sudo ln -s /opt/apache-tomcat/catalina.sh ./tomcat8当然我们也可以自己做一个
#!/bin/sh # # /etc/init.d/tomcat8 -- startup script for the Tomcat 8 servlet engine # # Written by Miquel van Smoorenburg <miquels@cistron.nl>. # Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>. # Modified for Tomcat by Stefan Gybas <sgybas@debian.org>. # Modified for Tomcat6 by Thierry Carrez <thierry.carrez@ubuntu.com>. # Modified for tomcat8 by Ernesto Hernandez-Novich <emhn@itverx.com.ve>. # Additional improvements by Jason Brittain <jason.brittain@mulesoft.com>. # ### BEGIN INIT INFO # Provides: tomcat8 # Required-Start: $local_fs $remote_fs $network # Required-Stop: $local_fs $remote_fs $network # Should-Start: $named # Should-Stop: $named # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start Tomcat. # Description: Start the Tomcat servlet engine. ### END INIT INFO set -e PATH=/bin:/usr/bin:/sbin:/usr/sbin NAME=tomcat8 DESC="Tomcat servlet engine" if [ `id -u` -ne 0 ]; then echo "You need root privileges to run this script" exit 1 fi # Make sure tomcat is started with system locale if [ -r /etc/default/locale ]; then . /etc/default/locale export LANG fi . /lib/lsb/init-functions if [ -r /etc/default/rcS ]; then . /etc/default/rcS fi case "$1" in start) log_daemon_msg "Starting $DESC" "$NAME" sh /opt/apache-tomcat/bin/startup.sh ;; stop) log_daemon_msg "Stopping $DESC" "$NAME" sh /opt/apache-tomcat/bin/shutdown.sh ;; status) set +e if [ "$?" = "0" ]; then if [ -f "$CATALINA_PID" ]; then log_success_msg "$DESC is not running, but pid file exists." exit 1 else log_success_msg "$DESC is not running." exit 3 fi else log_success_msg "$DESC is running with pid `cat $CATALINA_PID`" fi set -e ;; restart|force-reload) $0 stop sleep $0 start ;; *) log_success_msg "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac exit 0那么启动 tomcat8
service tomcat8 start OR etc/init.d/tomcat8 start停止 tomcat8
service tomcat8 stop OR etc/init.d/tomcat8 stop 2.2.6 高级配置之jsvc启动 sudo apt-get install jsvc