工作中很多软件和工具需要依赖java环境,JDK作为java的开发环境,是经常需要安装的。

下载JDK二进制包

官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 选择需要的jdk版本,下载就好,这里下载jdk-8u144-linux-x64.tar.gz

linux系统下载jdk技巧

centos下直接用wget下载会报错,需要加如下参数:

wget --no-cookie --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz &

解压到指定目录

wget --no-cookie --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz
tar zxvf jdk-8u144-linux-x64.tar.gz -C /usr/local/
ln -s /usr/local/jdk-8u144-linux-x64 /usr/local/java

设置java环境变量

export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

验证

[root@mysql-amoeba ~]# source /etc/profile
[root@mysql-amoeba ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

tomcat安装

tomcat下载链接https://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz

解压安装

tar zxvf apache-tomcat-8.5.20.tar.gz -C /usr/local/
ln -sv /usr/local/apache-tomcat-8.5.20/ /usr/local/tomcat

基本命令

  • 启动

/usr/local/tomcat/bin/startup.sh/usr/local/tomcat/bin/catalina.sh run

  • 关闭

/usr/local/tomcat/bin/shutdown.sh/usr/local/tomcat/bin/catalina.sh stop

日志切割

tomcat的日志文件目录为/usr/local/tomcat/logs,其中有个catalina.out的日志文件会随着运行时间变长而变大,当到达一定大小时,将无法写入更多的日志内容,使tomcat无法处理请求。所以需要对此日志文件进行切割。

  • 安装cronolog

yum install -y cronolog

  • 修改/usr/local/tomcat/bin/catalina.sh文件

有两处需要修改,都是修改catalina.out日志文件的输出格式。

if [ "$1" = "-security" ] ; then
    if [ $have_tty -eq 1 ]; then
      echo "Using Security Manager"
    fi
    shift
    eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -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 \
      >> "$CATALINA_OUT" 2>&1 "&"               #删除
      2>&1 |/usr/sbin/cronolog "$CATALINA_BASE"/logs/catalina-%Y-%m-%d.out &    #新增

  else
    eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -classpath "\"$CLASSPATH\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_OUT" 2>&1 "&"           #删除
      2>&1 |/usr/sbin/cronolog "$CATALINA_BASE"/logs/catalina-%Y-%m-%d.out &    #新增

  fi
  • 重启服务

    /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh

  • 效果
[root@izuf65fejv9adycyget0nvz logs]# ll -h cata*.out
-rw-r----- 1 root root 5.7M 1月   8 23:55 catalina-2018-01-08.out
-rw-r----- 1 root root 5.0M 1月   9 23:55 catalina-2018-01-09.out
-rw-r----- 1 root root 113K 1月  10 09:10 catalina-2018-01-10.out
-rw-r----- 1 root root 104M 1月   9 19:39 catalina.out