Tomcat基本配置
第二十六课 Tomcat基本配置
目录
一、 Tomcat介绍
二、 安装jdk
三、 安装Tomcat
四、 配置Tomcat监听80端口
五、 配置Tomcat虚拟主机
六、 Tomcat日志
七、扩展
一、 Tomcat介绍
Tomcat 是什么
Tomcat 是由 Apache软件基金会开发的一个 Servlet 容器,实现了对 Servlet 和 JSP 的支持,并提供了作为Web服务器的一些特有功能,如Tomcat管理和控制平台、安全域管理和Tomcat阀等。
由于 Tomcat 本身也内含了一个 HTTP 服务器,它也可以被视作一个单独的 Web 服务器。但是,不能将 Tomcat 和 Apache HTTP 服务器混淆,Apache HTTP 服务器是一个用 C 语言实现的 HTTP Web 服务器;这两个 HTTP web server 不是捆绑在一起的。Tomcat 包含了一个配置管理工具,也可以通过编辑XML格式的配置文件来进行配置。
tomcat是一个中间件,真正起作用解析java脚本的是jdk。jdk(java development kit)是整个java的核心,它包含了java运行环境和一堆java相关的工具以及java基础库。
最主流的jdk为sun公司发布的jdk,除此之外,其实IBM公司也有发布JDK,CentOS上也可以用yum安装openjdk
java程序写的网站用tomcat+jdk来运行
二、 安装jdk
1.从Orcale官网下载当前的jdk版本,然后上传到服务器上
当前下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
2.解压jdk包
[root@tomcat8 ~]# ls -l /usr/local/src/
total 195752
-rw-r--r-- 1 root root 190890122 May 23 21:09 jdk-8u171-linux-x64.tar.gz
//解压jdk包
[root@tomcat8 src]# tar -zxf jdk-8u171-linux-x64.tar.gz
[root@tomcat8 src]# ll
total 195752
drwxr-xr-x 8 10 143 255 Mar 28 20:18 jdk1.8.0_171
-rw-r--r-- 1 root root 190890122 May 23 21:09 jdk-8u171-linux-x64.tar.gz
3.移动jdk包到/usr/local/下
[root@tomcat8 src]# ls -ld /usr/local/jdk1.8/
drwxr-xr-x 8 10 143 255 Mar 28 20:18 /usr/local/jdk1.8/
4.新建jdk环境变量
[root@tomcat8 src]# vim /etc/profile
//在文件末尾添加如下内容
export JAVA_HOME=/usr/local/jdk1.8/
export JAVA_BIN=$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/jre/lib/charsets.jar
//使环境变量立即生效
[root@tomcat8 src]# source /etc/profile
5.测试jdk环境是否生效
[root@tomcat8 src]# source /etc/profile
[root@tomcat8 src]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
三、 安装Tomcat
1.下载tomcat源码包,这里以tomcat-8.5.31为例
[root@tomcat8 ~]# cd /usr/local/src/
[root@tomcat8 src]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz
--2018-07-16 03:43:30-- https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz
Resolving archive.apache.org (archive.apache.org)... 163.172.17.199
Connecting to archive.apache.org (archive.apache.org)|163.172.17.199|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9552281 (9.1M) [application/x-gzip]
Saving to: ‘apache-tomcat-8.5.31.tar.gz’
100%[=====================================================================================>] 9,552,281 120KB/s in 2m 35s
2018-07-16 03:46:06 (60.2 KB/s) - ‘apache-tomcat-8.5.31.tar.gz’ saved [9552281/9552281]
[root@tomcat8 src]# ls -lh
total 192M
-rw-r--r-- 1 root root 9.2M Apr 27 16:47 apache-tomcat-8.5.31.tar.gz
-rw-r--r-- 1 root root 183M May 23 21:09 jdk-8u171-linux-x64.tar.gz
2.解压tomcat并移动到/usr/local
[root@tomcat8 src]# tar -zxf apache-tomcat-8.5.31.tar.gz
[root@tomcat8 src]# ll
total 195752
drwxr-xr-x 9 root root 160 Jul 16 03:49 apache-tomcat-8.5.31
-rw-r--r-- 1 root root 9552281 Apr 27 16:47 apache-tomcat-8.5.31.tar.gz
-rw-r--r-- 1 root root 190890122 May 23 21:09 jdk-8u171-linux-x64.tar.gz
[root@tomcat8 src]# mv apache-tomcat-8.5.31 /usr/local/tomcat
[root@tomcat8 src]# ls -ld /usr/local/tomcat/
drwxr-xr-x 9 root root 160 Jul 16 03:49 /usr/local/tomcat/
3.启动tomcat
[root@tomcat8 src]# /usr/local/tomcat/bin/startup.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
//检测是否正常启动
[root@tomcat8 src]# netstat -nltup | grep java
tcp6 0 0 :::8080 :::* LISTEN 1267/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1267/java
tcp6 0 0 :::8009 :::* LISTEN 1267/java
本地测试
[root@tomcat8 src]# curl -x:127.0.0.1:80 127.0.0.1:8080
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Apache Tomcat/8.5.31</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
....中间略...
</ul>
</div>
</div>
<br class="separator" />
</div>
<p class="copyright">Copyright ©1999-2018 Apache Software Foundation. All Rights Reserved</p>
</div>
</body>
</html>
远程浏览器测试
四、 配置Tomcat监听80端口
Tomcat默认侦听的是8080端口,不是很方便。可以通过修改配置文件的方式将默认侦听的端口改为80.
[root@tomcat8 src]# vim /usr/local/tomcat/conf/server.xml
//修改如下字段
Connector port="8080"---->Connector port="80"
//停止tomcat
[root@tomcat8 src]# /usr/local/tomcat/bin/shutdown.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
//启动tomcat
[root@tomcat8 src]# /usr/local/tomcat/bin/startup.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
//注意,tomcat没有restart参数,需要用shutdown.sh关闭,然后再用Startup启动。
本地测试
[root@tomcat8 src]# curl -x127.0.0.1:80 127.0.0.1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Apache Tomcat/8.5.31</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</head>
...中间略...
</div>
<p class="copyright">Copyright ©1999-2018 Apache Software Foundation. All Rights Reserved</p>
</div>
</body>
远程浏览器测试
五、 配置Tomcat虚拟主机
这里以增加虚拟主机www.tomcattest.com为例
[root@tomcat8 src]# vim /usr/local/tomcat/conf/server.xml
...略...
//默认虚拟主机的配置<Host...> </Host>表示一个虚拟主机
<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>
//增加www.tomcattest.com虚拟主机配置
<Host name="www.tomcattest.com" appBase=""
unpackWARs= "true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/tomcattest.com/" debug="0" reloadable="true" crossContext="true"/>
</Host>
[root@tomcat8 tomcattest]# /usr/local/tomcat/bin/shutdown.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat8 tomcattest]# /usr/local/tomcat/bin/startup.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat8 tomcattest]# mkdir -p /data/wwwroot/tomcattest.com
[root@tomcat8 tomcattest]# ls -ld /data/wwwroot/tomcattest.com
drwxr-xr-x 2 root root 37 Jul 16 04:21 /data/wwwroot/tomcattest.com
//新建测试文件
[root@tomcat8 tomcattest]# vim 1.html
[root@tomcat8 tomcattest]# cat 1.html
I am www.tomcattest.com!
[root@tomcat8 tomcattest]# mv /usr/local/tomcat/webapps/ROOT/index.jsp /data/wwwroot/tomcattest/index.jsp
本地测试
[root@tomcat8 tomcattest]# curl -x127.0.0.1:80 www.tomcattest.com/1.html
I am www.tomcattest.com!
[root@tomcat8 tomcattest]# curl -x127.0.0.1:80 www.tomcattest.com/index.jsp -I
HTTP/1.1 200
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 16 Jul 2018 08:33:23 GMT
远程浏览器测试
Zrblog的安装
1.从官网下载zrblog的安装包并上传到服务器
2.将程序复制到webapp目录
//因为与默认网站的目录重名,ROOT.war解压后将默认网站的内容覆盖了
//如果不想覆盖默认网站,请在拷入ROOT目录前改名
[root@tomcat8 ~]# ls /usr/local/src/
apache-tomcat-8.5.31.tar.gz jdk-8u171-linux-x64.tar.gz ROOT.war
[root@tomcat8 ~]# cd !$
cd /usr/local/src/
[root@tomcat8 src]# mv ROOT.war /usr/local/tomcat/webapps/
[root@tomcat8 src]# cd !$
cd /usr/local/tomcat/webapps/
[root@tomcat8 webapps]# pwd
/usr/local/tomcat/webapps
[root@tomcat8 webapps]# ls
docs examples host-manager manager ROOT ROOT.war
3.新建zrlog数据库用户
[root@tomcat8 init.d]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on zrlog.* to 'zrlogadmin'@127.0.0.1 identified by '123456';
Query OK, 0 rows affected (0.01 sec)
4.进入安装向导
如果不想利用默认的网站,可将应用程序放入相应
会自动解压。已经解压好的网站程序,也可以放到docbase定义的目录下。
六、 Tomcat日志
tomcat日志在/usr/local/tomcat/logs/下
其中catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。
其中catalina.2017-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
localhost和localhost_access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access字样的为默认虚拟主机的错误日志。
访问日志默认不会生成,需要在server.xml中配置一下。
[root@tomcat8 tomcattest]# cd /usr/local/tomcat/logs/
[root@tomcat8 logs]# ls -l
total 140
-rw-r----- 1 root root 63847 Jul 16 04:26 catalina.2018-07-16.log
-rw-r----- 1 root root 63847 Jul 16 04:26 catalina.out
-rw-r----- 1 root root 0 Jul 16 03:51 host-manager.2018-07-16.log
-rw-r----- 1 root root 4134 Jul 16 04:26 localhost.2018-07-16.log
-rw-r----- 1 root root 1730 Jul 16 04:04 localhost_access_log.2018-07-16.txt
-rw-r----- 1 root root 0 Jul 16 03:51 manager.2018-07-16.log
//catalina日志示例
-rw-r----- 1 root root 0 Jul 16 03:51 manager.2018-07-16.log
[root@tomcat8 logs]# head -n 5 catalina.2018-07-16.log
16-Jul-2018 03:51:21.812 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.31
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Apr 27 2018 20:24:25 UTC
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.31.0
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-693.el7.x86_64
//catalina.out日志示例
[root@tomcat8 logs]# head -n 5 catalina.out
16-Jul-2018 03:51:21.812 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.31
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Apr 27 2018 20:24:25 UTC
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.31.0
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
16-Jul-2018 03:51:21.816 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-693.el7.x86_64
//localhost_access日志示例
[root@tomcat8 logs]# head -n 5 localhost_access_log.2018-07-16.txt
10.0.1.229 - - [16/Jul/2018:03:53:06 -0400] "GET / HTTP/1.1" 200 11250
10.0.1.229 - - [16/Jul/2018:03:53:06 -0400] "GET /tomcat.png HTTP/1.1" 200 5103
10.0.1.229 - - [16/Jul/2018:03:53:06 -0400] "GET /tomcat.css HTTP/1.1" 200 5581
10.0.1.229 - - [16/Jul/2018:03:53:07 -0400] "GET /bg-upper.png HTTP/1.1" 200 3103
10.0.1.229 - - [16/Jul/2018:03:53:07 -0400] "GET /asf-logo-wide.svg HTTP/1.1" 200 27235
//localhost日志示例
[root@tomcat8 logs]# head -n 5 localhost.2018-07-16.log
16-Jul-2018 03:51:23.554 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
16-Jul-2018 03:51:23.554 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
16-Jul-2018 03:51:23.556 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@371e34e1')
16-Jul-2018 04:00:44.831 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log SessionListener: contextDestroyed()
16-Jul-2018 04:00:44.831 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log ContextListener: contextDestroyed()
//配置www.tomcattest.com的访问日志
[root@tomcat8 logs]# vim /usr/local/tomcat/conf/server.xml
</Host>
<Host name="www.tomcattest.com" appBase=""
unpackWARs= "true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/tomcattest.com/" debug="0" reloadable="true" crossContext="true"/>
//增加日志配置
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/tomcattest"
prefix="localhost_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
[root@tomcat8 tomcattest]# /usr/local/tomcat/bin/shutdown.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat8 tomcattest]# /usr/local/tomcat/bin/startup.sh
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//jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat8 tomcattest]# ls
localhost_access.2018-07-16.log
[root@tomcat8 tomcattest]# curl -x127.0.0.1:80 www.tomcattest.com/1.html
I am www.tomcattest.com!
[root@tomcat8 tomcattest]# cat localhost_access.2018-07-16.log
127.0.0.1 - - [16/Jul/2018:04:52:41 -0400] "GET /1.html HTTP/1.1" 200 25
七、扩展
java容器比较
http://my.oschina.net/diedai/blog/271367
http://www.360doc.com/content/11/0618/21/16915_127901371.shtml
j2ee、j2se、ejb、javabean、serverlet、jsp之间关系
http://bbs.csdn.net/topics/50015576
tomcat server.xml配置详解
http://blog.csdn.net/yuanxuegui2008/article/details/6056754
tomcat常用数据库连接的方法