linux学习-安装tomcat以及自启动

出自:https://blog.csdn.net/GY325416/article/details/79327766

https://blog.csdn.net/u012351661/article/details/76254268

https://blog.csdn.net/beta_xiyan/article/details/76638949

https://blog.csdn.net/john_hongming/article/details/16941039

安装tomcat比较简单,解压出来就可以用了

一  上传源码到linux

我上传到了/usr/local/tomcat目录下面

这个因人而异,不影响的

二  解压tomcat

解压到当前目录

[plain]  view plain  copy
  1. tar zxvf apache-tomcat-8.5.16.tar.gz  

解压到其他目录

[plain]  view plain  copy
  1. tar zxvf apache-tomcat-8.5.16.tar.gz -C /tomcathome  

三  启动,停止tomcat

启动tomcat

[html]  view plain  copy
  1. 进入tomcat的bin目录,执行  
  2. ./startup.sh  

停止tomcat

[plain]  view plain  copy
  1. 进入tomcat的bin目录,执行  
  2. ./shutdown.sh  

四  开启8080端口

[plain]  view plain  copy
  1. firewall-cmd --zone=public --add-port=8080/tcp --permanent    永久开启8080端口  
  2. firewall-cmd --reload   重新载入  

现在在浏览器上就可以访问了

设置自启动:

假设Tomcat的安装路径为/opt/tomcat

1 为Tomcat添加启动参数

catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此在/opt/tomcat/bin路径下创建setenv.sh文件,内容如下:

export CATALINA_HOME=/opt/tomcat
export CATALINA_BASE=/opt/tomcat
#设置Tomcat的PID文件
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#添加JVM选项
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"

2 编写tomcat.service文件

在/usr/lib/systemd/system路径下添加tomcat.service文件,内容如下:

[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/tomcat/tomcat.pid
ExecStart=/opt/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

##[unit]配置了服务的描述,规定了在network启动之后执行,
##[service]配置服务的pid,服务的启动,停止,重启
##[install]配置了使用用户

3 将Tomcat加入服务管理

systemctl enable tomcat.service
systemctl disable tomcat.service
systemctl start tomcat.service
systemctl stop tomcat.service

systemctl restart tomcat.service

中间可能遇到问题:

在linux下安装好tomcat启动时报如下错误:

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 
At least one of these environment variable is needed to run this program

原因:找不到jdk或者jre路径。

解决办法:在setclasspath.bat的开头声明环境变量,打开tomcat的bin目录下面的setclasspath.sh,添加红色部分,路径修改为自己机器jdk和jre路径即可。

#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -----------------------------------------------------------------------------
#  Set CLASSPATH and Java options
#
#  $Id: setclasspath.sh 795037 2009-07-17 10:52:16Z markt $
# -----------------------------------------------------------------------------

export JAVA_HOME=/usr/local/jdk1.6.0_31
export JRE_HOME=/usr/local/jdk1.6.0_31/jre 


# Make sure prerequisite environment variables are set
if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
  # Bugzilla 37284 (reviewed).
  if $darwin; then
    if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
      export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
    fi
  else
    JAVA_PATH=`which java 2>/dev/null`
    if [ "x$JAVA_PATH" != "x" ]; then
      JAVA_PATH=`dirname $JAVA_PATH 2>/dev/null`
      JRE_HOME=`dirname $JAVA_PATH 2>/dev/null`
    fi
    if [ "x$JRE_HOME" = "x" ]; then
      # XXX: Should we try other locations?
      if [ -x /usr/bin/java ]; then
        JRE_HOME=/usr
      fi
    fi
  fi
  if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
    echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
    echo "At least one of these environment variable is needed to run this program"
    exit 1
  fi
fi
if [ -z "$JAVA_HOME" -a "$1" = "debug" ]; then
  echo "JAVA_HOME should point to a JDK in order to run in debug mode."
  exit 1
fi
if [ -z "$JRE_HOME" ]; then
  JRE_HOME="$JAVA_HOME"
fi

# If we're running under jdb, we need a full jdk.
if [ "$1" = "debug" ] ; then
  if [ "$os400" = "true" ]; then
    if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/javac ]; then
      echo "The JAVA_HOME environment variable is not defined correctly"
      echo "This environment variable is needed to run this program"
      echo "NB: JAVA_HOME should point to a JDK not a JRE"
      exit 1
    fi
  else
    if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/jdb -o ! -x "$JAVA_HOME"/bin/javac ]; then
      echo "The JAVA_HOME environment variable is not defined correctly"
      echo "This environment variable is needed to run this program"
      echo "NB: JAVA_HOME should point to a JDK not a JRE"
      exit 1
    fi
  fi
fi
if [ -z "$BASEDIR" ]; then
  echo "The BASEDIR environment variable is not defined"
  echo "This environment variable is needed to run this program"
  exit 1
fi
if [ ! -x "$BASEDIR"/bin/setclasspath.sh ]; then
  if $os400; then
    # -x will Only work on the os400 if the files are:
    # 1. owned by the user
    # 2. owned by the PRIMARY group of the user
    # this will not work if the user belongs in secondary groups
    eval
  else
    echo "The BASEDIR environment variable is not defined correctly"
    echo "This environment variable is needed to run this program"
    exit 1
  fi
fi

# Don't override the endorsed dir if the user has set it previously
if [ -z "$JAVA_ENDORSED_DIRS" ]; then
  # Set the default -Djava.endorsed.dirs argument
  JAVA_ENDORSED_DIRS="$BASEDIR"/endorsed
fi

# OSX hack to CLASSPATH
JIKESPATH=
if [ `uname -s` = "Darwin" ]; then
  OSXHACK="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Classes"
  if [ -d "$OSXHACK" ]; then
    for i in "$OSXHACK"/*.jar; do
      JIKESPATH="$JIKESPATH":"$i"
    done
  fi
fi

# Set standard commands for invoking Java.
_RUNJAVA="$JRE_HOME"/bin/java
if [ "$os400" != "true" ]; then
  _RUNJDB="$JAVA_HOME"/bin/jdb
fi

这里解决的问题是,在你本机通过 wget yourIP:8080/index.jsp 能够下载那个页面,但是外部机器不能通过IP地址进行访问,也就仅仅是因为防火墙的缘故 导致的。

主要原因在于防火墙的存在,导致的端口无法访问。
CentOS7使用firewall而不是iptables。所以解决这类问题可以通过添加firewall的端口,使其对我们需要用的端口开放。

1.使用命令  firewall-cmd --state查看防火墙状态。得到结果是running或者not running  

2.在running 状态下,向firewall 添加需要开放的端口
命令为 firewall-cmd --permanent --zone=public --add-port=8080/tcp //永久的添加该端口。去掉--permanent则表示临时。

4.firewall-cmd --reload //加载配置,使得修改有效。

5.使用命令 firewall-cmd --permanent --zone=public --list-ports
//查看开启的端口,出现8080/tcp这开启正确

6.再次使用外部浏览器访问,这出现tomcat的欢迎界面。

补充: 
开启防火墙的命令 
systemctl start firewalld.service 
关闭防火墙的命令 
systemctl stop firewalld.service 
开机自动启动 
systemctl enable firewalld.service 
关闭开机自动启动 
systemctl disable firewalld.service 
查看防火墙状态 
systemctl status firewalld下列显示表示没有问题。


猜你喜欢

转载自blog.csdn.net/kangkang_hacker/article/details/80772941
今日推荐