阿汤博客-承接中小企业服务器维护和网站维护,有意者可以联系博主!

Centos7.x java和tomcat单机多实例部署普通用户启动

运维文档 36℃ 0评论

1、下载tomcat和jdk

#wget https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz

#wget https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz

#tar -zxf apache-tomcat-8.5.35.tar.gz 

#tar -zxf jdk-8u191-linux-x64.tar.gz

#cp -rf apache-tomcat-8.5.35  /usr/local/tomcat-8.5

#cp -rf  jdk1.8.0_191 /usr/local/java

2、设置环境变量

#vim /etc/profile

添加

# Java

export JAVA_HOME="/usr/local/java"

export JRE_HOME="$JAVA_HOME/jre"

export PATH="$PATH:$JAVA_HOME/bin"

# Tomcat根目录

export CATALINA_HOME="/usr/local/tomcat-8.5" 

export PATH=$CATALINA_HOME:$PATH

#source /etc/profile

3、单机多实例部署

a、创建实例目录文件

#cd /usr/local/tomcat-8.5/

#mkdir -pv tomcat_8078/logs

#mv conf  temp  webapps  worktomcat_8078/

#cp -rf tomcat_8078 tomcat_8077

#cp -rf tomcat_8078 tomcat_bak  #备份一个实例以便后续增加

b、修改端口

#sed -i 's/8080/8078/g' tomcat_8078/conf/server.xml

#sed -i 's/8080/8077/g' tomcat_8077/conf/server.xml

#sed -i 's/8005/8018/g' tomcat_8078/conf/server.xml

#sed -i 's/8005/8017/g' tomcat_8077/conf/server.xml

#sed -i 's/8009/8038/g' tomcat_8078/conf/server.xml

#sed -i 's/8009/8037/g' tomcat_8077/conf/server.xml

c、启动脚本,以普通用户启动

#cat start_tomcat.sh

#! /bin/bash

source /etc/profile

export CATALINA_BASE=$CATALINA_HOME/${1%/}

export CATALINA_TMPDIR=$CATALINA_BASE/temp

STARTUP=$CATALINA_HOME/bin/startup.sh

TOMCAT_PID=`ps -ef |grep tomcat |grep "[D]catalina.home=$CATALINA_HOME" |grep -v 'grep'|awk '{print $2}'`

if [ -n "$TOMCAT_ID" ] ; then

  echo "${1%/} instance is running, cannot start it again…"

  exit 2

else

 echo "Startup ${1%/} now…"

 su tomcat -s /bin/bash -c $STARTUP

fi

if [ "$?" = "0" ]; then

    echo "Instance ${1%/} start successfully"

else

    echo "Instance ${1%/} start failed"

fi

#停止脚本

#cat stop_tomcat.sh

#! /bin/bash

source /etc/profile

export CATALINA_BASE=$CATALINA_HOME/${1%/}

export CATALINA_TMPDIR=$CATALINA_BASE/temp

SHUTDOWN=$CATALINA_HOME/bin/shutdown.sh

echo "Prepare to shutdown ${1%/} instance…"

$SHUTDOWN

TOMCAT_PID=`ps -ef |grep tomcat |grep "[D]catalina.home=$CATALINA_HOME" |grep -v 'grep'|awk '{print $2}'`

if [ -n "$TOMCAT_ID" ] ; then

  echo "Start to kill ${1%/} instance(PID:$TOMCAT_PID)…"

  kill -9 $TOMCAT_PID 

  echo "Instance ${1%/} has been killed successfully!"

else 

  echo "Instance ${1%/} stop successfully!"

fi

#chmod +x start_tomcat.sh stop_tomcat.sh

d、创建普通用户

#useradd -M -r -s /sbin/nologin tomcat

#chown -R tomcat.tomcat $CATALINA_HOME

e、调整jvm参数,限制内存

#vim  $CATALINA_HOME/bin/catalina.sh  #在最前面添加参数

JAVA_OPTS="-Xms64m -Xmx512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m -Xss2m"

CATALINA_OPTS="-Djava.awt.headless=true"

JAVA_OPTS="$JAVA_OPTS -Ddruid.registerToSysProperty=true"

4、启动实例

#start_tomcat tomcat_8078

#start_tomcat tomcat_8077

java和tomcat单机多实例部署普通用户启动

转载请注明:阿汤博客 » Centos7.x java和tomcat单机多实例部署普通用户启动

喜欢 (0)or分享 (0)