tomcat安裝與配置

前言:前面博文我們講解了tomcat與Java這篇博文就讓我們來了解一下tomcat的安裝與配置.


一:安裝JDK

獲取jdk安裝包和tomcat安裝包

 安裝jdk 

 因爲jdk沒有什麼依賴關係直接rpm包進行安裝.

[root@node2 ~]# rpm -ivh jdk-7u67-linux-x64.rpm

 修改環境變量

vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.7.0_67
export PATH=$PATH:$JAVA_HOME/bin

 測試一下

[root@node2 ~]# java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

OK 我們的JDK安裝成功了,現在來安裝tomcat


二:安裝Tomcat

 解壓tomcat

[root@node2 ~]# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/
[root@node2 ~]# cd /usr/local/
[root@node2 local]# ls
apache-tomcat-7.0.42  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@node2 local]# ln -sv apache-tomcat-7.0.42 tomcat
`tomcat' -> `apache-tomcat-7.0.42'

 修改tomcat環境變量

vim /etc/profile.d/tomcat.sh  
export CATALINA_HOME=/usr/local/tomcat    #tomcat爲我們創建的連接
export PATH=$PATH:$CATALINA_HOME/bin
[root@node2 tomcat]# source /etc/profile.d/tomcat.sh  #source一下配置文件

 測試一下

[root@node2 tomcat]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

檢查是否成功啓動

[root@node2 tomcat]# netstat -tnlp |grep java
tcp        0      0 :::8080                     :::*                        LISTEN      2194/java           
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      2194/java           
tcp        0      0 :::8009                     :::*                        LISTEN      2194/java

OK 我們的tomcat已經啓動其中8005端口啓動要比8009端口和8080端口慢點. 

測試訪問

wKioL1Q1SQjBfktmAAP031kq8pg550.jpg

Tomcat成功安裝.注意:在安裝tomcat之前要先安裝JDK.

三:tomcat的目錄結構

 [root@node2 ~]# cd /usr/local/tomcat/ 
 [root@node2 tomcat]# ls 
 bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work

        

    bin--tomcat的執行腳本

    conf--tomcat的配置文件

    lib--tomcat運行時需要的庫文件

    logs--tomcat運行時執行的日誌文件

    temp--tomcat臨時文件存放位置

    webapp--網站jsp文件放置位置

    work--Tomcat的工作目錄,Tomcat將翻譯JSP文件到的Java文件和class文件放在這裏.

   

 看一下tomcat的配置文件

  

[root@node2 tomcat]# ls ./conf 
Catalina catalina.policy catalina.properties context.xml logging.properties server.xml tomcat-users.xml web.xml

    

    server.xml:tomcat的全局的配置文件

    tomcat-users.xml : tomcat的用戶認證文件

    context.xml:每個webapp都有其配置文件,通常位於webapp目錄下的WEB-INF目錄中,通常用於定義會話管理器、Realm以及JDBC等;此配置文件是用於爲部署在當前tomcat實例上的所有的webapp提供默認配置;

    web.xml: 爲部署在當前tomcat實例上的所有的webapp提供默認部署描述符;通常用於爲webapp提供基本的servlet定義和MEM映射表;

    catalina.policy: 當基於-security選項啓動tomcat實例時會讀取此配置文件;用於定義JAVA的安全策略,配置訪問codebase或某些Java類的權限;

    catalina.properties: Java屬性定義的文件,用於設定類加載器路徑、安全包列表,以及一些調整jvm性能的相關參數的信息;

    logging.properties: 定義日誌相關的配置信息,如日誌級別、文件路徑等;


下面來介紹下tomcat的應用程序組成及webapp內文件目錄結構

webapp的特定組織格式,是一種層次型目標結構;通常包含了servlet代碼文件、jsp頁面文件、類文件、部署描述符文件等等;

   /: webapp的根目錄

   /WEB-INF: 當前webapp的私有資源目錄,通常web.xml和context.xml都放置於此目錄中;

   /classes: 此webapp私有的類

   /lib: 此webapp私有的,被打包爲jar格式的類;

   /META-INF: 


