赞
踩
前言:本章整理tomcat的知识点,tomcat知识点相较nginx比较少,但是也是运维必会的软件,这里结合实际项目整理一下。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当公司运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
- [root@localhost ~]# tree /usr/local/tomcat/ -L 1
- /usr/local/tomcat/
- ├── bin #存放tomcat管理脚本
- ├── conf # tomcat 配置文件存放目录
- ├── lib # web应用调用的jar包存放路径
- ├── LICENSE
- ├── logs # tomcat 日志存放目录,catalina.out 为主要输出日志
- ├── NOTICE
- ├── RELEASE-NOTES
- ├── RUNNING.txt
- ├── temp # 存放临时文件
- ├── webapps # web程序存放目录
- └── work # 存放编译产生的.java 与 .class文件
1.2、webapps目录介绍
- [root@localhost ~]# tree /usr/local/tomcat/webapps/ -L 1
- ├── docs # tomcat 帮助文档
- ├── examples # web应用示例
- ├── host-manager # 主机管理
- ├── manager # 管理
- └── ROOT # 默认网站的主目录(主页)
1.3、Tomcat配置文件目录介绍
- [root@localhost ~]# tree /usr/local/tomcat/conf/ -L 1
- /usr/local/tomcat/conf/
- ├── Catalina
- ├── catalina.policy
- ├── catalina.properties
- ├── context.xml
- ├── logging.properties
- ├── server.xml #tomcat 主配置文件
- ├── tomcat-users.xml # tomcat 管理用户配置文件
- └── web.xml
有几个常见的问题,在这里列出:
(1)讲述一下Tomcat8005、8009、8080三个端口的含义?
8005:关闭时使用
8009:为AJP端口,即容器使用,如Apache能通过AJP协议访问Tomcat的8009端口
8080:一般应用使用
(2)tomcat服务于java语言,Java语言具有跨平台特性,JAVA 如何做到跨平台
同一个JAVA程序(JAVA字节码的集合),通过JAVA虚拟机(JVM)运行于各大主流操作系统平台比如Windows、CentOS、Ubuntu等。程序以虚拟机为中介,来实现跨平台。
(3)什么是JAVA虚拟机
所谓虚拟机,就是一台虚拟的计算机。他是一款软件,用来执行一系列虚拟计算机指令。大体上,虚拟机可以分为系统虚拟机和程序虚拟机。大名鼎鼎的VisualBox、VMware就属于系统虚拟机。他们完全是对物理计算机的仿真。提供了一个可以运行完整操作系统的软件平台。 程序虚拟机的典型代表就是Java虚拟机,它专门为执行单个计算机程序而设计,在Java虚拟机中执行的指令我们称为Java字节码指令。无论是系统虚拟机还是程序虚拟机,在上面运行的软件都呗限制于虚拟机提供的资源中。
tomcat下载地址http://tomcat.apache.org/
jdk下载地址:Java 存档下载 — Java SE 8 | Oracle 中国
- [root@localhost ~]# tar xf jdk-8u151-linux-x64.tar.gz -C /usr/local
- [root@localhost ~]# ls /usr/local/
- bin etc games include jdk1.8.0_151 lib lib64 libexec sbin share src
- [root@localhost ~]# ln -s /usr/local/jdk1.8.0_151/ /usr/local/java
- [root@localhost ~]# echo 'JAVA_HOME=/usr/local/java' >> /etc/profile
- [root@localhost ~]# echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
- [root@localhost ~]# echo 'export JAVA_HOME PATH' >> /etc/profile
- [root@localhost ~]# tail -3 /etc/profile
- JAVA_HOME=/usr/local/java
- PATH=$JAVA_HOME/bin:$PATH
- export JAVA_HOME PATH
- [root@localhost ~]# source /etc/profile
- [root@localhost ~]# env | grep JAVA
- JAVA_HOME=/usr/local/java
- [root@localhost ~]# java -version
- java version "1.8.0_151"
- Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
- Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
- [root@localhost ~]# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/
- [root@localhost ~]# ln -s /usr/local/apache-tomcat-7.0.42/ /usr/local/tomcat
- [root@localhost ~]# echo 'CATALINA_HOME=/usr/local/tomcat' >> /etc/profile
- [root@localhost ~]# echo 'export CATALINA_HOME' >> /etc/profile
- [root@localhost ~]# source /etc/profile
- [root@localhost ~]# systemctl stop firewalld
- [root@localhost ~]# 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@localhost ~]# setenforce 0
-
- [root@localhost ~]# vi /etc/selinux/config
- [root@localhost ~]# cat /etc/selinux/config
-
-
- SELINUX=disabled
- [root@localhost ~]# bash /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/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
-
- [root@localhost ~]# netstat -tnlp |grep java
- tcp6 0 0 :::8009 :::* LISTEN 9889/java
- tcp6 0 0 :::8080 :::* LISTEN 9889/java
- tcp6 0 0 127.0.0.1:8005 :::* LISTEN 9889/java
使用浏览器访问
- [root@localhost ~]# bash /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/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
继续上面的环境
- [root@localhost ~]# yum install -y mariadb-server mariadb
- [root@localhost ~]# systemctl start mariadb
-
- [root@localhost ~]# mysqladmin -u root password 123 'Liumuquan@123'
-
-
- create database jspgou character set = utf8;
- Query OK, 1 row affected, 1 warning (0.01 sec)
- [root@localhost ~]# unzip jspgouV6-ROOT.zip
- [root@localhost ~]# vim ROOT/WEB-INF/config/jdbc.properties
- # 修改后台数据库连接参数,如下图
- [root@localhost ~]# mysql -uroot -pLiumuquan@123 -D jspgou < DB/jspgou.sql
- mysql: [Warning] Using a password on the command line interface can be insecure.
这里会有一个问题
自带的数据库,datatime字段默认值无效
可以通过如下办法排错
- # 进入数据库
-
- mysql> show session variables like '%sql_mode%';
- +---------------+-----------------------------------------------------------------------------------------------------------------------+
- | Variable_name | Value |
- +---------------+-----------------------------------------------------------------------------------------------------------------------+
- | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
- +---------------+-----------------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
-
- #将sql_mode复制出来,删除其中NO_ZERO_IN_DATE,NO_ZERO_DATE两个字段
-
- #退出数据库
- vim my.cnf
- 添加
- sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
-
- 重启数据库重新导入
- [root@localhost ~]# \cp -r ROOT /usr/local/tomcat/webapps/
-
-
- \cp用ROOt替换webapps下的ROOT
- [root@localhost ~]# bash /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/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
使用浏览器访问
这个位置给看文档的说一下,系统环境为centos7,这个网站直接部署在centos9上会出现无法连接数据库的问题。
Tomcat多实例是指在同一台服务器上运行多个独立的Tomcat服务器实例。每个Tomcat实例都有自己的配置、日志、应用程序和端口号,它们彼此独立,互不影响。这种部署方式通常用于在同一台服务器上运行多个独立的Web应用程序,每个应用程序使用独立的Tomcat实例来提供服务。
继续使用上面的环境,演示如下:
- [root@localhost ~]# bash /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/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- [root@localhost ~]# mkdir /usr/local/tomcat/instance{1..3}
- [root@localhost ~]# ls /usr/local/tomcat/
- bin instance1 instance3 LICENSE NOTICE RUNNING.txt webapps
- conf instance2 lib logs RELEASE-NOTES temp work
- [root@localhost ~]# cp -r /usr/local/tomcat/{conf,logs,temp,work} /usr/local/tomcat/instance1
- [root@localhost ~]# cp -r /usr/local/tomcat/{conf,logs,temp,work} /usr/local/tomcat/instance2
- [root@localhost ~]# cp -r /usr/local/tomcat/{conf,logs,temp,work} /usr/local/tomcat/instance3
[root@localhost ~]# vi /usr/local/tomcat/instance1/conf/server.xml
修改这个文件三处地方,分别是服务端口,程序工作端口,网站目录
- #服务端口 8080 替换成 8081
-
- <Connector port="8081" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
-
-
- #程序工作端口:8005替换成 8091
-
- <Server port="8091" shutdown="SHUTDOWN">
-
-
- #网站目录:webapps 替换成另一个目录
- appBase="/webapps"
- #拷贝修改好的文件到其余两个实例,这里是为了偷懒不改主目录,实际中不同的实例主目录也不同
-
- [root@localhost ~]# cp /usr/local/tomcat/instance1/conf/server.xml /usr/local/tomcat/instance2/conf/
- cp:是否覆盖"/usr/local/tomcat/instance2/conf/server.xml"? yes
-
- 编辑拷过去的文件
- 服务端口 8081替换成 8082
- 程序工作端口:8091替换成 8092
-
-
-
-
- [root@localhost ~]# cp /usr/local/tomcat/instance1/conf/server.xml /usr/local/tomcat/instance3/conf/
- cp:是否覆盖"/usr/local/tomcat/instance3/conf/server.xml"? yes
-
- 编辑拷过去的文件
- 服务端口 8081替换成 8083
- 程序工作端口:8091替换成 8093
- [root@localhost ~]# vi /usr/local/tomcat/instance1/ins1.sh
- #!/bin/bash
- #instance1
- . /etc/init.d/functions
- export CATALINA_BASE="/usr/local/tomcat/instance1"
-
- case "$1" in
- start)
- $CATALINA_HOME/bin/startup.sh
- ;;
- stop)
- $CATALINA_HOME/bin/shutdown.sh
- ;;
- restart)
- $CATALINA_HOME/bin/shutdown.sh
- sleep 5
- $CATALINA_HOME/bin/startup.sh
- ;;
- esac
- export JAVA_OPTS='-Xms64m -Xmx128m'
-
-
- #2,3的脚本只需要修改export CATALINA_BASE="/usr/local/tomcat/instance?"
-
- #记得对启动脚本进行赋权
-
-
- [root@localhost ~]# chmod +x /usr/local/tomcat/instance1/ins1.sh
- [root@localhost ~]# chmod +x /usr/local/tomcat/instance2/ins2.sh
- [root@localhost ~]# chmod +x /usr/local/tomcat/instance3/ins3.sh
- [root@localhost ~]# mkdir /webapps
- [root@localhost ~]# cp -r /usr/local/tomcat/webapps/ROOT/ /webapps/
- [root@localhost ~]# /usr/local/tomcat/instance1/ins1.sh start
- Using CATALINA_BASE: /usr/local/tomcat/instance1
- Using CATALINA_HOME: /usr/local/tomcat
- Using CATALINA_TMPDIR: /usr/local/tomcat/instance1/temp
- Using JRE_HOME: /usr/local/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- [root@localhost ~]# /usr/local/tomcat/instance2/ins2.sh start
- Using CATALINA_BASE: /usr/local/tomcat/instance2
- Using CATALINA_HOME: /usr/local/tomcat
- Using CATALINA_TMPDIR: /usr/local/tomcat/instance2/temp
- Using JRE_HOME: /usr/local/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- [root@localhost ~]# /usr/local/tomcat/instance3/ins3.sh start
- Using CATALINA_BASE: /usr/local/tomcat/instance3
- Using CATALINA_HOME: /usr/local/tomcat
- Using CATALINA_TMPDIR: /usr/local/tomcat/instance3/temp
- Using JRE_HOME: /usr/local/java
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
使用浏览器访问不同端口
tomcat其实很多地方与nginx相同,只是一些细节有差别,概念类的可以去翻看nginx的三篇文章
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。