一、安装tomcat
上篇博文中我们学习了java的基础知识,知道了tomcat是web container,通过java语言开发所以必须运行在JDK上,所以我们先安装jdk
1、安装jdk
[[email protected]_179 src]# ls apache-tomcat-8.0.29.tar.gz jdk-8u65-linux-x64.tar.gz [[email protected]_179 src]# tar xf jdk-8u65-linux-x64.tar.gz -C /usr/local [[email protected]_179 src]# cd /usr/local/jdk1.8.0_65/ [[email protected]_179 jdk1.8.0_65]# ls bin db javafx-src.zip lib man release THIRDPARTYLICENSEREADME-JAVAFX.txt COPYRIGHT include jre LICENSE README.html src.zip THIRDPARTYLICENSEREADME.txt
2、添加环境变量
[[email protected]_179 jdk1.8.0_65]# vi /etc/profile.d/java.sh 1 export JAVA_HOME=/usr/local/jdk1.8.0_65 2 export PATH=$JAVA_HOME/bin:$PATH
3、测试jdk是否安装成功
[[email protected]_179 ~]# java -version java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
4、安装tomcat
[[email protected]_179 src]# tar xf apache-tomcat-8.0.29.tar.gz -C /usr/local [[email protected]_179 src]# cd /usr/local [[email protected]_179 local]# ll 总用量 48 drwxr-xr-x 9 root root 4096 11月 26 15:02 apache-tomcat-8.0.29 drwxr-xr-x. 2 root root 4096 9月 23 2011 bin drwxr-xr-x. 2 root root 4096 9月 23 2011 etc drwxr-xr-x. 2 root root 4096 9月 23 2011 games drwxr-xr-x. 2 root root 4096 9月 23 2011 include drwxr-xr-x 8 10 143 4096 10月 7 08:29 jdk1.8.0_65 drwxr-xr-x. 2 root root 4096 9月 23 2011 lib drwxr-xr-x. 2 root root 4096 9月 23 2011 lib64 drwxr-xr-x. 2 root root 4096 9月 23 2011 libexec drwxr-xr-x. 2 root root 4096 9月 23 2011 sbin drwxr-xr-x. 5 root root 4096 10月 27 16:37 share drwxr-xr-x. 2 root root 4096 9月 23 2011 src [[email protected]_179 local]# ln -sv apache-tomcat-8.0.29 tomcat "tomcat" -> "apache-tomcat-8.0.29" [[email protected]_179 local]# cd tomcat/ [[email protected]_179 tomcat]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
5、添加环境变量
[[email protected]_179 ~]# vi /etc/profile.d/comcat.sh 1 export CATALINA_HOME=/usr/local/tomcat 2 export PATH=$CATALINA_HOME/bin:$PATH
6、测试tomcat是否安装成功
[[email protected]_179 ~]# catalina.sh -h Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_65 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Usage: catalina.sh ( commands ... ) commands: debug Start Catalina in a debugger debug -security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run -security Start in the current window with security manager start Start Catalina in a separate window start -security Start in a separate window with security manager stop Stop Catalina, waiting up to 5 seconds for the process to end stop n Stop Catalina, waiting up to n seconds for the process to end stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running configtest Run a basic syntax check on server.xml - check exit code for result version What version of tomcat are you running? Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined [[email protected]_179 ~]# 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/local/jdk1.8.0_65 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [[email protected]_179 ~]# netstat -nlptu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13387/java tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1078/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1166/master tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 13387/java tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN 13387/java
二、Tomcat的目录结构
1、Tomcat的目录结构
[[email protected]_179 tomcat]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
bin:脚本及启动时用到的类
lib:类库
conf:配置文件
logs:日志文件
webapps:应用程序默认部署目录
work:工作目录
temp:临时目录
2、配置文件
[[email protected]_179 tomcat]# ls conf Catalina catalina.properties logging.properties tomcat-users.xml web.xml catalina.policy context.xml server.xml tomcat-users.xsd
server.xml:主配置文件
context.xml:每个webapp都可以有专用的配置文件,这些配置文件通常位于webapp应用程序目录下的WEB-INF目录中,用于定义会话管理器、JBC等,conf/contest.xml是为各webapp提供默认配置
web.xml:每个webapp部署之后才能被访问,此文件则用于为所有的webapp提供默认部署相关配置
tomcat-users.xml:用户认证的帐号和密码配置文件
catalina.policy:当使用-security选项启动tomcat实例时会读取此配置文件来实现安全运行策略
catalina.properties:Java属性的定义文件,用于设定类加载器路径等,以及一些JVM相关的调优参数
logging.properties:日志相关的配置信息
3、webapp的组织结构
tomcat自带有两个webapp(应用程序):
Manager:管理tomcat自身的应用
Host_manage:管理tomcat的虚拟主机
[[email protected]_179 tomcat]# ls webapps/ #网站根目录 docs examples host-manager manager ROOT #子应用程序 [[email protected]_179 tomcat]# ls webapps/host-manager/ images index.jsp manager.xml META-INF WEB-INF
有特定组织形式、层次型目录结构:主要包含了servlet代码文件,JSP页面文件、类文件、部署描述文件等信息(创建一个app必须要有这些文件,文件可以为空但一定要有)
/:webapp的根目录
index.jsp:wepapp主页
WEB-INF:当前webapp的私有资源目录,通常存放当前webapp自用的web.xml和context.xml
classes:此webapp的私有类 (tomcat自带的2个webapp没有私有类,没有该目录)
lib:此webapp的私有类,被打包为jar格式的类(tomcat自带的2个webapp没有私有类,没有该目录)
META-INF:私有资源目录(可选的)
手动添加测试应用程序:
1)创建特定目录结构:
[[email protected]_179 webapps]# pwd /usr/local/tomcat/webapps [[email protected]_179 webapps]# mkdir -pv testapp/{WEB-INF,lib,classes,META-INF} mkdir: 已创建目录 "testapp" mkdir: 已创建目录 "testapp/WEB-INF" mkdir: 已创建目录 "testapp/lib" mkdir: 已创建目录 "testapp/classes" mkdir: 已创建目录 "testapp/META-INF"
2)创建/usr/local/tomcat/webapps/testapp/index.jsp
[[email protected]_179 webapps]# cd testapp/ [[email protected]_179 testapp]# vi index.jsp #测试只有这个主页文件也可以访问的呀? 1 <%@ page language="java" %> 2 <%@ page import="java.util.*" %> 3 <html> 4 <head> 5 <title>JSP test page.</title> 6 </head> 7 <body> 8 <% out.println("Hello,world!"); %> 9 </body> 10 </html>
注意:如果自定义host及webapp路径,在host指定的路径下创建ROOT来存放默认的站点主应用程序;