首页 > 代码库 > linux下安装配置tomcat以及tomcat开机自启配置

linux下安装配置tomcat以及tomcat开机自启配置


Linux下Tomcat安装配置以及Windows不能连接服务器Tomcat解决方案


一、从官方网站上下载tomcat软件包。

官网地址: http://tomcat.apache.org/
点击左侧的 download的一个版本,我选择的是 tomcat7.0,选择一个后缀名为.tar.gz文件直接下载到本地。


二、通过工具SSH Secure 上传至linux服务器中,进行解压

解压tomcat压缩文件: 

 #tar zxvf apache-tomcat-7.0.53.tar.gz

将解压后的tomcat文件移动至/usr/local下

 #mv apach-tomcat-6.0.16 /usr/local


三、配置tomcat环境;【之前我已经安装好JDK了】

 编辑profie文件:

#vi /etc/profile

将下面文件copy到export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL后面;具体jdk路径根据你安装的位置来配置;

export JAVA_HOME=/usr/java/jdk1.7.0_15
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/usr/local/apache-tomcat-7.0.53
export CATALINA_BASE=/usr/local/apache-tomcat-7.0.53

保存退出,重新登录使之生效。 
   
四、启动tomcat服务器
进入到/usr/local/apach-tomcat-6.0.20/bin目录

运行./startup.sh

启动成功会出现如下内容:
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.53
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.53
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.53/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_15

Using CLASSPATH:       /usr/local/apache-tomcat-7.0.53/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.53/bin/tomcat-juli.jar
Tomcat started.


五、测试是否启动成功

在linux浏览器中输入http://localhost:8080/就可以看到tomcat的log了

如果8080端口被占用,或者你只是想修改tomcat的端口,请参考如下文件,我直接copy上来了。【我这里是修改为9080端口】

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!--
  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.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="9005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container", 
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
  
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
        maxThreads="150" minSpareThreads="4"/>
    -->
    
    
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="9080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    -->           
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">         
    --> 
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->        

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->

      </Host>
    </Engine>
  </Service>
</Server>

修改完后,启动tomcat,如果Windows访问Linux的Tomcat,显示无法连接,先查看tomcat下的logs日志文件,如果现实地址已被占用,则需重新修改,如果已经正常启动,说明防火墙未关闭。则只有修改linux防火墙的配置,打开8080端口。

1,在开启了防火墙时,做如下设置,开启相关端口,修改/etc/sysconfig/iptables 文件,添加以下内容:
        -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

2、直接关闭Linux防火墙。

在此说一下关于启动和关闭防火墙的命令

1) 重启后生效
       开启: chkconfig iptables on
       关闭: chkconfig iptables off
   2) 即时生效,重启后失效
       开启: service iptables start
       关闭: service iptables stop


附文:linux下配置完tomcat后,要想tomcat开机自动启动的话,需要另外的配置!

方法一:【建议采用方法一,我试过了,成功了,方法二貌似没弄好,呵呵】

 linux 下tomcat开机自启动

 修改Tomcat/bin/startup.sh 为:

 export JAVA_HOME=/usr/java/jdk1.7.0_15

 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.

 export PATH=$PATH:$JAVA_HOME/bin

 export CATALINA_HOME=/usr/local/tomcat

 /usr/local/tomcat/bin/catalina.sh start

 

在/etc/rc.d/rc.local中加入:

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

方法二:

 1、把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat.

 #!/bin/bash

 #

 # kenny kenny.zhou@tom.com

 # /etc/rc.d/init.d/tomcat

 # init script for tomcat precesses

 #

 # processname: tomcat

 # description: tomcat is a j2se server

 # chkconfig: 2345 86 16

 # description:  Start up the Tomcat servlet engine.

 if [ -f /etc/init.d/functions ]; then

         . /etc/init.d/functions

 elif [ -f /etc/rc.d/init.d/functions ]; then

         . /etc/rc.d/init.d/functions

 else

         echo -e "\atomcat: unable to locate functions lib. Cannot continue."

         exit -1

 fi

 RETVAL=$?

 CATALINA_HOME="/usr/local/tomcat"

 case "$1" in

 start)

         if [ -f $CATALINA_HOME/bin/startup.sh ];

           then

             echo $"Starting Tomcat"

             $CATALINA_HOME/bin/startup.sh

         fi

         ;;

 stop)

         if [ -f $CATALINA_HOME/bin/shutdown.sh ];

           then

             echo $"Stopping Tomcat"

             $CATALINA_HOME/bin/shutdown.sh

         fi

         ;;

 *)

echo $"Usage: $0 {start|stop}"

         exit 1

         ;;

 esac

 exit $RETVAL

 #以上为tomcat开机启动的启动脚本代码

 2、将tomcat文件拷贝到/etc/init.d/下,并运行:chkconfig --add tomcat  //这句的意思是添加tomcat服务

 3、在tomcat/bin/catalina.sh文件中加入以下语句:

 export JAVA_HOME=/YOURPATH/tomcat/jdk

 export CATALINA_HOME=/YOURPATH/tomcat

 export CATALINA_BASE=/YOURPATH/tomcat

 export CATALINA_TMPDIR=/YOURPATH/tomcat/temp

 启动tomcat: service tomcat start

 

停止tomcat:  service tomcat stop

 

至此tomcat开机启动全部配置完毕,下次linux重启的时候就会自动启动tomcat了