CentOS系统下安装Tomcat7环境教程

作者:袖梨 2022-06-30

安装JDK

在Oracle Java SEDownload页面下载JDK。

(1) 勾选「Accept License Agreement」

(2) 点击「jdk-8u91-linux-x64.rpm」

系统为64位时选择x64,32位时选择i586。

(3) 把下载的文件用FTP上传到服务器。

(4) 安装JDK

# rpm -ivh ./jdk-8u91-linux-x64.rpm
安装Tomcat

在Apache Tomcat – Welcome!的Downloads页面下载Tomcat。

# wget http://ftp.kddilabs.jp/infosystems/apache/tomcat/tomcat-7/v7.0.70/bin/apache-tomcat-7.0.70.tar.gz
# tar zxvf ./apache-tomcat-7.0.70.tar.gz -C /usr/local/

(2) 添加tomcat用户

# useradd -m -d /home/tomcat tomcat
# chown -R tomcat:tomcat /usr/local/apache-tomcat-7.0.70/

(3) apache-tomcat-7.0.7目录太长,做个软链接到/usr/local/tomcat,并修改用户

# ln -s /usr/local/apache-tomcat-7.0.70 /usr/local/tomcat
# chown -h tomcat. /usr/local/tomcat

(4) 启动tomcat

# sudo -u tomcat /usr/local/tomcat/bin/startup.sh

(5) 输入http://IP地址:8080/

停止Tomcat时,执行如下命令。

# sudo -u tomcat /usr/local/tomcat/bin/shutdown.sh

创建Tomcat启动/停止脚本

(1) 创建/etc/init.d/tomcat文件。

 代码如下 复制代码

#!/bin/bash

#chkconfig: 2345 10 90
#description:tomcat service

# Source function library.
. /etc/init.d/functions

CATALINA_HOME="/usr/local/tomcat"
CATALINA_PID="/usr/local/tomcat/catalina.pid"
TOMCAT_USER="tomcat"
NAME="tomcat"
RETVAL=$?

case "$1" in
start)
  if [ -f "/var/lock/subsys/${NAME}" ]; then
    echo "Tomcat is already running"
    exit 1
  fi
  echo "Starting Tomcat"
  sudo -u ${TOMCAT_USER} ${CATALINA_HOME}/bin/catalina.sh start
  touch /var/lock/subsys/tomcat
;;
stop)
  if [ ! -f "/var/lock/subsys/${NAME}" ]; then
    echo "Tomcat is already stopping"
    exit 1
  fi
  echo "Stopping Tomcat"
  sudo -u ${TOMCAT_USER} ${CATALINA_HOME}/bin/catalina.sh stop -force
  rm -f /var/lock/subsys/tomcat
;;
restart)
  $0 stop
  sleep 10
  $0 start
;;
status)
  if [ -f "${CATALINA_PID}" ]; then
    read kpid < ${CATALINA_PID}
    if [ -d "/proc/${kpid}" ]; then
      echo "${NAME} (pid ${kpid}) is running..."
    else
      echo "${NAME} is not running, but PID file exists"
    fi
  else
    echo "${NAME} is stopped"
  fi
;;
*)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
;;
esac

exit ${RETVAL}


在这里不实用startup.sh和shutdown.sh,直接用catalina.sh进行tomcat启动及停止。

(2) 脚本赋予执行权限

# chmod +x /etc/init.d/tomcat

(3) 配置开机自动启动

# chkconfig --add tomcat
# chkconfig --list | grep tomcat
# chkconfig tomcat on

Apache和Tomcat

在Apache的80端口监听,然后用Apache ProxyPass启用Tomcat。

# vi /etc/httpd/conf/httpd.conf
ProxyPass /tomcat/ ajp://localhost:8009/
ProxyPass /examples/ ajp://localhost:8009/examples/

之后重启httpd就可以用80端口访问Tomcat。

相关文章

精彩推荐