简介:
Tomcat是Apache软件基金会( Apache Software Foundation )的Jakarta项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成,是目前比较流行的Web应用服务器。
Tomcat服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,是开发和调试JSP程序的首选。
Java程序写的网站用Tomcat+JDK来运行。
Tomcat是一个中间件,真正起作用的,解析Java脚本的是JDK。
JDK(Java Development Kit)是整个Java的核心,它包含了Java运行环境和一堆Java相关的工具以及Java基础库。
环境:单节点部署
ip及主机名 | 镜像 |
---|---|
192.168.200.66;tomcat | CentOS-7-x86_64-DVD-1511.iso |
一、修改主机名并关闭防火墙和selinux
hostnamectl set-hostname tomcat
systemctl stop firewalld && systemctl disable firewalld && setenforce 0
二、jdk安装
http://www.oracle. com/echnetwork/java/javase/downloads/jdk8-downloads-
2133151.html //下载到本机,上传至虚拟机的/usr/local/src目录
[root@tomcat ~]# cd /usr/local/src/
[root@tomcat src]# tar -zxf jdk-8u211-linux-x64.tar.gz //将jdk包解压
[root@tomcat src]# mv jdk1.8.0_211/ /usr/local/jdk1.8 //移动并改名
[root@tomcat src]# vi /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 //生效配置文件
[root@tomcat src]# java -version //检查jdk是否安装成功
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)
如果系统已经安装jdk,我们需要把原来的Java目录重命名为java_bak,然后生效环境变量。
[root@tomcat src]# mv /usr/bin/java /usr/bin/java_bak
[root@tomcat src]# source /etc/profile
[root@tomcat src]# java -version
三、安装tomcat
http://tomcat.apache.org/
//官方下载tomcat的包下载到本机,上传至虚拟机的/usr/local/src目录
[root@tomcat ~]# cd /usr/local/src/
[root@tomcat src]# tar -zxf apache-tomcat-8.5.63.tar.gz //将tomcat包解压
[root@tomcat src]# mv apache-tomcat-8.5.63/ /usr/local/tomcat //移动并改名
[root@tomcat src]# cd /usr/local/tomcat/
[root@tomcat tomcat]# ./bin/startup.sh //启动tomacat
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.
//注意:tomcat不支持restart,要想重启,需要先shutdown,然后在start。
[root@tomcat tomcat]# netstat -ntpl //查看端口
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:22 0.0.0.0:* LISTEN 1455/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1968/master
tcp6 0 0 :::8080 //为提供Web服务的端口; :::* LISTEN 18755/java
tcp6 0 0 :::22 :::* LISTEN 1455/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1968/master
tcp6 0 0 127.0.0.1:8005 // 8005为管理端口; :::* LISTEN 18755/java
访问网页:
192.168.200.66:8080
四、配置Tomcat监听80端口
[root@tomcat tomcat]# vi /usr/local/tomcat/conf/server.xml
[root@tomcat 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 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 tomcat]# netstat -tnpl //检查端口是否为80
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:22 0.0.0.0:* LISTEN 1455/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1968/master
tcp6 0 0 :::80 :::* LISTEN 11341/java
tcp6 0 0 :::22 :::* LISTEN 1455/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1968/master
使用浏览器输入IP:192.168.200.66
直接访问
如果不能成功访问请先关闭nginx服务,然后在重启tomcat。
[root@tomcat tomcat]# systemctl stop nginx
[root@tomcat 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 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.
五、配置Tomcat虚拟主机
[root@tomcat tomcat]# vi /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 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>
其中< Host >和< /Host >之间的配置为虚拟主机配置部分:
name:定义域名;
appBase:定义应用的目录;
unpackWARs:是否自动解压war包;
autoDeploy :如果此项设置为true,表示Tomcat服务处于运行状态,能够检测appbase下的文件,如果有新的Web应用加入进来,会自动发布这个Web应用。
(一)appBase部署java应用
[root@tomcat tomcat]# cd /usr/local/src/
[root@tomcat src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
[root@tomcat src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/ //将war包拷贝到/usr/local/tomcat/webapps/目录下之后,包会自动解压。(前提是tomcat正常启动。
[root@tomcat src]# ll /usr/local/tomcat/webapps/
total 6972
drwxr-x---. 15 root root 4096 Mar 15 05:15 docs
drwxr-x---. 7 root root 93 Mar 15 05:15 examples
drwxr-x---. 6 root root 74 Mar 15 05:15 host-manager
drwxr-x---. 6 root root 4096 Mar 15 05:15 manager
drwxr-x---. 3 root root 4096 Mar 15 05:15 ROOT
drwxr-x---. 9 root root 4096 Mar 15 05:51 zrlog-1.7.1-baaecb9-release
-rw-r--r--. 1 root root 7122027 Mar 15 05:51 zrlog-1.7.1-baaecb9-release.war
[root@tomcat src]# cd /usr/local/tomcat/webapps/
[root@tomcat webapps]# mv zrlog-1.7.1-baaecb9-release zrlog //重命名zrlog-1.7.1-baaecb9-release
[root@tomcat webapps]# ll //一但重命名或删除war包解压后的包的文件时,war包就会在解压出一个文件夹。
drwxr-x---. 15 root root 4096 Mar 15 05:15 docs
drwxr-x---. 7 root root 93 Mar 15 05:15 examples
drwxr-x---. 6 root root 74 Mar 15 05:15 host-manager
drwxr-x---. 6 root root 4096 Mar 15 05:15 manager
drwxr-x---. 3 root root 4096 Mar 15 05:15 ROOT
drwxr-x---. 9 root root 4096 Mar 15 05:51 zrlog
drwxr-x---. 9 root root 4096 Mar 15 05:54 zrlog-1.7.1-baaecb9-release
-rw-r--r--. 1 root root 7122027 Mar 15 05:51 zrlog-1.7.1-baaecb9-release.war
[root@tomcat webapps]# rm -rf zrlog-1.7.1-baaecb9-release.war //将war包删除
安装数据库并配置(已经安装的直接配置)
yum install mariadb mariadb-server -y
systemctl start mariadb
[root@tomcat ~]# mysqladmin -uroot -p password 000000
Enter password: //回车
[root@tomcat ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
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; //创建zrlog数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on zrlog.* to 'zrlog'@localhost identified by '000000'; //创建zrlog用户,并授权
Query OK, 0 rows affected (0.00 sec)
使用浏览器访问
192.168.200.66/zrlog
,进行安装向导。
数据库密码为刚才设置的密码:
000000
,系统游戏为自定义邮箱:tomcat@163.com
,输入完成单击下一步。
管理员密码:
123456
,网站标题和网站子标题自定义。输入完成,单击下一步,
安装完成,单击
点击查看
进入zrlog管理页面。
点击
管理
,完成登录。
点击
文章撰写
,自定义输入内容并保存
。
在主页面可以看到刚才的内容。
(二)ocBase部署java应用
[root@tomcat ~]# mkdir -p /data/wwwroot/123.cn/ //创建docBase目录
[root@tomcat ~]# mv /usr/local/tomcat/webapps/zrlog/* /data/wwwroot/123.cn/
在windos的hosts文件中添加
192.168.200.66 www.123.cn
写入后,ping www.123.cn测试
重启tomcat
[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.
使用浏览器访问
www.123.cn
六、tomcat日志
catalina开头的日志:为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。
catalina.2020-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。
host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。
localhost和localhost-access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access字样的为默认虚拟主机的错误日志。
[root@tomcat ~]# ll /usr/local/tomcat/logs //Tomcat日志存放在/usr/local/tomcat/logs/目录下
total 120
-rw-r-----. 1 root root 31787 Mar 15 06:32 catalina.2021-03-15.log
-rw-r-----. 1 root root 46651 Mar 15 06:32 catalina.out
-rw-r-----. 1 root root 0 Mar 15 05:15 host-manager.2021-03-15.log
-rw-r-----. 1 root root 5583 Mar 15 06:32 localhost.2021-03-15.log
-rw-r-----. 1 root root 10991 Mar 15 06:28 localhost_access_log.2021-03-15.txt
-rw-r-----. 1 root root 0 Mar 15 05:15 manager.2021-03-15.log
-rw-r-----. 1 root root 8931 Mar 15 06:27 zrlog
配置生成日志
[root@tomcat ~]# vi /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"/>
在以上内容之后添加
<Valve className="org.apache.catalina.valves.AccessLogValve"directory="logs"
prefix="123.cn_access_log" suffix=".txt"
pattern="%h %l %u %t "%r"%s %b"/>
··········
valve:为日志文件配置;
prefix:定义访问日志的前缀;
suffix:定义日志的后缀;
pattern:定义日志格式。
重启tomcat服务
[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 ~]# ll /usr/local/tomcat/logs/
total 228
-rw-r-----. 1 root root 72 Mar 15 06:51 123.cn_access_log.2021-03-15.txt
-rw-r-----. 1 root root 55917 Mar 15 06:48 catalina.2021-03-15.log
-rw-r-----. 1 root root 82917 Mar 15 06:51 catalina.out
-rw-r-----. 1 root root 0 Mar 15 05:15 host-manager.2021-03-15.log
-rw-r-----. 1 root root 6318 Mar 15 06:48 localhost.2021-03-15.log
-rw-r-----. 1 root root 10991 Mar 15 06:28 localhost_access_log.2021-03-15.txt
-rw-r-----. 1 root root 0 Mar 15 05:15 manager.2021-03-15.log
-rw-r-----. 1 root root 8931 Mar 15 06:27 zrlog
[root@tomcat ~]# cat /usr/local/tomcat/logs/123.cn_access_log.2021-03-15.txt
192.168.200.1 - - [15/Mar/2021:06:51:19 +0800] "GET / HTTP/1.1"200 863