搭建配置Tomcat服务

tech2022-09-12  109

一、搭建配置Tomcat服务 Tomcat介绍 1.关闭防火墙并设置开机不自启,配置SElinux规则。 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# 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@localhost ~]# setenforce 0 2. 修改主机名 使用hostnamectl命令修改主机名 [root@localhost ~]# hostnamectl set-hostname tomcat 3.下载apache-tomcat和jdk到/usr/local/src下 4.解压 tar -zxvf jdk-8u77-linux-x64.tar.gz 5.创建目录 mkdir /usr/local/jdk1.8 移动jdk1.8.0_77下的所有到/usr/local/jdk1.8 mv jdk1.8.0_77/* /usr/local/jdk1.8/ 6. 修改环境变量 [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 7.使文件生效 source /etc/profile 8.测试 java –version which java 安装Tomcat

1.解压 tar -zxvf apache-tomcat-8.5.55.tar.gz 2.移动 mv apache-tomcat-8.5.55 /usr/local/tomcat 3.启动tomcat /usr/local/tomcat/bin/startup.sh 4.查看端口 [root@tomcat src]# netstat -lnpt | grep java tcp6 0 0 :::8009 ::😗 LISTEN 49228/java tcp6 0 0 :::8080 ::😗 LISTEN 49228/java tcp6 0 0 127.0.0.1:8005 ::😗 LISTEN 49228/java [root@tomcat src]# 三个端口8009、8005和8080的意义: 8080为提供Web服务的端口; 8005为管理端口; 8009端口为第三方服务调用的端口,比如httpd和Tomcat结合时会用到。 5.浏览器输入Ip地址+端口号 http://192.168.100.6:8080

