tomcat安装部署

一 Linux基础优化

设置最大打开文件数

1)系统级的设置

# vi /etc/sysctl.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 = 120

# 使其立即生效(centos 6系列可能报错,不用管)

#sysctl -p

2)用户级设置

# sudo vi /etc/security/limits.conf

1
2
3
4
*           soft  nofile     65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

3)要使 limits.conf 文件配置生效,必须要确保 pam_limits.so 文件被加入到启动文件中。

# vi /etc/pam.d/login

在最后一行添加:

1
session required /lib64/security/pam_limits.so

二 JVM配置

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

# 上传jdk-7u79-linux-x64.gz到服务器

# mkdir /opt/server

# tar zvxf jdk-7u79-linux-x64.gz -C /opt/server

# chmod 755 /opt -R

修改 /etc/profile

# vi /etc/profile

——————在文件末尾添加以下内容——————

1
2
3
4
5
JAVA_HOME=/opt/server/jdk1.7.0_79(Java7)
JAVA_HOME=/opt/server/jdk1.8.0_181(Java8)
CLASSPATH=$JAVA_HOME/jre/lib/rt.jar:.
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH CLASSPATH

——————结束————————————————

# source /etc/profile

# 检查java版本:

1
2
3
4
# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

三 Tomcat解压安装

# tar zxvf apache-tomcat-8.0.53.tar.gz -C /opt/server/

# chmod 755 -R /opt/

# cd /opt/server

# mv apache-tomcat-8.0.53 tomcat

# cd /opt/server/tomcat/webapps/

# rm -rf docs examples manager host-manager

四 日志切分

# Tomcat日志配置

用root用户,安装cronolog

# 上传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

# 修改tomcat脚本

# cd /opt/server

# vim tomcat/bin/catalina.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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 "&"

五 日志/参数乱码/app路径

1)jvm 修改 (注意每个环境路径不同)

catalina.sh 文件里 JAVA_OPTS最终修改如下(如果没有,则添加下面内容在文件第一行):

#vim tomcat/bin/catalina.sh

1
2
3
4
5
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.7.0_79

2)线程数修改

# vim tomcat/conf/server.xml

1
2
3
4
5
6
7
8
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"
maxThreads="1000"
minSpareThreads="200"
maxSpareThreads="500"
/>

3)App路径修改

# vim tomcat/conf/server.xml

1
2
<Host name="localhost" appBase="/opt/apps"
unpackWARs="true" autoDeploy="true" reloadable="true">

# mkdir /opt/apps

# chmod 755 -R /opt/apps

六 清理日志脚本

每天早上1点清理tomcat日志

# mkdir /opt/server/scripts

# chmod 755 -R /opt/server/scripts

# vim /opt/server/scripts/clear_tomcat_logs.sh

1
2
3
4
5
6
#!/bin/bash
# clear tomcat logs
KEEP_TIME=30
DOMAIN_HOME="/opt/server/tomcat"
/usr/bin/find $DOMAIN_HOME/logs -mtime +$KEEP_TIME -exec rm -rf {} \; >>/tmp/clean_tomcat_logs.log
exit 0

# chmod +x /opt/server/scripts/clear_tomcat_logs.sh

# crontab -e

0 1 * * * sh /opt/server/scripts/clear_tomcat_logs.sh

七 tomcat系统服务脚本

# touch /etc/init.d/tomcat

# vim /etc/init.d/tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/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-59001
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

设置开机自启

# chkconfig tomcat --add

至此,tomcat部署完成!