环境准备
规划节点,本次实验为单节点部署:
关闭防火墙并设置开机不自启,配置SElinux规则
[root@tomcat ~]# systemctl stop firewalld.service
[root@tomcat ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@tomcat ~]# setenforce 0
修改主机名
[root@tomcat ~]# hostnamectl set-hostname tomcat
// 修改完后Ctrl+D退出后重新连接
安装JDk
1.JDK的下载
官网下载地址:
http://www.oracle. com/echnetwork/java/javase/downloads/jdk8-downloads-
2133151.html
- JDK安装
进入到/usr/local/src/目录下,解压刚刚上传的jdk-8u211-linux-x64.tar.gz,并将解压后的文件移动到/usr/local/目录下改名为jdk1.8,然后使用ls命令查看下/usr/local/目录下是否有jdk1.8目录。
[root@tomcat src]# du -sh jdk
jdk1.8.0_271/ jdk-8u271-linux-x64.tar.gz
[root@tomcat src]# du -sh jdk1.8.0_271/
342M jdk1.8.0_271/
[root@tomcat src]# mv jdk1.8.0_271/ /usr/local/jdk1.8
3.修改环境变量
设置环境变量,编辑/etc/profile文件
[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 //使环境变量生效
- 检查JDK安装情况
①配置完成环境变量后,使用java -version命令检查是否安装成功,如果显示结果带有java version "1.8.0_271"字样和我们解压的jdk-8u271-linux-x64.tar.gz包的版本相对应,则证明安装成功。
[root@tomcat src]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
②可能不出现的不是上面的这种
[root@tomcat src]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
发现不是自己安装的JDK,这是因为系统自带OpenJDK或者以前安装过OpenJDK。使用witch命令,查看现在Java的所在目录:
[root@tomcat src]# which java
/usr/bin/java
如果结果为/usr/bin/java则说明这是系统自带的OpenJDK。这时,我们为了实验的一致性,把原来的Java目录重命名为java_bak,并使用source命令再次使环境变量生效,然后使用java -verison命令查看是否有java version "1.8.0_211"字样,命令如下:
[root@tomcat src]# mv /usr/bin/java /usr/bin/java_bak
[root@tomcat src]# source /etc/profile
[root@tomcat 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@tomcat src]#
安装Tomcat
- 安装Tomcat
官网下载地址:http://tomcat.apache.org/
使用Xftp上传到虚拟机/usr/local/src/目录下,然后解压,将解压后的文件移动到/usr/local目录下,并命名为tomcat
[root@tomcat ~]# cd /usr/local/src
[root@tomcat src]# ll
total 150032
-rw-r--r--. 1 root root 10488933 Mar 31 06:04 apache-tomcat-8.5.60.tar.gz
-rw-r--r--. 1 root root 143142634 Mar 31 04:48 jdk-8u271-linux-x64.tar.gz
[root@tomcat src]#
[root@tomcat src]# tar -zxvf apache-tomcat-8.5.60.tar.gz
[root@tomcat src]# ll
total 150036
drwxr-xr-x. 9 root root 4096 Mar 31 06:05 apache-tomcat-8.5.60
-rw-r--r--. 1 root root 10488933 Mar 31 06:04 apache-tomcat-8.5.60.tar.gz
-rw-r--r--. 1 root root 143142634 Mar 31 04:48 jdk-8u271-linux-x64.tar.gz
[root@tomcat src]# mv apache-tomcat-8.5.60 /usr/local/tomcat
- 启动Tomcat
使用/usr/local/tomcat/bin/startup.sh这个命令启动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.
- 测试
用netstat命令来监听java相关服务端口,查看是否有以下端口存在(8009、8080、8005),如果存在则证明Tomcat服务启动成功
[root@tomcat src]# yum install -y net-tools
[root@tomcat src]# netstat -ntpl |grep java
tcp6 0 0 :::8080 :::* LISTEN 10186/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 10186/java
然后打开浏览器,在地址栏中输入http://IP:8080/(这里的IP为你虚拟机的IP地址,此处IP为192.168.200.23),可以看到Tomcat的默认页面。如图所示:
配置Tomcat监听80端口
- 修改配置文件
编辑Tomcat配置文件server.xml
[root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml
修改完配置文件,接下来就是重启Tomcat服务,Tomcat服务是不支持restart的方式重启服务的,所以要想重启服务必须先关闭服务,使用命令/usr/local/tomcat/bin/shutdown.sh,然后再启动服务,使用命令/usr/local/tomcat/bin/startup.sh
[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.
- 测试
监听Java相关服务端口来查看是否启动成功
[root@tomcat ~]# netstat -ntpl | grep java
tcp6 0 0 :::80 :::* LISTEN 11123/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 11123/java
如果启动成功,则会出现三个端口(8009、8005、80),但我没有出现8009
但是这里可以看到,并没有80端口出现,这是因为这台机器我们之前安装过LNMP。Nginx的默认端口也是80,这就和我刚刚设置的Tomcat端口发生冲突。这时,我们需要关闭Nginx服务,重新启动Tomcat服务。
[root@tomcat ~]# service nginx stop
Stopping nginx (via systemctl): [ OK ]
//出现OK字样,Nginx服务关闭成功。
[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
[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
Tomcat started.
这时,我们再次使用netstat命令监听Java相关服务端口,看这次三个端口是否存在,命令结果如下:
[root@tomcat ~]# netstat -plnt | grep java
tcp6 0 0 :::8009 :::* LISTEN 47873/java
tcp6 0 0 :::80 :::* LISTEN 47873/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 47873/java
- 浏览器访问
打开浏览器在地址栏中输入192.168.200.23
配置Tomcat虚拟主机
- 查看默认虚拟主机
定义:Tomcat和Apache、Nginx一样,都支持虚拟主机配置。每个虚拟主机配置文件就是一台服务器,支持一个IP下可以访问多个域名,每个域名代表一个网站。所以一台服务器上可以配置多个网站。
名词解释:
- 新增虚拟主机
编辑Tomcat配置文件:
[root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml
…
- appBase部署Java应用
下载地址:http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
[root@tomcat ~]# cd /usr/local/src
[root@tomcat src]# ll
total 156988
-rw-r--r--. 1 root root 10488933 Mar 31 06:04 apache-tomcat-8.5.60.tar.gz
-rw-r--r--. 1 root root 143142634 Mar 31 04:48 jdk-8u271-linux-x64.tar.gz
-rw-r--r--. 1 root root 7122027 Mar 31 09:19 zrlog-1.7.1-baaecb9-release.war
[root@tomcat src]# ls |grep zrlog
zrlog-1.7.1-baaecb9-release.war
appBase支持自动解压,所以直接将war包拷贝到/usr/local/tomcat/webapps/目录下:
[root@tomcat src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/
[root@tomcat src]# ls /usr/local/tomcat/webapps/
docs manager zrlog-1.7.1-baaecb9-release.war
examples ROOT
host-manager zrlog-1.7.1-baaecb9-release
//将war包拷贝到/usr/local/tomcat/webapps/目录下之后,包会自动解压。
(前提是tomcat正常启动。
重命名war包的文件名:
[root@tomcat src]# cd /usr/local/tomcat/webapps/
[root@tomcat webapps]# mv zrlog-1.7.1-baaecb9-release zrlog
[root@tomcat webapps]# ls
docs host-manager ROOT zrlog-1.7.1-baaecb9-release.war
examples manager zrlog
[root@tomcat webapps]# ls
docs host-manager ROOT zrlog-1.7.1-baaecb9-release
examples manager zrlog zrlog-1.7.1-baaecb9-release.war
//一但重命名或删除war包解压后的包的文件时,war包就会在解压出一个文件夹。
用浏览器访问http://http://192.168.200.23/zrlog/
这是一个配置数据库的过程
MySQL的安装(我用的是yum源安装滴,也可用tar包):
[root@tomcat ~]# yum install -y mariadb mariadb-server
开启mariadb服务并初始化:
[root@tomcat ~]# systemctl start mariadb
[root@tomcat ~]# 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!
登录mysql:
[root@tomcat ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB Server
Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
创建zrlog数据库:
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 '000000';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
//退出数据库
检查创建用户是否可以登陆数据库,使用zrlog用户登录:
[root@tomcat ~]# mysql -u zrlog -h 127.0.0.1 -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| zrlog |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
使用 zrlog用户信息填写刚才在浏览器中打开的网页,Email填写自己的邮箱,本次是实验,填写内容为自定义邮箱(admin@163.com),单击“下一步”按钮
设置管理员账号(admin)和管理员密码(123456),网站标题和子标题按需填写,本次自定义内容(网站标题:“测试”,网站子标题:“linux”),如图所示:
填写完成后,单击“下一步”按钮,可以看到安装完成的界面
单击“点击查看”按钮,我们就可以进入搭建好的zrlog页面了
进入管理页面,写一些文章,单击上图主菜单栏中的“管理”按钮
输入安装向导里已经设置好的账户名和密码(admin:123456),单击“登录”按钮
单击“文章撰写”栏目,写上自己想写的内容,然后保存
然后回到主页面,你就可以看到刚才你写的内容
- ocBase部署Java应用
查看虚拟主机docBase的路径
[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"/>
</Host>
//docBase 定义的目录为:/data/wwwroot/123.cn
创建docBase目录
[root@tomcat ~]# cd /usr/local/tomcat/webapps/
[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/
Windows下绑定hosts:
1、按下win+R组合键调出运行窗口
2、在打开框中粘贴以下路径:C:\Windows\System32\drivers\etc按下Enter键确认
3、此时会跳转到指定路径文件下,如图所示
4、右键菜单,选择属性
5、在hosts属性窗口中,找到安全栏下的users用户名,然后点击编辑
6、在hosts的权限窗口,勾选users的修改和写入,点击应用即可
7、然后回到路径文件下,用记事本方式打开hosts进行修改保存即可
测试:
打开命令提示符(CMD),用ping命令ping www.123.cn 看IP是否为虚拟机IP,如果是的话,现在就可以访问了
接下来就用域名去访问zrlog页面,由于之前配置完虚拟主机后,并没有重启服务,这里要重启一下服务
[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.
重启Tomcat服务完成后,打开浏览器在地址栏输入www.123.cn,就可以看到zrlog的首页
Tomcat日志
- 查看日志
Tomcat在应用过程中,难免会出现错误,如何去查看这些错误,这就需要查看Tomcat的日志。Tomcat日志存放在/usr/local/tomcat/logs/目录下
[root@tomcat webapps]# cd
[root@tomcat ~]# ls /usr/local/tomcat/logs/
catalina.2021-03-31.log localhost.2021-03-31.log
catalina.out localhost_access_log.2021-03-31.txt
host-manager.2021-03-31.log manager.2021-03-31.log
名词解释:
- 配置生成日志
日志默认不会生成,需要在server.xml中配置一下。所以接下来配置新增虚拟主机的访问日志,具体方法是在对应虚拟主机的里面加入下面的配置(我们用www.123.cn来进行配置),编辑以下配置文件
[root@tomcat ~]# cd /usr/local/tomcat/logs/
[root@tomcat logs]# pwd
/usr/local/tomcat/logs
[root@tomcat logs]# ls
catalina.2021-03-31.log localhost.2021-03-31.log
catalina.out localhost_access_log.2021-03-31.txt
host-manager.2021-03-31.log manager.2021-03-31.log
[root@tomcat logs]# vi /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve."
directory="logs"
prefix="123.cn_access" suffix=".log"
pattern="%h %l %u %t "%r"%s %b"/>
名词解释:
配置完成后,重启Tomcat服务:
[root@tomcat logs]# cd ..
[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服务完成后,访问网站,查看/usr/local/tomcat/logs目录下是否有日志生成,并且查看生成的日志信息
[root@tomacat tomcat]# ls /usr/local/tomcat/logs/
catalina.2020-11-10.log localhost.2020-11-10.log
catalina.out localhost_access_log.2020-11-10.txt
host-manager.2020-11-10.log manager.2020-11-10.log
[root@tomacat tomcat]# cat /usr/local/tomcat/logs/localhost_access_log.2020-11-10.txt
192.168.200.1 - - [10/Nov/2020:02:08:41 +0800] "GET / HTTP/1.1" 200 11156
192.168.200.1 - - [10/Nov/2020:02:08:41 +0800] "GET /tomcat.css HTTP/1.1" 200 5542
192.168.200.1 - - [10/Nov/2020:02:08:41 +0800] "GET /tomcat.svg HTTP/1.1" 200 67795
192.168.200.1 - - [10/Nov/2020:02:08:41 +0800] "GET /bg-nav.png HTTP/1.1" 200 140
[root@tomcat ~]#
Tomcat搭建配置完成