如下是定製腳本
下載tomcat 7 並解壓縮到/opt下,java
wget http://mirrors.noc.im/apache/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz -O - | tar -xzf - -C /opt mv /opt/apache-tomcat-7.0.69 /opt/tomcat7 yum install tomcat-native
下面將tomcat的server.xml,catalina.out,pid分別獨立起來。web
新建:apache
vim /opt/tomcat7/tomcat.shvim
#!/bin/sh # config: /opt/tomcat7/conf/server-xxxx.conf # CATALINA_BASE: /opt/tomcat7 # CATALINA_OUT: /home/logs/tomcat/catalina-xxx.out # server.xml /opt/tomcat7/conf/server-xxx.xml #if [ `whoami` = "root" ];then # echo "You're root,please run as Non-Root user." # exit 1 #fi #項目名稱 PROJECT=$2 #java相關 export JAVA_HOME=/opt/jdk7 export JRE_HOME=$JAVA_HOME/jre export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH export JAVA_OPTS="-Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -server -Xms2g -Xmx2g -Xss256K -XX:NewSize=1g -XX:MaxNewSize=1g -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -Djava.security.egd=file:/dev/./urandom" #tomcat相關 export CATALINA_BASE=/opt/tomcat7 export CATALINA_HOME=/opt/tomcat7 export CATALINA_OUT=/home/logs/tomcat/catalina-$PROJECT.out export CATALINA_PID="/home/logs/PID/catalina-$PROJECT.pid" CONFIGFILE=$CATALINA_HOME/conf/server-$PROJECT.xml export CUSTOM_OPTS="-config $CONFIGFILE" if [[ $1 != "-server" ]]; then echo "+Usage: tomcat.sh -server PROJECTNAME {start|stop|restart|status}+"; exit 1 elif [ -z $2 ]; then echo "+Usage: tomcat.sh -server PROJECTNAME {start|stop|restart|status}+"; exit 1 elif [ -z $3 ]; then echo "+Usage: tomcat.sh -server PROJECTNAME {start|stop|restart|status}+"; exit 1 fi if [ ! -f $CONFIGFILE ] ; then echo "PROJECT_CONFIG $CONFIGFILE is not exist,please check."; exit 1 fi start(){ $CATALINA_HOME/bin/startup.sh $CUSTOM_OPTS } stop() { $CATALINA_HOME/bin/shutdown.sh -force $CUSTOM_OPTS } status() { if [ -f $CATALINA_PID ] && ps --no-heading -p `cat $CATALINA_PID`; then echo "$PROJECT server is running" else echo "$PROJECT server is not running" fi } case "$3" in 'start') start ;; 'stop') stop ;; 'restart') stop start ;; 'status') status ;; *) echo "Usage: $0 -server PROJECT {start|stop|restart|status}" exit 1 esac exit 0
新建項目projectA,須要初始化以下tomcat
mkdir /home/logs/tomcat -p mkdir /home/logs/PID -p cp /opt/tomcat7/conf/server.xml /opt/tomcat7/conf/server-projectA.xml vim /opt/tomcat7/conf/server-projectA.xml
server-projectA.xml的內容基本以下bash
<?xml version='1.0' encoding='utf-8'?> <Server port="8183" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="4"/> <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="false"> <!--改成你的路徑--> <Context docBase="/home/app/projectA-source" path="" reloadable="false"/> </Host> </Engine> </Service> </Server>
useradd -M projectA -s /sbin/nologin #添加用戶,一個用戶對應一個單獨項目,禁止loginapp
啓動項目ProjectAless
sudo -u projectA /opt/tomcat7/bin/tomcat.sh -server projectA startdom
中止webapp
sudo -u projectA /opt/tomcat7/bin/tomcat.sh -server projectA stop
重啓
sudo -u projectA /opt/tomcat7/bin/tomcat.sh -server projectA restart