标签: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
测试访问
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 "%r" %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>
有时我们会疑惑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
配置数据库名字和数据库用户
看基于tomcat环境的个人空间开通了
OK 我们的tomcat配置就写到这里,欢迎大家提出宝贵的意见.
本文出自 “slayer” 博客,请务必保留此出处http://slayer.blog.51cto.com/4845839/1562328
标签:tomcat
原文地址:http://slayer.blog.51cto.com/4845839/1562328