一、Tomcat 简介
Tomcat是Apache软件基金会(Apache Software Foundation)项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成。
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。
解析java代码: JAVA容器,WEB容器,WEB中间件:
Tomcat,JBOSS、resin、weblogic等 ---解析动态页面 Weblogic ---收费。
其他web容器如:
Python-------->uwsgi php----------->php
Tomcat和Nginx、Apache(httpd)、Web服务器一样,具有处理HTML页面的功能不过Tomcat处理静态HTML的能力不如Nginx/Apache服务器。
一个tomcat默认并发是200(官方),可以修改,但实际用的时候也就200并发左右。
https端口:443
使用方案:
建议使用Nginx和Tomcat配合,Nginx处理静态,Tomcat处理动态程序 方案三中后端Tomcat可以运行在单独的主机,也可以是同一台主机上的多实例
Tomcat官网: http://tomcat.apache.org
1、JDK
JDK是 Java 语言的软件开发工具包,JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。
JDK下载面页:
2、安装Tomcat & JDK
1.系统环境说明
[root@reece-tomcat ~]# getenforce
Disabled
[root@reece-tomcat ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
2 .安装JDK
上传jdk1.8到服务器。安装jdk
[root@reece-tomcat ~]# tar xzf jdk-8u191-linux-x64.tar.gz -C /usr/local/
[root@reece-tomcat ~]# cd /usr/local/
[root@reece-tomcat local]# mv jdk1.8.0_191/ java
设置环境变量:
[root@reece-tomcat local]# vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/java #指定java安装目录
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH #用于指定java系统查找命令的路径
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar #类的路径,在编译运行java程序时,如果有调用到其他类的时候,在classpath中寻找需要的类。
检测JDK是否安装成功:
[root@reece-tomcat local]# source /etc/profile.d/jdk.sh
[root@reece-tomcat local]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
3.安装Tomcat
[root@reece-tomcat ~]# mkdir /data/application -p
[root@reece-tomcat ~]# cd /usr/src/
[root@reece-tomcat src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz
[root@reece-tomcat src]# tar xzf apache-tomcat-8.5.49.tar.gz -C /data/application/
[root@reece-tomcat src]# cd /data/application/
[root@reece-tomcat application]# mv apache-tomcat-8.5.49/ tomcat
设置环境变量:
[root@reece-tomcat application]# vim /etc/profile
export TOMCAT_HOME=/data/application/tomcat #指定tomcat的安装目录
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$TOMCAT_HOME/bin #在jdk环境变量、PATH变量后添加:$TOMCAT_HOME/bin
[root@reece-tomcat application]# source /etc/profile
查看tomcat是否安装成功:
[root@reece-tomcat tomcat]# /data/application/tomcat/bin/version.sh
Using CATALINA_BASE: /data/application/tomcat
Using CATALINA_HOME: /data/application/tomcat
Using CATALINA_TMPDIR: /data/application/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /data/application/tomcat/bin/bootstrap.jar:/data/application/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.49
Server built: Nov 17 2019 18:45:30 UTC
Server number: 8.5.49.0
OS Name: Linux
OS Version: 3.10.0-514.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_211-b12
JVM Vendor: Oracle Corporation
二、Tomcat目录介绍
1、tomcat主目录介绍
[root@reece-tomcat ~]# cd /data/application/tomcat/
[root@reece-tomcat tomcat]# yum install -y tree
[root@reece-tomcat tomcat]# tree -L 1
.
├── bin #存放tomcat的管理脚本
├── BUILDING.txt
├── conf #tomcat的配置文件
├── CONTRIBUTING.md
├── lib #web应用调用的jar包存放路径
├── LICENSE
├── logs #tomcat日志存放目录,catalina.out日志为只要输出日志
├── NOTICE
├── README.md
├── RELEASE-NOTES
├── RUNNING.txt
├── temp #存放临时文件
├── webapps #默认网站发布目录
└── work #存放编译生产的.java与.class文件
7 directories, 7 files
2、webapps目录介绍
[root@reece-tomcat tomcat]# cd webapps/
[root@reece-tomcat webapps]# tree
.
├── docs #tomcat的帮助文档
├── examples #web应用实例
├── host-manager #主机管理
├── manager #管理
└── ROOT #默认站点根目录
5 directories, 0 files
3、Tomcat配置文件目录介绍(conf)
[root@reece-tomcat webapps]# cd ../conf/
[root@reece-tomcat conf]# tree
.
├── Catalina
├── catalina.policy
├── catalina.properties
├── context.xml
├── logging.properties
├── logs
├── server.xml # tomcat 主配置文件
├── server.xml.bak
├── server.xml.bak2
├── tomcat-users.xml # tomcat 管理用户配置文件
├── tomcat-users.xsd
└── web.xml
2 directories, 10 files
4、Tomcat的管理
启动程序 #/data/application/tomcat/bin/startup.sh #启动 关闭程序 #/data/application/tomcat/bin/shutdown.sh #关闭
启动
[root@reece-tomcat conf]# cd ../bin/
[root@reece-tomcat bin]# ./startup.sh
Using CATALINA_BASE: /data/application/tomcat
Using CATALINA_HOME: /data/application/tomcat
Using CATALINA_TMPDIR: /data/application/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /data/application/tomcat/bin/bootstrap.jar:/data/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
注意:tomcat未启动的情况下使用shutdown脚本,会有大量的输出信息。
检查tomcat是否启动正常
[root@reece-tomcat bin]# netstat -lntp |grep java
tcp6 0 0 :::8080 :::* LISTEN 30560/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 30560/java
tcp6 0 0 :::8009 :::* LISTEN 30560/java
端口:
8005:这个端口负责监听关闭Tomcat的请求 shutdown:向以上端口发送的关闭服务器的命令字符串。
8009: 与其他http服务通信接口。
8080: 建立http也就是客户端访问连接用。可以修改
启动完成浏览器进行访问
查看日志
[root@reece-tomcat bin]# tail -f /data/application/tomcat/logs/catalina.out
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/application/tomcat/webapps/host-manager] has finished in [21] ms
04-Jul-2019 22:40:00.026 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/data/application/tomcat/webapps/manager]
04-Jul-2019 22:40:00.042 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/application/tomcat/webapps/manager] has finished in [16] ms
04-Jul-2019 22:40:00.048 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
04-Jul-2019 22:40:00.058 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
04-Jul-2019 22:40:00.062 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 479 ms
扩展:解决Tomcat启动慢的方法
Tomcat启动慢主要原因是生成随机数的时候卡住了,导致tomcat启动不了。
yum install rng-tools # 安装rngd服务(熵服务,增大熵池)
systemctl start rngd # 启动服务
5、tomcat 配置管理页面(了解)
[root@reece-tomcat ~]# vim /data/application/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="user" password="password" roles="tomcat,manager-gui,manager-script,admin-gui,admin-script"/>
[root@reece-tomcat ~]# vim /data/application/tomcat/webapps/host-manager/META-INF/context.xml
[root@reece-tomcat ~]# vim /data/application/tomcat/webapps/manager/META-INF/context.xml
# 内容均修改为如下图片,然后重启tomcat
6、server.xml配置文件
<?xml version='1.0' encoding='utf-8'?>
<!--
<Server>
port指定Tomcat监听shutdown命令端口
shutdown指定终止Tomcat服务器运行。
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<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>
<Service name="Catalina">
<!-- Connector主要参数说明(见下面) -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<!-- 详情常见(host参数详解)-->
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="" debug=""/>
<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>
7、Connector主要参数说明
port:指定服务器端要创建的端口号,并在这个端口监听来自客户端的请求。
protocol:连接器使用的协议,支持HTTP和AJP。AJP(Apache Jserv Protocol)专用于tomcat与apache建立通信的.
redirectPort:指定服务器正在处理http请求时收到了一个SSL传输请求后重定向的端口号
maxThreads:接收最大请求的并发数
connectionTimeout 指定超时的时间数(以毫秒为单位)
host参数详解
host:表示一个虚拟主机
name:指定主机名
appBase:应用程序基本目录,即存放应用程序的目录.一般为appBase="webapps",相对于CATALINA_HOME而言的,也可以写绝对路径。
unpackWARs:如果为true,则tomcat会自动将WAR文件解压,否则不解压,直接从WAR文件中运行应用程序
autoDeploy:在tomcat启动时,是否自动部署
三、WEB站点部署
上线的代码有两种方式:
第一种方式是直接将程序目录放在webapps目录下面,这种方式大家已经明白了,就不多说了。
第二种方式是使用开发工具将程序打包成war包,然后上传到webapps目录下面。
1、使用war包部署web站点
[root@reece-tomcat ~]# pwd
/root
下载jenkins的war包
[root@reece-tomcat ~]# wget http://updates.jenkins-ci.org/download/war/2.129/jenkins.war
[root@reece-tomcat ~]# ls
jenkins.war
[root@reece-tomcat ~]# cd /data/application/tomcat #进入tomcat目录
[root@reece-tomcat tomcat]# cp -r webapps/ /opt/ #将原来的发布网站目录备份
[root@reece-tomcat tomcat]# cd webapps/
[root@jreece-tomcat webapps]# ls
docs examples host-manager manager ROOT
[root@reece-tomcat webapps]# rm -rf * #清空发布网站里面的内容
[root@reece-tomcat webapps]# cp /root/jenkins.war . #将war包拷贝到当前目录
[root@reece-tomcat webapps]# ../bin/startup.sh #启动
Using CATALINA_BASE: /data/application/tomcat
Using CATALINA_HOME: /data/application/tomcat
Using CATALINA_TMPDIR: /data/application/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /data/application/tomcat/bin/bootstrap.jar:/data/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@reece-tomcat webapps]# ls
jenkins jenkins.war
手动解压:
[root@reece-tomcat webapps]# ../bin/shutdown.sh #关闭tomcat
[root@reece-tomcat ~]# cd /data/application/tomcat/webapps/
[root@reece-tomcat webapps]# rm -rf *
[root@reece-tomcat webapps]# mkdir ROOT #创建一个ROOT目录存放war包
[root@reece-tomcat webapps]# ls
ROOT
[root@reece-tomcat webapps]# cd ROOT/
[root@reece-tomcat ROOT]# cp /root/jenkins.war .
[root@reece-tomcat ROOT]# unzip jenkins.war
浏览器访问:http://192.168.1.7:8080/jenkins
2、自定义默认网站目录
修改默认发布目录:
[root@reece-tomcat ~]# mkdir /data/application/webapp #创建发布目录
[root@reece-tomcat ~]# vim /data/application/tomcat/conf/server.xml
将原来的
修改为
[root@reece-tomcat ~]# cp /root/jenkins.war /data/application/webapp/
[root@reece-tomcat ~]# /data/application/tomcat/bin/startup.sh
Using CATALINA_BASE: /data/application/tomcat
Using CATALINA_HOME: /data/application/tomcat
Using CATALINA_TMPDIR: /data/application/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /data/application/tomcat/bin/bootstrap.jar:/data/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@reece-tomcat ~]# ll /data/application/webapp/ #已经自动解压
jenkins/ jenkins.war
3、部署开源站点(jspgou商城)
[root@reece-tomcat ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@reece-tomcat ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
[root@reece-tomcat ~]# cd /etc/yum.repos.d/
[root@reece-tomcat yum.repos.d]# vim mysql-community.repo
#注意enabled中0表示关闭,1表示开启
修改如下
安装
[root@reece-tomcat yum.repos.d]# yum -y install mysql-server mysql
[root@reece-tomcat yum.repos.d]# cd
[root@reece-tomcat ~]# systemctl start mysqld
[root@reece-tomcat ~]# systemctl enable mysqld
查找密码并修改密码
[root@reece-tomcat ~]# grep pass /var/log/mysqld.log #过滤查找密码
2019-07-05T15:57:15.294365Z 1 [Note] A temporary password is generated for root@localhost: rs4.tyYXo(M;
[root@reece-tomcat ~]# mysqladmin -p'rs4.tyYXo(M;' password 'Zh@123456' #修改密码
配置数据库
[root@reece-tomcat ~]# mysql -p'Zh@123456'
mysql: [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.7.26 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 jspgou default charset=utf8; #创建数据库设置字符集
Query OK, 1 row affected (0.00 sec)
mysql> \q
Bye
jspgou商城上线
上传jspgou商城的代码
[root@reece-tomcat ~]# unzip jspgouV6.1-ROOT.zip
[root@jreece-tomcat ~]# cp -r ROOT/ /data/application/tomcat/webapps/
[root@reece-tomcat ~]# cd /data/application/tomcat/webapps/
[root@reece-tomcat webapps]# ls
ROOT
[root@reece-tomcat webapps]# vim ROOT/WEB-INF/config/jdbc.properties
配置数据库连接----jdbc
将数据导入数据库:
[root@reece-tomcat ~]# cd DB/
[root@reece-tomcat DB]# ls
jspgou.sql
[root@reece-tomcat DB]# mysql p'Zh@123456' -D jspgou < jspgou.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1067 (42000) at line 97: Unknown error 1067
[root@reece-tomcat DB]# vim /etc/my.cnf ----添加sql_mod
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUB
explicit_defaults_for_timestamp=1
[root@reece-tomcat DB]# systemctl restart mysqld
[root@reece-tomcat DB]# mysql -p'Zh@123456' -D jspgou < jspgou.sql
启动tomcat访问:
[root@reece-tomcat ~]# /data/application/tomcat/bin/startup.sh
[root@reece-tomcat ~]# netstat -lntp #看端口是否起来