任务列表:
16.1 Tomcat介绍
16.2 安装jdk
16.3 安装Tomcat
16.4 配置Tomcat监听80端口
16.5/16.6/16.7 配置Tomcat虚拟主机
16.8 Tomcat日志
扩展
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
JAR、WAR包区别 http://blog.csdn.net/lishehe/article/details/41607725
我们已经学过了LAMP和LNMP架构;针对的开发语言是PHP;可以说PHP是一门开发Web程序非常流行的语言;早期的比较流行的是asp,是在windows平台上运行的一种编程语言;但是因为安全性不高;所以越来越多做网站的人用Php去开发网站,相对来说也是比较安全的。
除了PHP外,还有一门语言叫java;java是一门非常庞大的开发语言;不仅仅是可以开发开网站;也可以开发大型的软件、工具、甚至是游戏都可以;tomcat实际上是一个中间件,用来运行Java语言写的网站;那我们Php写的代码,要用php去解析;java就要用tomcat加上JDK一起去解析。
下载连接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
[root@linux-02 src]# mv jdk-8u211-linux-x64.tar.gz /usr/local/src/
[root@linux-02 src]# cd /usr/local/src/
[root@linux-02 src]# ls
mysql-5.6.43-linux-glibc2.12-x86_64 nginx-1.16.0 php-5.6.39.tar.bz2
jdk-8u211-linux-x64.tar.gz mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz nginx-1.16.0.tar.gz php-7.3.0.tar.bz2
mariadb-10.2.23-linux-x86_64.tar.gz mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz php-5.6.39
[root@linux-02 src]# tar -zxvf jdk-8u211-linux-x64.tar.gz
……
解压过程省略
[root@linux-02 src]# ls
jdk1.8.0_211 mysql-5.6.43-linux-glibc2.12-x86_64 nginx-1.16.0 php-5.6.39.tar.bz2
jdk-8u211-linux-x64.tar.gz mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz nginx-1.16.0.tar.gz php-7.3.0.tar.bz2
mariadb-10.2.23-linux-x86_64.tar.gz mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz php-5.6.39
[root@linux-02 src]# mv jdk1.8.0_211 /usr/local/jdk1.8
[root@linux-02 src]# ls /usr/local/jdk1.8/
bin include jre LICENSE README.html src.zip THIRDPARTYLICENSEREADME.txt
COPYRIGHT javafx-src.zip lib man release THIRDPARTYLICENSEREADME-JAVAFX.txt
[root@linux-02 src]#
## 添加系统环境变量 ##
[root@linux-02 src]# vim /etc/profile
## 在文件末添加jdk的目录 ##
export JAVA_HOME=/usr/local/jdk1.8/
export JAVA_BIN=/usr/local/jdk1.8/bin
export JRE_HOME=/usr/local/jdk1.8/jre
export PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
export CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
[root@linux-02 src]# source /etc/profile
[root@linux-02 src]# java -version //出现以下内容表示配置成功。
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
[root@linux-02 src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.20/bin/apache-tomcat-9.0.20.tar.gz
--2019-05-28 03:21:34-- https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.20/bin/apache-tomcat-9.0.20.tar.gz
正在解析主机 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
正在连接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10851264 (10M) [application/x-gzip]
正在保存至: “apache-tomcat-9.0.20.tar.gz”
100%[===========================================================================================>] 10,851,264 1.69MB/s 用时 7.5s
2019-05-28 03:21:42 (1.37 MB/s) - 已保存 “apache-tomcat-9.0.20.tar.gz” [10851264/10851264])
[root@linux-02 src]# ls
apache-tomcat-9.0.20.tar.gz mysql-5.6.43-linux-glibc2.12-x86_64 nginx-1.16.0 php-5.6.39.tar.bz2
jdk-8u211-linux-x64.tar.gz mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz nginx-1.16.0.tar.gz php-7.3.0.tar.bz2
mariadb-10.2.23-linux-x86_64.tar.gz mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz php-5.6.39
[root@linux-02 src]# tar -zxvf apache-tomcat-9.0.20.tar.gz
……
解压省略
[root@linux-02 src]# mv apache-tomcat-9.0.20 /usr/local/tomcat
[root@linux-02 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@linux-02 src]# netstat -ltnp | grep java
tcp6 0 0 :::8009 :::* LISTEN 16548/java
tcp6 0 0 :::8080 :::* LISTEN 16548/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 16548/java
[root@linux-02 src]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@linux-02 src]# systemctl stop firewalld
[root@linux-02 src]# iptable -F
[root@linux-02 src]# cd /usr/local/tomcat/
[root@linux-02 tomcat]# vim conf/server.xml
注意tomcat启动的时候没有restart的选项,所以在启动tomcat的时候需要先关闭掉,然后在启动tomcat。再启动tomcat的时候需要查看下8005管理端口有没有启动,如果没有启动起来,web网页会打开很慢。
[root@linux-02 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@linux-02 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@linux-02 tomcat]# netstat -ltnp | grep java
tcp6 0 0 :::8009 :::* LISTEN 16730/java
tcp6 0 0 :::80 :::* LISTEN 16730/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 16730/java
[root@linux-02 tomcat]#
在tomcat的配置文件中,增加一个虚拟主机,代码如下,注意在配置虚拟主机文件的时候,配置host文件所处位置。
[root@linux-02 tomcat]# vim conf/server.xml
<Host name="www.xihaji.cn" appBase=""
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/www.xihaji.cn/" debug="0" reloadable="true" crossContext="true"/>
</Host>
[root@linux-02 tomcat]# cd /usr/local/src/
[root@linux-02 src]# wget -O zrlog.war http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war&ref=index
[2] 16919
[1] 退出 1 wget -O http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
[root@linux-02 src]# --2019-05-28 03:59:32-- http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
正在解析主机 dl.zrlog.com (dl.zrlog.com)... 112.17.55.225, 112.17.55.203
正在连接 dl.zrlog.com (dl.zrlog.com)|112.17.55.225|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10316293 (9.8M) [application/zip]
正在保存至: “zrlog.war”
100%[===========================================================================================>] 10,316,293 5.48MB/s 用时 1.8s
2019-05-28 03:59:34 (5.48 MB/s) - 已保存 “zrlog.war” [10316293/10316293])
[2]+ 完成 wget -O zrlog.war http://dl.zrlog.com/release/zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
[root@linux-02 src]# ls
zrlog.war包放置到webapps下会自动解压成zrlog的文件夹,由于我们在配置文件中有设置自动解压。
[root@linux-02 src]# du -sh zrlog.war
9.9M zrlog.war
[root@linux-02 src]# mv zrlog.war /usr/local/tomcat/webapps/
[root@linux-02 src]# cd /usr/local/tomcat/webapps/
[root@linux-02 webapps]# ls
docs examples host-manager manager ROOT zrlog.war
[root@linux-02 webapps]# ls
docs examples host-manager manager ROOT zrlog.war
## 自动解压zrlog.war包 ##
[root@linux-02 webapps]# ls
docs examples host-manager manager ROOT zrlog zrlog.war
[root@linux-02 webapps]#
检查本机的mysql是否启动,创建数据库zrlog,创建一个zrlog用户访问数据库zrlog,测试创建的用户是否可以登录数据库。
[root@linux-02 webapps]# ps aux | grep mysql
root 7018 0.0 0.0 115644 1812 ? S 03:00 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/linux-02.pid
mysql 7287 0.1 24.4 1302756 455176 ? Sl 03:00 0:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=linux-02.err --pid-file=/data/mysql/linux-02.pid --socket=/tmp/mysql.sock
root 16961 0.0 0.0 112728 988 pts/0 R+ 04:08 0:00 grep --color=auto mysql
[root@linux-02 webapps]# mysql -uroot -plinux002
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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 [email protected] identified by "linux002" ;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@linux-02 webapps]# mysql -uzrlog -plinux002 -h127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> exit
Bye
刚才修改过tomcat的配置文件,所以我们需要重新启动下tomcat服务。
[root@linux-02 webapps]# mv zrlog/* /data/wwwroot/www.xihaji.cn/
[root@linux-02 webapps]# /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
(reverse-i-search)`start': ./bin/^Cartup.sh
[root@linux-02 webapps]# /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@linux-02 webapps]#
网站的图标和一些重要信息都在/usr/local/tomcat/webapps/ROOT下
[root@linux-02 webapps]# ls
docs examples host-manager manager ROOT zrlog zrlog.war
[root@linux-02 webapps]# cd ROOT/
[root@linux-02 ROOT]# ls
asf-logo-wide.svg bg-middle.png bg-upper.png index.jsp tomcat.css tomcat.png tomcat.svg
bg-button.png bg-nav.png favicon.ico RELEASE-NOTES.txt tomcat.gif tomcat-power.gif WEB-INF
[root@linux-02 ROOT]#
[root@linux-02 ROOT]# cd /usr/local/tomcat/logs/
[root@linux-02 logs]# ls
catalina.2019-05-28.log host-manager.2019-05-28.log localhost_access_log.2019-05-28.txt
catalina.out localhost.2019-05-28.log manager.2019-05-28.log
[root@linux-02 logs]#
访问日志默认不会生成,需要在server.xml中配置;
[root@linux-02 logs]# vim /usr/local/tomcat/conf/server.xml
此为虚拟主机下的,访问日志配置:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" //关键字logs
prefix="localhost_access_log" suffix=".txt" //定义的前缀名,后缀名
pattern="%h %l %u %t "%r" %s %b" /> //定义日志格式
新增加的虚拟主机默认并不会生成类似默认虚拟主机的那个localhost.日期.log日志; 错误日志会统一记录到catalina.out中。
关于Tomcat日志,最需要关注catalina.out,当出现问题时,我们应该第一想到去查看它。