生产场景tomcat多实例标准安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生产场景tomcat多实例标准安装相关的知识,希望对你有一定的参考价值。

#####tomcat多实例安装#####

1、安装java jdk1.8.0_172

############################生产场景一键快速安装java jdk ####################################

mkdir /usr/java

cd /usr/local/src/

rz -y           上传jdk-8u172-linux-x64.tar.gz,jdk版本为1.8.0.172

tar xf jdk-8u172-linux-x64.tar.gz -C /usr/java

ln -s /usr/java/jdk1.8.0_172/ /usr/java/jdk

 

vim /etc/profile     #添加如下4行内容

#set java environment

export JAVA_HOME=/usr/java/jdk

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

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

一键命令操作如下:

sed -i.ori '$a #set java environment export export JAVA_HOME=/usr/java/jdk export export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' /etc/profile

 

source /etc/profile

java -version         

出现如下3行说明java jdk安装完成

java version "1.8.0_172"

Java(TM) SE Runtime Environment (build 1.8.0_172-b11)

Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

################################一键快速安装java jdk 完成#####################################


2、安装tomcat多实例

mkdir /server/tools -p

mkdir /application/

cd /server/tools

rz -y  #上传apache-tomcat-8.5.31.tar.gz

tar xf apache-tomcat-8.5.31.tar.gz -C /application/

cp -a /application/apache-tomcat-8.5.31 /application/tomcat-sbcd

cp -a /application/apache-tomcat-8.5.31 /application/tomcat-sbcd-h5


2.1、tomcat实例1:tomcat-sbce安装及配置

1)修改server.xml配置文件

vim /application/tomcat-sbce/conf/server.xml

修改1:

<Server port="8005" shutdown="SHUTDOWN">修改为:

<Server port="58006" shutdown="SHUTDOWN">

修改2:tomcat http端口默认8080,需要修改

    <Connector port="8080" protocol="HTTP/1.1"修改为如下:

    <Connector port="58080" protocol="HTTP/1.1"

修改3:8009是和apache httpd连接用的,因为前端用nginx,所以这里要把这个注释掉。

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />这段注释掉,修改为如下:

<!--

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

-->

修改4:

      <Host name="localhost"  appBase="webapps" 修改为如下:

      <Host name="localhost"  appBase="/data/webapps-sbce"


2)创建webapps目录和测试文件

mkdir /data/webapps-sbce/ROOT -p

echo tomcat is ok >/data/webapps-sbce/ROOT/index.jsp

[[email protected] logs]# cat /data/webapps-sbce/ROOT/index.jsp

tomcat is ok


3)启动tomcat

useradd sysadmin

chown -R sysadmin.sysadmin /application/tomcat-sbce

chown -R sysadmin.sysadmin /data/webapps-sbce

chmod +x /etc/init.d/tomcat-sbce

[[email protected] logs]# /etc/init.d/tomcat-sbce start

Starting tomcat

Tomcat is Started                                          [  OK  ]

[[email protected] logs]# netstat -lntup|grep java

tcp        0      0 :::58080                    :::*                        LISTEN      938/java            

tcp        0      0 ::ffff:127.0.0.1:58005      :::*                        LISTEN      938/java            

[[email protected] logs]# ps -ef|grep java


4)验证tomcat启动是否ok

浏览器登录

http://10.68.8.53:58080/

可以看到tomcat is ok即可

然后把测试的主页删除,

rm -f /data/webapps-sbce/ROOT/index.jsp

后面网站代码就放到/data/webapps-sbce/ROOT/目录即可


2.2、tomcat实例2:tomcat-sbce-h5安装及配置

1)修改server.xml配置文件

vim /application/tomcat-sbce-h5/conf/server.xml

修改1:

<Server port="8005" shutdown="SHUTDOWN">修改为:

<Server port="58006" shutdown="SHUTDOWN">

修改2:

    <Connector port="8080" protocol="HTTP/1.1"修改为如下:

    <Connector port="58081" protocol="HTTP/1.1"

修改3:8009是和apache httpd连接用的,因为前端用nginx,所以这里要把这个注释掉。

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />这段注释掉,修改为如下:

<!--

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

-->

修改4:

      <Host name="localhost"  appBase="webapps" 修改为如下:

      <Host name="localhost"  appBase="/data/webapps-sbce-h5"


2)创建webapps目录和测试文件

mkdir /data/webapps-sbce-h5/ROOT -p

echo tomcat is ok >/data/webapps-sbce-h5/ROOT/index.jsp

[[email protected] logs]# cat /data/webapps-sbce-h5/ROOT/index.jsp

tomcat is ok


3)启动tomcat

useradd sysadmin

chown -R sysadmin.sysadmin /application/tomcat-sbce-h5

chown -R sysadmin.sysadmin /data/webapps-sbce-h5

[[email protected] logs]# /etc/init.d/tomcat-sbce-h5 start

Starting tomcat

Tomcat is Started                                          [  OK  ]

[[email protected] logs]# netstat -lntup|grep java

tcp        0      0 :::58080                    :::*                        LISTEN      938/java            

tcp        0      0 :::58081                    :::*                        LISTEN      1050/java           

tcp        0      0 ::ffff:127.0.0.1:58005      :::*                        LISTEN      938/java            

tcp        0      0 ::ffff:127.0.0.1:58006      :::*                        LISTEN      1050/java 

