Tomcat的上传设置和开机自启动

Tomcat的确还是轻量了些,测试中需要上传一个比较大的文件,约5M,在上传过程中报错,查找原因,发现默认配置最大只能上传2M,所以呢在配置文件server.xml中配置Connector时可以指定maxPostSize属性来限制上传文件的大小,我们这里设置为

maxPostSize=”0″

对上传的文件大小不作限制,当然了如果前端还有代理服务,上面的超时时间可能也是需要改改的。

然后再把日志部分设置一下,我嚓,不加标签wordpress居然会把下面这段给吞了。

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="../../../../tmp/www.cia.gov.log-" suffix=""

pattern="combined" fileDateFormat="yyyyMMdd"/>

Tocmat自启动约莫有两种方式,这两种方式各有特点。

第一种,使用jsvc开机脚本启动,这种方式的特点是,开机启动,tomcat可以运行在80端口,但子进程可以其它非root用户运行,www用户是可以对tomcat目录文件进行读写的,大家知道在Unix系统中普通用户是无法把服务开放在80端口的,这个脚本如下所示,呵呵,当然还需要编译jsvc了,自己去查,多说无益,乖,这种方式比较适合于使用war包批量部署的情况。

#!/bin/sh
# chkconfig: 2345 10 90
# description:Tomcat service
export JAVA_HOME=/usr/java/jdk1.6.0_32/
export CATALINA_HOME=/home/www/tomcat7
export TOMCAT_USER=www

# 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
#
# 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.
#
# -----------------------------------------------------------------------------
# Commons Daemon wrapper script.
#
# $Id: daemon.sh 1202058 2011-11-15 06:37:12Z mturk $
# -----------------------------------------------------------------------------
#
# resolve links - $0 may be a softlink
ARG0="$0"
while [ -h "$ARG0" ]; do
 ls=`ls -ld "$ARG0"`
 link=`expr "$ls" : '.*-> \(.*\)$'`
 if expr "$link" : '/.*' > /dev/null; then
 ARG0="$link"
 else
 ARG0="`dirname $ARG0`/$link"
 fi
done
DIRNAME="`dirname $ARG0`"
PROGRAM="`basename $ARG0`"
while [ ".$1" != . ]
do
 case "$1" in
 --java-home )
 JAVA_HOME="$2"
 shift; shift;
 continue
 ;;
 --catalina-home )
 CATALINA_HOME="$2"
 shift; shift;
 continue
 ;;
 --catalina-base )
 CATALINA_BASE="$2"
 shift; shift;
 continue
 ;;
 --catalina-pid )
 CATALINA_PID="$2"
 shift; shift;
 continue
 ;;
 --tomcat-user )
 TOMCAT_USER="$2"
 shift; shift;
 continue
 ;;
 * )
 break
 ;;
 esac
done
# OS specific support (must be 'true' or 'false').
cygwin=false;
darwin=false;
case "`uname`" in
 CYGWIN*)
 cygwin=true
 ;;
 Darwin*)
 darwin=true
 ;;
esac

# Use the maximum available, or set MAX_FD != -1 to use that
test ".$MAX_FD" = . && MAX_FD="maximum"
# Setup parameters for running the jsvc
#
test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat
# Set JAVA_HOME to working JDK or JRE
# JAVA_HOME=/opt/jdk-1.6.0.22
# If not set we'll try to guess the JAVA_HOME
# from java binary if on the PATH
#
if [ -z "$JAVA_HOME" ]; then
 JAVA_BIN="`which java 2>/dev/null || type java 2>&1`"
 test -x "$JAVA_BIN" && JAVA_HOME="`dirname $JAVA_BIN`"
 test ".$JAVA_HOME" != . && JAVA_HOME=`cd "$JAVA_HOME/.." >/dev/null; pwd`
else
 JAVA_BIN="$JAVA_HOME/bin/java"
fi

# Only set CATALINA_HOME if not already set
test ".$CATALINA_HOME" = . && CATALINA_HOME=`cd "$DIRNAME/.." >/dev/null; pwd`
test ".$CATALINA_BASE" = . && CATALINA_BASE="$CATALINA_HOME"
test ".$CATALINA_MAIN" = . && CATALINA_MAIN=org.apache.catalina.startup.Bootstrap
test ".$JSVC" = . && JSVC="$CATALINA_BASE/bin/jsvc"

# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
CLASSPATH=
JAVA_OPTS=
if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
 . "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
 . "$CATALINA_HOME/bin/setenv.sh"
fi

# Add on extra jar files to CLASSPATH
test ".$CLASSPATH" != . && CLASSPATH="${CLASSPATH}:"
CLASSPATH="$CLASSPATH$CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar"

test ".$CATALINA_OUT" = . && CATALINA_OUT="$CATALINA_BASE/logs/catalina-daemon.out"
test ".$CATALINA_TMP" = . && CATALINA_TMP="$CATALINA_BASE/temp"

