tomcat安装

8系列的最新版本:apache-tomcat-8.5.64.tar.gz

两台主机:
192.168.2.162
192.168.2.164

系统优化(root) (performance_tuning.sh)
设置最大打开文件数
1)系统级的设置
# vim /etc/sysctl.conf
fs.file-max = 262144
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.rmem_default = 1048576
net.core.wmem_default = 524288
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 2500
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 30
net.nf_conntrack_max = 655360
net.netfilter.nf_conntrack_tcp_timeout_established = 1200

sysctl -p

用户级设置
# vim /etc/security/limits.conf
*    soft    nofile     65535
*    hard    nofile     65535

3)要使 limits.conf 文件配置生效,必须要确保 pam_limits.so 文件被加入到启动文件中。
# vim /etc/pam.d/login
32位系统在最后一行添加:
session required /lib/security/pam_limits.so

64位系统在最后一行添加:
session required /lib64/security/pam_limits.so


# df -h 查看系统最大分区,安装在此分区最佳

安装jdk,若已安装则忽略该步骤
# 创建dzfp用户, dzfp组
# useradd dzfp
# tar -zxv -f apache-tomcat-8.5.64.tar.gz -C /opt/server/
# cd /opt/server
# mv apache-tomcat-8.5.64/ tomcat
# chmod 755 -R /opt/server/tomcat
# chown -R dzfp:dzfp /opt/server/tomcat/
# cd /opt/server/tomcat/webapps/
# rm -rf docs examples manager host-manager    
jvm 修改 (注意红色部分每个环境路径不同)
catalina.sh 文件里 JAVA_OPTS最终修改如下(如果没有,则添加下面内容在文件第一行):
# vim tomcat/bin/catalina.sh
JAVA_OPTS="${JAVA_OPTS} -Duser.timezone=Asia/Shanghai -Dfile.encoding=utf-8 -server -XX:PermSize=512m -XX:MaxPermSize=512m -Xms4096m -Xmx4096m -Djava.awt.headless=true"
CATALINA_HOME=/opt/server/tomcat
JAVA_HOME=/opt/server/jdk1.8.0_231

线程数优化
# vim tomcat/conf/server.xml
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
URIEncoding="UTF-8" maxThreads="1000" minSpareThreads="200" maxSpareThreads="500"
    />
App路径修改,# 这个路径保持默认的,以下这三步就不用操作了
# vim tomcat/conf/server.xml
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true" reloadable="true">

# mkdir /opt/var/app
# chmod 755 -R /opt/var/app
# chown -R dzfp:dzfp /opt/server/tomcat /opt/var/app
设置开机自启动
# vim /etc/init.d/tomcat

#!/bin/sh
# tomcat: Start/Stop/Restart tomcat
# chkconfig: 2345 40 20
# description: Tomcat is a Java Servlet Container
# match these values to your environment:
TOMCAT_HOME=/opt/server/tomcat
TOMCAT_USER=dzfp
NOW_USER=$(whoami)
. /etc/init.d/functions TOMCAT=${TOMCAT_HOME}

start() {
	echo -n "Starting Tomcat:"
	if [ "$NOW_USER" == "dzfp" ];then
		${TOMCAT_HOME}/bin/startup.sh
	else
		su  - ${TOMCAT_USER}  -c  "${TOMCAT_HOME}/bin/startup.sh"
	fi
   }

stop() {
	echo -n "Stopping Tomcat:"
	if [ "$NOW_USER" == "dzfp" ];then
		${TOMCAT_HOME}/bin/shutdown.sh
	else
		su  - ${TOMCAT_USER} -c "${TOMCAT_HOME}/bin/shutdown.sh"
	fi
# clear tomcat cache
    rm -rf $TOMCAT_HOME/work/Catalina
   }
# See how we were called.
case "$1" in
  start)
      start
  ;;
    stop)
        stop
    ;;
 status)
    ;;
    restart)
       stop
sleep 2
       start
    ;;
 *)
echo "Usage: $0 {start stop restart}" ;;
esac
exit $RETVAL

# chmod +x /etc/init.d/tomcat
chown -R dzfp:dzfp /etc/init.d/tomcat
##### chown -R dzfp:dzfp /opt/server/tomcat /opt/var/app /etc/init.d/tomcat (这一步酌情使用)
# chkconfig --add tomcat
# chkconfig tomcat on
# chkconfig tomcat --list
# 启动tomcat
# /etc/init.d/tomcat start

设置tomcat日志目录
# sed -i 's#${catalina.base}/logs#/opt/var/logs/tomcat#g' /opt/server/tomcat/conf/logging.properties
# mkdir -p /opt/var/logs/tomcat

或者直接通过软连接
# mkdir -p /opt/var/logs
# ln -s /opt/server/tomcat/logs /opt/var/logs/tomcat

日志切分



# Tomcat日志配置
用root用户,安装cronolog
下载地址:http://rpmfind.net/linux/RPM/epel/7/x86_64/Packages/c/cronolog-1.6.2-14.el7.x86_64.html
wget http://rpmfind.net/linux/epel/7/x86_64/Packages/c/cronolog-1.6.2-14.el7.x86_64.rpm

# 上传cronolog-1.6.2-10.el6.x86_64.rpm 到 /opt/server
# rpm -ivh cronolog-1.6.2-10.el6.x86_64.rpm
# 用which命令查看路径验证安装
# which cronolog
/usr/sbin/cronolog

# 使用dzfp用户修改tomcat脚本
# cd /opt/server
# vim tomcat/bin/catalina.sh  (根据括号内容修改)
#  touch "$CATALINA_OUT"
  if [ "$1" = "-security" ] ; then
    if [ $have_tty -eq 1 ]; then
      echo "Using Security Manager"
    fi
    shift
    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Djava.security.manager \
      -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start  2>&1 \ #(添加这行)
      | /usr/sbin/cronolog $CATALINA_BASE/logs/catalina.%Y-%m-%d.out >> /dev/null & #(添加这行)
#      org.apache.catalina.startup.Bootstrap "$@" start \ #(注释这行)
#      >> "$CATALINA_OUT" 2>&1 "&" #(注释这行)

  else
    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start  2>&1 \ #(添加这行)
      | /usr/sbin/cronolog $CATALINA_BASE/logs/catalina.%Y-%m-%d.out >> /dev/null & #(添加这行)
#      org.apache.catalina.startup.Bootstrap "$@" start \ #(注释这行)
#      >> "$CATALINA_OUT" 2>&1 "&" #(注释这行)

清理日志脚本
每天早上1点清理tomcat日志
# mkdir /opt/scripts
# chmod 755 -R /opt/scripts
# vim /opt/scripts/clear_tomcat_logs.sh
#!/bin/bash
# clear tomcat logs
KEEP_TIME=30
DOMAIN_HOME="/opt/var/logs/tomcat/" # 存放tomcat日志的路径
/usr/bin/find $DOMAIN_HOME -mtime +$KEEP_TIME -exec rm -rf {} \; >>/tmp/clean_tomcat_logs.log
exit 0
# chmod +x /opt/scripts/clear_tomcat_logs.sh
# crontab -e
0 1 * * * sh /opt/scripts/clear_tomcat_logs.sh

# chown -R dzfp.dzfp /opt/* (这一步没必要操作)

posted @ 2021-04-08 13:26  哈喽哈喽111111  阅读(68)  评论(0编辑  收藏  举报