[[email protected] logs]# ps -ef|grep java


4)验证tomcat启动是否ok

浏览器登录

http://10.68.8.53:58081/

可以看到tomcat is ok即可。

然后把测试的主页删除,

rm -f /data/webapps-sbce-h5/ROOT/index.jsp

后面网站代码就放到/data/webapps-sbce-h5/ROOT/目录即可


注意事项:

server.xml中<Host name="localhost"  appBase="/data/webapps-sbce-h5"

/data/webapps-sbce-h5/ROOT/index.jsp 此路径放网站代码


tomcat-sbce脚本如下:

export JAVA_HOME、export PATH、CATALINA_HOME、TOMCAT_USER

其中CATALINA_HOME=/application/tomcat-sbce/目录后面一定要带/否则,tomcat_pid匹配的时候不精确,比如有两个实例tomcat-sbce和tomcat-sbce-h5,要确定每个pid进程的时候,使用ps -ef|grep /application/tomcat-sbce可以查看两个进程,不精确,所以要带完整路径ps -ef|grep /application/tomcat-sbce/才精确。

变化的只有这4个变量。

[[email protected] logs]# cat /etc/init.d/tomcat-sbce

#!/bin/bash

### BEGIN INIT INFO

# Provides:          tomcat

# Required-Start:    $remote_fs $syslog

# Required-Stop:     $remote_fs $syslog

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: tomcat-server daemon

# Description:       tomcat-server daemon

### END INIT INFO

#

# chkconfig: - 95 15

# description: Tomcat start/stop/status script


#Location of JAVA_HOME (bin files)

export JAVA_HOME=/usr/java/jdk


#Add Java binary files to PATH

export PATH=$JAVA_HOME/bin:$PATH


#CATALINA_HOME is the location of the configuration files of this instance of Tomcat

#CATALINA_HOME must be followed by /,Because you can use ps-ef |grep to match exactly

CATALINA_HOME=/application/tomcat-sbce/


#TOMCAT_USER is the default user of tomcat

TOMCAT_USER=sysadmin


#TOMCAT_USAGE is the message if this script is called without any options

TOMCAT_USAGE="Usage: $0 {e[00;32mstarte[00m|e[00;31mstope[00m|e[00;32mstatuse[00m|e[00;31mrestarte[00m}"


#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop

SHUTDOWN_WAIT=5

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

actions(){

    RETVAL=$?

    if [ $RETVAL -eq 0 ];then

      action "Tomcat is $1" /bin/true

    else

      action "Tomcat is $1" /bin/false

    fi

}


tomcat_pid() {

  echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2`

}


start() {

  pid=$(tomcat_pid)

  if [ -n "$pid" ]; then

    echo -e "e[00;31mTomcat is already running (pid: $pid)e[00m"

  else

    if [ "$USER" = "root" ]; then

      echo -e "e[00;32mStarting tomcate[00m"

      su - $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh >/dev/null 2>&1 &

      actions Started

    elif [ "$USER" = "$TOMCAT_USER" ];then

      echo -e "e[00;32mStarting tomcate[00m"

      $CATALINA_HOME/bin/startup.sh >/dev/null 2>&1 &

      actions Started

    else

      echo -e "User must be root or $TOMCAT_USER"

      exit 2

    fi

  fi

  return 0

}


status() {

  pid=$(tomcat_pid)

  if [ -n "$pid" ]; then

    echo -e "e[00;32mTomcat is running with pid: $pide[00m"

  else

    echo -e "e[00;31mTomcat is not runninge[00m"

  fi

}


stop() {

  pid=$(tomcat_pid)

  if [ -n "$pid" ]; then

    [ "$USER" = "root" ] && {

      echo -e "e[00;31mStoping Tomcate[00m"

      su - $TOMCAT_USER -c $CATALINA_HOME/bin/shutdown.sh >/dev/null 2>&1 &

    }

    [ "$USER" = "$TOMCAT_USER" ] && {

      echo -e "e[00;31mStoping Tomcate[00m"

      $CATALINA_HOME/bin/shutdown.sh >/dev/null 2>&1 &

    }

    [ "$USER" != "root" -a "$USER" != "$TOMCAT_USER" ] && {

      echo -e "User must be root or $TOMCAT_USER"

      exit 2

    }

    let kwait=$SHUTDOWN_WAIT

    count=0;

    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]

    do

      echo -n -e "e[00;31mwaiting for processes to exite[00m ";

      action stopped

      sleep 1

      let count=$count+1;

    done


    if [ $count -gt $kwait ]; then

      echo -n -e " e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT secondse[00m"

      kill -9 $pid

    fi

  else

    echo -e "e[00;31mTomcat is not runninge[00m"

  fi


  return 0

}


case $1 in

  start)

    start

    ;;

  stop)

    stop

    ;;

  restart)

    stop

    start

    ;;

  status)

    status

    ;;

  *)

    echo -e $TOMCAT_USAGE

    ;;

esac

exit 0


以上是关于生产场景tomcat多实例标准安装的主要内容,如果未能解决你的问题,请参考以下文章

CentOS6.X 安装多实例的Tomcat

多实例tomcat

tomcat单实例多应用多实例多应用部署

tomcat配置管理:基于多实例的tomcat配置管理

Tomcat多实例单应用部署方案 (转)

mysql多实例部署