# Add tomcat-juli.jar to classpath
# tomcat-juli.jar can be over-ridden per instance
if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then
 CLASSPATH="$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar"
else
 CLASSPATH="$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar"
fi
# Set juli LogManager config file if it is present and an override has not been issued
if [ -z "$LOGGING_CONFIG" ]; then
 if [ -r "$CATALINA_BASE/conf/logging.properties" ]; then
 LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
 else
 # Bugzilla 45585
 LOGGING_CONFIG="-Dnop"
 fi
fi

test ".$LOGGING_MANAGER" = . && LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"

# Set -pidfile
test ".$CATALINA_PID" = . && CATALINA_PID="$CATALINA_BASE/logs/catalina-daemon.pid"

# Increase the maximum file descriptors if we can
if [ "$cygwin" = "false" ]; then
 MAX_FD_LIMIT=`ulimit -H -n`
 if [ "$?" -eq 0 ]; then
 # Darwin does not allow RLIMIT_INFINITY on file soft limit
 if [ "$darwin" = "true" -a "$MAX_FD_LIMIT" = "unlimited" ]; then
 MAX_FD_LIMIT=`/usr/sbin/sysctl -n kern.maxfilesperproc`
 fi
 test ".$MAX_FD" = ".maximum" && MAX_FD="$MAX_FD_LIMIT"
 ulimit -n $MAX_FD
 if [ "$?" -ne 0 ]; then
 echo "$PROGRAM: Could not set maximum file descriptor limit: $MAX_FD"
 fi
 else
 echo "$PROGRAM: Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
 fi
fi

# ----- Execute The Requested Command -----------------------------------------
case "$1" in
 run )
 shift
 "$JSVC" $* \
 $JSVC_OPTS \
 -java-home "$JAVA_HOME" \
 -pidfile "$CATALINA_PID" \
 -wait 10 \
 -nodetach \
 -outfile "&1" \
 -errfile "&2" \
 -classpath "$CLASSPATH" \
 "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
 -Dcatalina.base="$CATALINA_BASE" \
 -Dcatalina.home="$CATALINA_HOME" \
 -Djava.io.tmpdir="$CATALINA_TMP" \
 $CATALINA_MAIN
 exit $?
 ;;
 start )
 "$JSVC" $JSVC_OPTS \
 -java-home "$JAVA_HOME" \
 -user $TOMCAT_USER \
 -pidfile "$CATALINA_PID" \
 -wait 10 \
 -outfile "$CATALINA_OUT" \
 -errfile "&1" \
 -classpath "$CLASSPATH" \
 "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
 -Dcatalina.base="$CATALINA_BASE" \
 -Dcatalina.home="$CATALINA_HOME" \
 -Djava.io.tmpdir="$CATALINA_TMP" \
 $CATALINA_MAIN
 exit $?
 ;;
 stop )
 "$JSVC" $JSVC_OPTS \
 -stop \
 -pidfile "$CATALINA_PID" \
 -classpath "$CLASSPATH" \
 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
 -Dcatalina.base="$CATALINA_BASE" \
 -Dcatalina.home="$CATALINA_HOME" \
 -Djava.io.tmpdir="$CATALINA_TMP" \
 $CATALINA_MAIN
 exit $?
 ;;
 version )
 "$JSVC" \
 -java-home "$JAVA_HOME" \
 -pidfile "$CATALINA_PID" \
 -classpath "$CLASSPATH" \
 -errfile "&2" \
 -version \
 -check \
 $CATALINA_MAIN
 if [ "$?" = 0 ]; then
 "$JAVA_BIN" \
 -classpath "$CATALINA_HOME/lib/catalina.jar" \
 org.apache.catalina.util.ServerInfo
 fi
 exit $?
 ;;
 * )
 echo "Unknown command: \`$1'"
 echo "Usage: $PROGRAM ( commands ... )"
 echo "commands:"
 echo " run Start Tomcat without detaching from console"
 echo " start Start Tomcat"
 echo " stop Stop Tomcat"
 echo " version What version of commons daemon and Tomcat"
 echo " are you running?"
 exit 1
 ;;
esac

这第二种方式呢,就显得略简单一些,那就是大家耳熟能详的sudo了,问题的关键在于,sudo是需要一个合法的tty的,很显然,开机启动,是木有tty的!那么我们要怎么样才能达到这个目的呢?呵呵,我们只需要修改一处地方就可以了,那就是/etc/sudoers。

注释掉 Defaults    requiretty 就可以啦

然后嘛,随便写到rc.local或者是自己新建一个sh脚本都是不错的选择,例如:

vi /etc/rc.local
sudo -u www /home/www/tomcat8/bin/startup.sh &

这种方式的缺点呢是不能将服务器开启于80端口,优点呢在于普通用户的密码可以分发给开发人员,他们可以自己去启动,停止,不像第一种方法,启动和停止都需要使用root用户,这种方式比较适合代码尚未完全稳定,或者是经常在所谓迭代开发的生产环境。

Leave a Reply

Your email address will not be published. Required fields are marked *