centos6和7配置tomcat8和jdk8环境【普通用户jsvc】详细教程-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

centos6和7配置tomcat8和jdk8环境【普通用户jsvc】详细教程

买醉 2015-07-19 20:57:49 21272

一.准备工作软件
(1-1)WinSCP / PuTTY / notepad++【编辑文件用这个,别用win系统下的记事本】


(1-2)安装gcc
yum -y install gcc

(1-3)准备Tomcat和jdk安装包【都下载tar.gz包】
Tomcat 8 可以到http://tomcat.apache.org/download-80.cgi 下载
sun jdk 可以到http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载

(1-4)下面以 apache-tomcat-8.0.24.tar.g和jdk-8u51-linux-x64.tar.gz为列
下载好以后你可以用WinSCP把下载好的2个程序包上传目标机器上,当然也可以在你的目标机器上直接wget

我习惯传到/tmp目录下
[attachment=79581]

(1-5)如果要和其他软件整合,就跳过这步,如果直接使用就需要打开目标机器的防火墙
centos6
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart


centos7
systemctl enable firewalld.service
systemctl restart firewalld.service
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=22/tcp
firewall-cmd --reload
systemctl restart firewalld.service


(1-6)关闭SELINUX
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

(1-7)建立运行tomcat的普通用户tomcat【当然也可以建立其他的普通用户来运行,具体的看(3-2)和(3-3)】
/usr/sbin/groupadd tomcat
/usr/sbin/useradd -g tomcat tomcat -s /bin/false


(1-8)建立tomcat安装目录【我习惯吧tomcat安装到/usr/local/webserver目录下了】
mkdir -pv /usr/local/webserver

二.安装jdk
(2-1)
tar zxvf jdk-8u51-linux-x64.tar.gz -C /usr/local/
意思就是把jdk-8u51-linux-x64.tar.gz解压到/usr/local/目录下
[attachment=79582]



(2-2)在/etc/profile.d/目录下建立个jdk.sh的文件,文件内容为
JAVA_HOME=/usr/local/jdk1.8.0_51/
JAVA_BIN=/usr/local/jdk1.8.0_51/bin/
JRE_HOME=/usr/local/jdk1.8.0_51/jre/
JRE_BIN=/usr/local/jdk1.8.0_51/jre/bin/
CLASSPATH=/usr/local/jdk1.8.0_51/jre/lib:/usr/local/jdk1.8.0_51/lib:/usr/local/jdk1.8.0_51/jre/lib/charsets.jar
PATH=$PATH:$JAVA_BIN:$JRE_BIN
export JAVA_HOME JAVA_BIN JRE_HOME JRE_BIN PATH CLASSPATH


(2-3)给/etc/profile.d/jdk.sh文件运行权限
chmod +x /etc/profile.d/jdk.sh

(2-4)刷新下变量使jdk.sh里的内容生效
source /etc/profile.d/jdk.sh[attachment=79583]


(2-5)看下新装的jdk生效了没
java -version[attachment=79584]
看到以上内容就是安装成功了,以后升级也很简单,还是把新版本解压到/usr/local/目录下,删除/usr/local/下的老版本目录,修改/etc/profile.d/jdk.sh里的内容,把里面的路径改成新的路径,然后source /etc/profile.d/jdk.sh一下就行


三.安装tomcat
(3-1)安装tomcat的jsvc,来使用普通用户来启动tomcat,而不是直接运行catalina.sh或者startup.sh启动tomcat
cd /tmp
tar zxvf apache-tomcat-8.0.24.tar.gz -C /usr/local/webserver/
cd /usr/local/webserver/apache-tomcat-8.0.24/bin
tar zxvf commons-daemon-native.tar.gz
cd commons-daemon-1.0.15-native-src/unix
./configure --with-java=/usr/local/jdk1.8.0_51
make
cp jsvc ../..



