任务1 Tomcat介绍
1、简介
Tomcat是Apache软件基金会( Apache Software Foundation )的Jakarta项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成。受Java爱好者的喜爱,并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器。
Tomcat服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。目前有很多网站是用Java编写的,所以解析。
Java程序就必须有相关的软件来完成,Tomcat就是其中之一。
Java程序写的网站用Tomcat+JDK来运行。
Tomcat是一个中间件,真正起作用的,解析Java脚本的是JDK。
JDK(Java Development Kit)是整个Java的核心,它包含了Java运行环境和一堆Java相关的工具以及Java基础库。最主流的JDK为Sun公司发布的JDK,除此之外,其实IBM公司也有发布JDK,CentOS上也可以用yum安装OpenJDK。
2、 环境准备
规划节点,本次实验为单节点部署:
节点IP | 主机名 | 节点 |
---|---|---|
192.168.100.10 | tomcat | Tomcat |
关闭防火墙并设置开机不自启
[root@tomcat ~]# systemctl stop firewalld
[root@tomcat ~]# 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@tomcat ~]# setenforce 0
[root@tomcat ~]#
3、修改主机名
//修改主机名,可修改为tomcat
[root@localhost ~]# hostnamectl set-hostname tomcat
[root@localhost ~]# su
[root@tomcat ~]#
任务2 安装JDK
1、JDK的下载
使用浏览器访问这个地址下载
https://www.oracle.com/java/technologies/javase-downloads.html
下载需要注册账号,注册一个即可。
通过CRT上传JDK到/usr/local/src/目录下。
2、JDK安装
上传jdk
[root@tomcat ~]# cd /usr/local/src/
[root@tomcat src]# ls
jdk-8u301-linux-x64.tar.gz
[root@tomcat src]#
解压jdk
[root@tomcat src]# tar -zxvf jdk-8u301-linux-x64.tar.gz
移动到/usr/local/目录下改名为jdk1.8
[root@tomcat src]# mv jdk1.8.0_301/ /usr/local/jdk1.8
[root@tomcat src]# ls /usr/local/
bin etc games include jdk1.8 lib lib64 libexec mysql sbin share src
[root@tomcat src]#
3、修改环境变量
[root@tomcat src]# vim /etc/profile
将以下内容添加到文件底部
JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
刷新环境变量
[root@tomcat src]# source /etc/profile
4、检查JDK安装情况
[root@tomcat src]# java -version
java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
任务3 安装Tomcat
1、安装Tomcat
官网下载地址:http://tomcat.apache.org/
CRT上传到虚拟机/usr/local/src/目录下
[root@tomcat src]# ls
apache-tomcat-8.5.70.tar.gz jdk-8u301-linux-x64.tar.gz
解压
[root@tomcat src]# tar -zxvf apache-tomcat-8.5.70.tar.gz
移动文件到/usr/local目录下,并命名为tomcat
[root@tomcat src]# mv apache-tomcat-8.5.70 /usr/local/tomcat
[root@tomcat src]#
2、启动Tomcat
启动Tomcat
[root@tomcat 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@tomcat src]#
3、测试
[root@tomcat src]# netstat -lnpt | grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 4870/java
tcp6 0 0 :::8080 :::* LISTEN 4870/java
[root@tomcat src]#
8080为提供Web服务的端口;
8005为管理端口;
默认有8080和8005端口即可。
8009端口为第三方服务调用的端口,比如httpd和Tomcat结合时会用到。
这时打开浏览器输入地址http://192.168.100.10:8080即可查看tomcat默认页面。
任务4 配置Tomcat监听80端口
1、修改配置文件
Tomcat 监听的是8080端口,如果想要直接访问,不加8080端口(默认输入一个IP或者域名,访问80端口时是可以省略IP后端口号的)就需要配置Tomcat监听80端口,Tomcat是支持端口自定义的。
[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
直接搜索8080找到如下内容
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
将这里的8080直接改成80
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
关闭服务
[root@tomcat ~]# /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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
启动服务
[root@tomcat ~]# /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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@tomcat ~]#
2、测试
[root@tomcat ~]# netstat -plnt | grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 4963/java
tcp6 0 0 :::80 :::* LISTEN 4963/java
[root@xinshaohua ~]#
任务5 配置Tomcat虚拟主机
1、查看默认虚拟主机
[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
<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>
# 这是一个默认的虚拟主机
# 其中<Host>和</Host>之间的配置为虚拟主机配置部分:
# name定义域名;
# appBase定义应用的目录;
# unpackWARs为是否自动解压war包;
# autoDeploy 如果此项设置为true,表示Tomcat服务处于运行状态,
# 能够检测appbase下的文件,如果有新的Web应用加入进来,会自动发布这个Web应用。
# 自动解压war包
[root@tomcat tomcat]# ls /usr/local/tomcat/webapps/
docs examples host-manager manager ROOT
[root@tomcat tomcat]#
Java的应用通常是一个JAR的压缩包,你只需要将JAR的压缩包放到appBase目录下面即可。
刚刚我访问的Tomcat默认页其实就是在appBase目录下面,不过是在它子目录ROOT里
[root@tomcat tomcat]# ls /usr/local/tomcat/webapps/ROOT/
asf-logo-wide.svg bg-middle.png bg-upper.png index.jsp tomcat.css WEB-INF
bg-button.png bg-nav.png favicon.ico RELEASE-NOTES.txt tomcat.svg
[root@tomcat tomcat]#
2、新增虚拟主机
[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
新增虚拟主机,编辑server.xml在</Host>下面增加以下内容
<Host name="www.123.cn" appBase=""
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/123.cn/" debug="0" reloadable="true" crossContext="true"/>
</Host>
3、appBase部署Java应用
下载一个zrlog(Java写的blog站点应用,轻量),zrlog实际就是一个war包。
(下载地址:http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war)
[root@tomcat ~]# cd /usr/local/src/
[root@tomcat src]# ls
apache-tomcat-8.5.70.tar.gz jdk-8u301-linux-x64.tar.gz zrlog-1.7.1-baaecb9-release.war
[root@tomcat src]#
appBase支持自动解压,所以直接将war包移动到/usr/local/tomcat/webapps/目录下
[root@tomcat src]# mv zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/
[root@tomcat src]# cd /usr/local/tomcat/webapps/
[root@tomcat webapps]# ls
docs host-manager ROOT zrlog-1.7.1-baaecb9-release.war
examples manager zrlog-1.7.1-baaecb9-release
[root@tomcat webapps]#
重命名war包的文件名
[root@tomcat webapps]# mv zrlog-1.7.1-baaecb9-release zrlog
[root@tomcat webapps]# ls
docs examples host-manager manager ROOT zrlog zrlog-1.7.1-baaecb9-release.war
[root@tomcat webapps]#
用浏览器访问http://192.168.100.10/zrlog
[root@tomcat webapps]# yum install -y mariadb-server
[root@tomcat webapps]# systemctl start mariadb
[root@tomcat webapps]# netstat -ntlp
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:3306 0.0.0.0:* LISTEN 11951/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 937/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1091/master
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1507/java
tcp6 0 0 :::80 :::* LISTEN 1507/java
tcp6 0 0 :::22 :::* LISTEN 937/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1091/master
[root@tomcat webapps]#
配置密码,一个回车 ,yynyy
[root@tomcat webapps]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@tomcat webapps]#
[root@tomcat webapps]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
创建数据库
MariaDB [(none)]> create database zrlog;
Query OK, 1 row affected (0.00 sec)
创建zrlog用户并授权
MariaDB [(none)]> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@tomcat webapps]# systemctl restart mariadb
4. ocBase部署Java应用
创建docBase目录
[root@tomcat webapps]# mkdir -p /data/wwwroot/123.cn
将/usr/local/tomcat/webapps/zrlog 中的所有文件移动到/data/wwwroot/123.cn/目录下
[root@tomcat webapps]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/123.cn/
[root@tomcat webapps]# vim /usr/local/tomcat/conf/server.xml
<Host name="www.123.cn" appBase=""
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/data/wwwroot/123.cn/" debug="0" reloadable="true" crossContext="true"/>
</Host>
接下来在Windows下绑定hosts文件
hosts文件路径:C:\Windows\System32\drivers\etc
在文件下面添加
192.168.100.10 www.123.cn
docBase这个参数用来定义网站的文件存放路径。如果不定义,默认是在appBase/ROOT下面。定义了docBase就以该目录为主了,其中appBase和docBase可以一样。在这一步操作过程中很多人会遇到过访问404的问题,其实就是docBase没有定义对。
appBase为应用存放目录(实际上是一个相对路径,相对于 /usr/local/tomcat/ 路径),通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录。
搭建了一个Tomcat,想要使用Tomcat去跑一个网站。首先应用不能是一个传统所谓的目录(Apache、Nginx访问网站,首先需要指定一个目录,目录里存放着PHP文件或者是Html的文件,然后去访问),Tomcat需要提供一个war的包,就是一个压缩包,这个压缩包里面包含着运行这个网站的一些文件,包括配置,js代码,数据库相关的等等,都需要打包成war这种文件,而这个文件需要放置到 webapps 里面。
测试
重启服务,然后等8005端口启动
[root@tomcat 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
[root@tomcat 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@tomcat webapps]# netstat -ntlp |grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 12536/java
tcp6 0 0 127.0.0.1:47145 :::* LISTEN 12556/java
tcp6 0 0 127.0.0.1:27145 :::* LISTEN 12556/java
tcp6 0 0 :::80 :::* LISTEN 12536/java
tcp6 0 0 127.0.0.1:57145 :::* LISTEN 12556/java
[root@tomcat webapps]#
重启Tomcat服务完成后,打开浏览器在地址栏输入www.123.cn,就可以看到zrlog的首页。
任务6 Tomcat日志
1、查看日志
[root@tomcat webapps]# ls /usr/local/tomcat/logs/
catalina.2021-08-22.log localhost.2021-08-22.log zrlog
catalina.out localhost_access_log.2021-08-22.txt
host-manager.2021-08-22.log manager.2021-08-22.log
[root@tomcat webapps]#
catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。
catalina.2021-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
localhost和localhost-access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access字样的为 默认虚拟主机的错误日志。
2、配置生成日志
由于日志默认不会生成,需要在server.xml中配置一下。
配置方法就是在对应虚拟主机的
[root@tomcat webapps]# vim /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="www.123.cn_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
valve为日志文件配置;
prefix定义访问日志的前缀;
suffix定义日志的后缀;
pattern定义日志格式。
重启服务
[root@tomcat 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看是否有日志生成
[root@tomcat webapps]# ls /usr/local/tomcat/logs/
catalina.2021-08-22.log localhost_access_log.2021-08-22.txt
catalina.out manager.2021-08-22.log
host-manager.2021-08-22.log www.123.cn_access_log.2021-08-22.txt
localhost.2021-08-22.log zrlog
[root@tomcat webapps]#
查看日志多出来就是我们这个虚拟主机的访问日志