爲tomcat提供啓動腳本

  #!/bin/sh
  # Tomcat init script for Linux.
  #
  # chkconfig: 2345 96 14
  # description: The Apache Tomcat servlet/JSP container.
  # JAVA_OPTS='-Xms64m -Xmx128m'
  JAVA_HOME=/usr/java/latest
  CATALINA_HOME=/usr/local/tomcat
  export JAVA_HOME CATALINA_HOME
  case $1 in
  start)
    exec $CATALINA_HOME/bin/catalina.sh start ;;
  stop)
    exec $CATALINA_HOME/bin/catalina.sh stop;;
  restart)
    $CATALINA_HOME/bin/catalina.sh stop
    sleep 2
    exec $CATALINA_HOME/bin/catalina.sh start ;;
  *)
    echo "Usage: `basename $0` {start|stop|restart}"
    exit 1
    ;;
  esac

[root@node2 ~]# chmod +x /etc/rc.d/init.d/tomcat 
[root@node2 ~]# chkconfig --add tomcat 
[root@node2 ~]# service tomcat stop

四:配置tomcat虛擬機

首先我們先來查看下tomcat的server配置文件

<?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="8005" shutdown="SHUTDOWN">  #管理端口
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--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" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <!-- 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">    #定義的一個service是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="8080" protocol="HTTP/1.1"    #Connector 連接器端口
               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="8009" 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"/>
      -->
      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- 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"/>
      </Realm>
      <Host name="localhost"  appBase="webapps"  #這裏定義了虛擬主機
          unpackWARs="true" autoDeploy="true">

        <!-- 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
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" /> #這裏定義了日誌文件屬性

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

好下面進行虛擬主機的配置

vim /usr/local/tomcat/conf/server.xml

在</host>與</Engine>之間添加下面基於主機名的虛擬主機.爲了方便測試 修改server.xml裏的監聽端口8080爲80

<Host name="www.llh.com" appBase="/web/webapp"
            unpackWARs="true" autoDeploy="true">
    <Context path="/" docBase="/web/webapp" reloadable="true"/>
</Host>

提供jsp文件

mkdir -pv /web/webapp

vim index.jisp

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
 <head>
  <title>JSP test page.</title>
 </head>
 <body>
  <% out.println("Welcome to test. Site, http://www.llh.com"); %>
 </body>
</html>


wKiom1Q2i1zRIpRYAACg313eGec875.jpg


有時我們會疑惑Host裏的appBase和Context裏的docBase有什麼聯繫和區別?

  host裏的appBase所定義的路徑裏安裝的是非歸檔的web應用程序的目錄或歸檔後的WAR文件的目錄路徑

  Context裏的docBase路徑相應的Web應用程序的存放位置;也可以使用相對路徑,起始路徑爲此Context所屬Host中appBase定義的路徑;切記,docBase的路徑名不能與相應的Host中appBase中定義的路徑名有包含關係,比如,如果appBase爲deploy,而docBase絕不能爲deploy-bbs類的名字;


五:部署一個jsp網站案例

  部署jsp網址需要用到mysql,首先把mysql安裝上去

  yum install mysql-server

mysql> CREATE DATABASE jeebbs;  #創建論壇的數據庫
Query OK, 1 row affected (0.01 sec)

mysql> GRANT ALL ON jeebbs.* TO 'jeebbs'@'localhost' IDENTIFIED BY '123';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON jeebbs.* TO 'jeebbs'@'127.0.0.1' IDENTIFIED BY '123';
Query OK, 0 rows affected (0.00 sec) #給連接mysql的用戶授權.

獲得jecenter安裝包解壓安裝包內軟件到/web/webapp

wKiom1Q3yi-QHiBdAAPKB3_GL1o501.jpg

配置數據庫名字和數據庫用戶

wKioL1Q3ywDhEB4oAAIyJxYQkgQ676.jpg

看基於tomcat環境的個人空間開通了

wKioL1Q3y4fRL3gOAAKbJl2ABjc565.jpg


OK 我們的tomcat配置就寫到這裏,歡迎大家提出寶貴的意見.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章