(3-2)修改apache-tomcat-8.0.24目录的运行权限为tomcat
chown -R tomcat:tomcat /usr/local/webserver/apache-tomcat-8.0.24
[attachment=79585]

(3-3)在/etc/rc.d/init.d/目录下建立个名为tomcat的tomcat启动脚本文件,内容为:
#!/bin/sh

# chkconfig: 2345 10 90
# description: Starts and Stops the Tomcat daemon.

JAVA_HOME=/usr/local/jdk1.8.0_51
CATALINA_HOME=/usr/local/webserver/apache-tomcat-8.0.24
ARG0="$0"
while [ -h "$ARG0" ]; do
  ls=`ls -ld "$ARG0"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    ARG0="$link"
  else
    ARG0="`dirname $ARG0`/$link"
  fi
done
DIRNAME="`dirname $ARG0`"
PROGRAM="`basename $ARG0`"
for o
do
  case "$o" in
    --java-home )
        JAVA_HOME="$2"
        shift; shift;
        continue
    ;;
    --catalina-home )
        CATALINA_HOME="$2"
        shift; shift;
        continue
    ;;
    --catalina-base )
        CATALINA_BASE="$2"
        shift; shift;
        continue
    ;;
    --catalina-pid )
        CATALINA_PID="$2"
        shift; shift;
        continue
    ;;
    --tomcat-user )
        TOMCAT_USER="$2"
        shift; shift;
        continue
    ;;
    * )
        break
    ;;
  esac
done
cygwin=false;
darwin=false;
case "`uname`" in
    CYGWIN*)
        cygwin=true
        ;;
    Darwin*)
        darwin=true
        ;;
esac

test ".$MAX_FD" = . && MAX_FD="maximum"
test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat
JAVA_HOME=/usr/local/jdk1.8.0_51
if [ -z "$JAVA_HOME" ]; then
    JAVA_BIN="`which java 2>/dev/null || type java 2>&1`"
    test -x "$JAVA_BIN" && JAVA_HOME="`dirname $JAVA_BIN`"
    test ".$JAVA_HOME" != . && JAVA_HOME=`cd "$JAVA_HOME/.." >/dev/null; pwd`
else
    JAVA_BIN="$JAVA_HOME/bin/java"
fi
test ".$CATALINA_HOME" = . && CATALINA_HOME=`cd "$DIRNAME/.." >/dev/null; pwd`
test ".$CATALINA_BASE" = . && CATALINA_BASE="$CATALINA_HOME"
test ".$CATALINA_MAIN" = . && CATALINA_MAIN=org.apache.catalina.startup.Bootstrap
test ".$JSVC" = . && JSVC="$CATALINA_BASE/bin/jsvc"
CATALINA_HOME=/usr/local/webserver/apache-tomcat-8.0.24
JAVA_OPTS="-server -Xms512m -Xmx1024m -XX:MaxNewSize=256m"
if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
  . "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
  . "$CATALINA_HOME/bin/setenv.sh"
fi
test ".$CLASSPATH" != . && CLASSPATH="${CLASSPATH}:"
CLASSPATH="$CLASSPATH$CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar"
test ".$CATALINA_OUT" = . && CATALINA_OUT="$CATALINA_BASE/logs/catalina-daemon.out"
test ".$CATALINA_TMP" = . && CATALINA_TMP="$CATALINA_BASE/temp"
if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then
  CLASSPATH="$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar"
else
  CLASSPATH="$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar"
fi
if [ -z "$LOGGING_CONFIG" ]; then
  if [ -r "$CATALINA_BASE/conf/logging.properties" ]; then
    LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
  else
    LOGGING_CONFIG="-Dnop"
  fi
fi
test ".$LOGGING_MANAGER" = . && LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
test ".$CATALINA_PID" = . && CATALINA_PID="$CATALINA_BASE/logs/catalina-daemon.pid"
if [ "$cygwin" = "false" ]; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ "$?" -eq 0 ]; then
        if [ "$darwin" = "true" -a "$MAX_FD_LIMIT" = "unlimited" ]; then
            MAX_FD_LIMIT=`/usr/sbin/sysctl -n kern.maxfilesperproc`
        fi
        test ".$MAX_FD" = ".maximum" && MAX_FD="$MAX_FD_LIMIT"
        ulimit -n $MAX_FD
        if [ "$?" -ne 0 ]; then
            echo "$PROGRAM: Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        echo "$PROGRAM: Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi
case "$1" in
    run     )
      shift
      "$JSVC" $* \
      $JSVC_OPTS \
      -java-home "$JAVA_HOME" \
      -pidfile "$CATALINA_PID" \
      -wait 10 \
      -nodetach \
      -outfile "&1" \
      -errfile "&2" \
      -classpath "$CLASSPATH" \
      "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMP" \
      $CATALINA_MAIN
      exit $?
    ;;
    start   )
      "$JSVC" $JSVC_OPTS \
      -java-home "$JAVA_HOME" \
      -user $TOMCAT_USER \
      -pidfile "$CATALINA_PID" \
      -wait 10 \
      -outfile "$CATALINA_OUT" \
      -errfile "&1" \
      -classpath "$CLASSPATH" \
      "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMP" \
      $CATALINA_MAIN
      exit $?
    ;;
    stop    )
      "$JSVC" $JSVC_OPTS \
      -stop \
      -pidfile "$CATALINA_PID" \
      -classpath "$CLASSPATH" \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMP" \
      $CATALINA_MAIN
      exit $?
    ;;
    version  )
      "$JSVC" \
      -java-home "$JAVA_HOME" \
      -pidfile "$CATALINA_PID" \
      -classpath "$CLASSPATH" \
      -errfile "&2" \
      -version \
      -check \
      $CATALINA_MAIN
      if [ "$?" = 0 ]; then
        "$JAVA_BIN" \
        -classpath "$CATALINA_HOME/lib/catalina.jar" \
        org.apache.catalina.util.ServerInfo
      fi
      exit $?
    ;;
    *       )
      echo "Unkown command: \`$1'"
      echo "Usage: $PROGRAM ( commands ... )"
      echo "commands:"
      echo "  run               Start Catalina without detaching from console"
      echo "  start             Start Catalina"
      echo "  stop              Stop Catalina"
      echo "  version           What version of commons daemon and Tomcat"
      echo "                    are you running?"
      exit 1
    ;;
esac





[attachment=79586]


[attachment=79587]



(3-4)赋予启动文件执行权限
chmod +x /etc/rc.d/init.d/tomcat


(3-5)建立tomcat启动项
chkconfig --add tomcat
chkconfig tomcat on



(3-6)启动tomcat
service tomcat start

四.使用 netstat -lnp
命令看看tomcat端口占用情况,centos7系统需要先安装net-tools yum -y install net-tools
[attachment=79591]



五.在浏览器里输入http://ip地址:8080看看tomca默认欢迎页面
[attachment=79588]



六.上传一个jsp探针到/usr/local/webserver/apache-tomcat-8.0.24/webapps/ROOT目录下,并赋予tomcat用户和执行权限在浏览器里打开来看看效果
chmod +x /usr/local/webserver/apache-tomcat-8.0.24/webapps/ROOT/test.jsp
chown -R tomcat:tomcat /usr/local/webserver/apache-tomcat-8.0.24/webapps/ROOT/test.jsp
http://ip地址:8080/test.jsp
[attachment=79589]



至此centos6/7配置tomcat 8和jdk8 完成


热点 centos jsp(java)环境一键安装包(resin 4加jdk8教程)见: http://bbs.aliyun.com/read/250121.html

热点原创教程,转帖请注明来自http://blog.7qy.com 纯白色
centos普通用户 centos详细教程 centos配置tomcat 配置tomcatjdk jdk普通用户
分享到
取消 提交回答
全部回答(0)
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题
推荐课程