6.修改配置文件[root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml …

8080直接改成80 7.关闭服务 /usr/local/tomcat/bin/shutdown.sh 8.再开启 /usr/local/tomcat/bin/startup.sh 9.浏览器访问 http://192.168.100.6 配置虚拟主机 1.编辑Tomcat配置文件 [root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml …

… 和之间的配置为虚拟主机配置部分: name定义域名; appBase定义应用的目录; unpackWARs为是否自动解压war包; autoDeploy 如果此项设置为true,表示Tomcat服务处于运行状态,能够检测appbase下的文件,如果有新的Web应用加入进来,会自动发布这个Web应用 Appbase部署java应用2. 新增虚拟主机 2.新增虚拟主机,编辑server.xml在下面增加以下内容: [root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml …

Appbase部署java应用 1.下载war包到/usr/local/src wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war 2.复制 tomcatcp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/ cd到/webapps下 cd /usr/local/tomcat/webapps/ 3.改名 mv zrlog-1.7.1-baaecb9-release zrlog 4.用浏览器访问,这里需要数据库 http://192.168.100.6 /zrlog

1.安装数据库 yum -y install mariadb mariadb-server 2.启动数据库 systemctl start mariadb 3.初始化数据库 mysql_secure_installation 4.进入数据库 Mysql –uroot –p123456 5.创建zrlog数据库 mysql> create database zrlog; 6.授权创建zrlog用户 mysql> grant all on zrlog.* to ‘zrlog’@127.0.0.1 identified by ‘000000’; 7.退出数据库 mysql>exit 8.然后再网站上输入创建好的数据库用户名和密码和邮箱

Tomcat日志

查看日志 [root@tomcat ~]# ls /usr/local/tomcat/logs/ catalina.2020-03-29.log localhost.2020-03-29.log catalina.out localhost_access_log.2020-03-29.txt host-manager.2020-03-29.log manager.2020-03-29.log [root@tomcat ~]#配置生成日志 [root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml …

… [root@tomcat ~]# valve为日志文件配置; prefix定义访问日志的前缀; suffix定义日志的后缀; pattern定义日志格式。 3.配置完成后,重启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 [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. [root@tomcat ~]# 4.重启Tomcat服务完成后,访问网站,查看/usr/local/tomcat/logs目录下是否有日志生成,并且查看生成的日志信息,命令如下: [root@tomcat ~]# ls /usr/local/tomcat/logs/ 123_access_log.2020-03-29.txt localhost.2020-03-29.log catalina.2020-03-29.log localhost_access_log.2020-03-29.txt catalina.out manager.2020-03-29.log host-manager.2020-03-29.log [root@tomcat ~]# cat /usr/local/tomcat/logs/123_access_log.2020-03-29.txt 192.168.174.1 - - [29/Mar/2020:22:06:16 -0700] “GET / HTTP/1.1” 200 8645 192.168.174.1 - - [29/Mar/2020:22:06:16 -0700] “GET //favicon.ico HTTP/1.1” 200 9662 [root@tomcat ~]# 至此,Tomcat搭建配置完成

二、主从同步服务 基础环境安装 1.修改主机名 mysql1节点: [root@localhost ~]# hostnamectl set-hostname mysql1 [root@localhost ~]# logout [root@mysql1 ~]# hostnamectl Static hostname: mysql1 Icon name: computer-vm Chassis: vm Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa Boot ID: 69ad020d53e54892b9005f82e182c140 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64 mysql2节点: [root@localhost ~]# hostnamectl set-hostname mysql2 [root@localhost ~]# logout [root@mysql2 ~]# hostnamectl Static hostname: mysql2 Icon name: computer-vm Chassis: vm Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa Boot ID: 816b270a1275496caa3254300fc359c4 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64 2.关闭防火墙及SELinux服务 两个节点关闭防火墙firewalld及SELinux服务,命令如下:

setenforce 0

systemctl stop firewalld

3.配置hosts文件 两个节点配置/etc/hosts文件,修改为如下: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.200.6 mysql1 192.168.200.7 mysql2 4.配置YUM源并安装数据库服务

yum install -y mariadb mariadb-server

systemctl start mariadb

systemctl enable mariadb

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. 初始化数据库并配置主从服务 1.初始化数据库 [root@mysql1 ~]# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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: #输入数据库root密码000000 Re-enter new password: #再次输入密码000000 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! 2.配置mysql1主节点 [root@mysql1 ~]# cat /etc/my.cnf [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步mysql系统数据库 server_id = 6 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.100.6,server_id就写6 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

Settings user and group are ignored when systemd is used.

If you need to run mysqld under a different user or group,

customize your systemd unit file for mariadb according to the

instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid

include all files from the config directory

!includedir /etc/my.cnf.d 重启数据库服务,并进入数据库,命令如下: [root@mysql1 ~]# systemctl restart mariadb [root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> 在mysql1节点,授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限。命令如下: MariaDB [(none)]> grant all privileges on . to root@’%’ identified by “000000”; Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on . to ‘user’@‘mysql2’ identified by ‘000000’; Query OK, 0 rows affected (0.00 sec) 3.配置mysql2从节点 修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容。 [root@mysql2 ~]# cat /etc/my.cnf [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步mysql系统数据库 server_id = 7 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.40,server_id就写7 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

Settings user and group are ignored when systemd is used.

If you need to run mysqld under a different user or group,

customize your systemd unit file for mariadb according to the

instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid

include all files from the config directory

!includedir /etc/my.cnf.d 在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下: [root@mysql2 ~]# systemctl restart mariadb [root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 5.5.44-MariaDB MariaDB Server

Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> change master to master_host=‘mysql1’,master_user=‘user’,master_password=‘000000’; Query OK, 0 rows affected (0.01 sec) 配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。命令如下: MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: mysql1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 245 Relay_Log_File: mariadb-relay-bin.000005 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 245 Relay_Log_Space: 1256 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 30 1 row in set (0.00 sec) 可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。 验证数据库主从服务 1.主节点创建数据库 先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下: [root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> create database test; Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use test; Database changed MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> insert into company values(1,“alibaba”,“china”); Query OK, 1 row affected (0.01 sec)

MariaDB [test]> select * from company; ±—±--------±------+ | id | name | addr | ±—±--------±------+ | 1 | alibaba | china | ±—±--------±------+ 1 row in set (0.00 sec) 2.从节点验证复制功能 登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能,命令如下: [root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright © 2000, 2015, 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 | | mysql | | performance_schema | | test | ±-------------------+ 4 rows in set (0.00 sec)

MariaDB [(none)]> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

Database changed MariaDB [test]> show tables; ±---------------+ | Tables_in_test | ±---------------+ | company | ±---------------+ 1 row in set (0.00 sec)

MariaDB [test]> select * from company; ±—±--------±------+ | id | name | addr | ±—±--------±------+ | 1 | alibaba | china | ±—±--------±------+ 1 row in set (0.00 sec) 可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

最新回复(0)