中小型企业网络部署搭建项目(搭建WordPress)

项目需求

【项目名称】
搭建中小型企业网站平台
【项目说明】

设计一个中小型企业网站平台架构,可以满足公司日常一天10-30万PV访问量。项目中要包括安全,备份,监控,shell脚本自动化管理等内容。
【项目考核技能点】

  1. 画好架构图, 不限制用什么工具画图、visio、亿图、xmind、在线画图processon等

  2. LAMP或LNMP架构

  3. 在Web架构上部署网站模版。如:discuz论坛或ecshop商城或wordpress博客等。

  4. 整个方案要包括:安全、监控、备份等相关内容。

  5. 给web平台,起一个名字。比如:某宝,某东。

    参考架构图:
    请添加图片描述

项目分析

PV是page view的简写。PV是指页面的访问次数,每打开或刷新一次页面,就算做一个pv。

每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间。

计算模型:
每台服务器每秒处理请求的数量=((80%总PV量)/(24小时60分60秒20%)) / 服务器数量 。其中关键的参数是80%、20%。

每天30w PV 在单台机器上:

(300000 * 0.8)/ (86400 * 0.2)= 13.888(QPS)

也就是单台机器需要满足 14(QPS)

Apache的最大连接数,默认为256个。可以满足需求

ulimit 命令是 Linux 系统的内建功能,它具有一套参数集,用于控制 shell 进程及其所创进程的资源使用限制。它主要用于设置用户和系统的资源限制,如打开文件的最大数量、内存使用限制等。

默认的ulimit open files(用户可以打开文件的最大数目)默认为1024 。也可满足需求。

留足余量:

但实际情况并不会这么均匀的分布,会有高峰有低谷。为了应对高峰时段,应该留一些余地,最少也要x2倍,x3倍也不为过。

14个请求/秒 * 2倍=28个请求/秒

14个请求/秒 * 3倍=42个请求/秒

结论:

如果服务器一秒能处理42个请求/秒,就可以应对平均30万PV/每天。

结构图
请添加图片描述

规划表

序号用途操作系统IP主机名
1apache服务器rocky-8.8192.168.137.31zhyj1
2mysql-masterrocky-8.8192.168.137.32zhyj2
3mysql-slaverocky-8.8192.168.137.33zhyj3
4数据备份(web,mysql)rocky-8.8192.168.137.34zhyj4
5zabbix6监控所有主机rocky-8.8192.168.137.35zhyj5
6ansible集中管理rocky-8.8192.168.137.36zhyj6

系统基础环境准备

修改ip地址为静态ip地址

编辑网卡配置文件

[root@zhyj1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160

在这里插入图片描述

重启网卡

[root@zhyj1 ~]# nmcli connection down ens160
[root@zhyj1 ~]# nmcli connection up ens160

在这里插入图片描述
其他机器同理(192.168.137. 32 33 34 35 36)

管理员主机(192.168.137.36)安装ansible软件

ansible是是一个"配置管理工具",它是一个"自动化运维工具",是运维人员的瑞士军刀,可以帮助我们完成一些批量工作或者重复性工作。

ansible是在epel扩展源中

**配置管理员主机(192.168.137.36)epel源 **

[root@zhyj6 ~]# dnf install epel-release.noarch -y

在这里插入图片描述

使用dnf安装ansible

[root@zhyj6 ~]# dnf install ansible -y

查看ansible的版本

[root@zhyj6 ~]# ansible --version
ansible [core 2.15.3]

定义主机清单

[root@zhyj6 ~]# vim /etc/ansible/hosts

在这里插入图片描述

生成ssh公钥和私钥,将生成的公钥拷贝到其他机器,免密登录

[root@zhyj6 ~]# ssh-keygen   回车 * 4  ----生成完成
[root@zhyj6 ~]# ssh-copy-id 192.168.137.35

在这里插入图片描述

其他机器同理

使用ping命令检测是否成功连通

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m ping

请添加图片描述

使用ansible为所有主机安装epel扩展源

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m yum -a  'name=epel-release.noarch state=latest'

在这里插入图片描述

修改epel源为阿里云

[root@zhyj6 yum.repos.d]# ansible 'weblist' -m shell -a " sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel* && sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel* "

在这里插入图片描述

使用ansible关闭所有主机selinux

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m shell -a 'sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config' 

在这里插入图片描述

使用ansible关闭所有主机firewalld

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m service -a 'name=firewalld state=stopped enabled=no'

在这里插入图片描述

使用ansible为所有主机修改阿里云源地址

![

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m script -a '/root/aliyuan.sh'

在这里插入图片描述

Apache php Mysql环境配置

为192.168.137.31主机安装php环境

[root@zhyj1 ~]# yum install -y httpd php php-fpm php-mysqlnd php-gd php-opcache php-json php-xml

为192.168.137.32 192.168.137.33安装mysql

[root@zhyj2 ~]#dnf module install mysql -y
[root@zhyj3 ~]#dnf module install mysql -y

将相应服务启动并设置开机自启动

[root@zhyj1 ~]# systemctl enable --now httpd
[root@zhyj2 ~]# systemctl enable --now mysqld
[root@zhyj3 ~]# systemctl enable --now mysqld

查看服务运行状态

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

数据库安好后,我们需要运行安全初始化命令提高mysql的安全性。 这里实验环境我的mysql密码就设置为123456了

运行mysql_secure_installation会执行几个设置:(192.168.137.32机器)
a)为root用户设置密码
b)删除匿名账号
c)取消root用户远程登录
d)删除test库和对test库的访问权限
e)刷新授权表使修改生效

[root@zhyj2 ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: 
Please set the password for root here.

New password: 

Re-enter new password: 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

192.168.137.33机器同理

安装discuz网站

rz上传discuz网站压缩包

在这里插入图片描述

解压到当前目录下

[root@zhyj1 ~]# unzip Discuz_X3.2_SC_UTF8.zip

把upload文件夹里的文件都移动到 /var/www/html/ 文件夹中

[root@zhyj1 ~]#mv /root/upload/* /var/www/html/

给 /var/www 目录及其子目录赋予权限

[root@zhyj1 ~]# chown apache.apache -R /var/www

重启httpd服务

[root@zhyj1 ~]# systemctl restart httpd

浏览器中输入ip地址进行安装

在这里插入图片描述

环境状态都正确 点击下一步进行安装
在这里插入图片描述

这里选择第二个选项

在这里插入图片描述

填写好相关信息

这里我使用的是192.168.32主机为主数据库,192.168.137.33主机为从数据库 稍后将进行配置

在这里插入图片描述

安装报错记录

在这里插入图片描述

在终端中使用mysql命令没有找到

在这里插入图片描述

分析,可能没有安装mysql软件包

使用yum安装msql

在这里插入图片描述

还是存在该问题

在这里插入图片描述

**经过查询得知 root 用户没有远程连接的权限 **

修改 root 用户的连接权限

[root@zhyj2 ~]# mysql -uroot -p123456
mysql> use mysql;
Database changed
mysql>  select host from user where user='root';
+-----------+
| host      |
+-----------+
| localhost |
+-----------+
1 row in set (0.00 sec)

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host from user where user='root';
+------+
| host |
+------+
| %    |
+------+
1 row in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;

接下来成功安装

进入管理后台

在这里插入图片描述

修改网站名称

在这里插入图片描述

修改成功

在这里插入图片描述

跳转回首页后底部显示网站名称:zhyj-论坛

在这里插入图片描述

删除网站安装目录

[root@zhyj1 ~]# rm -rf /var/www/html/install/

Mysql主从配置

rocky8.8 192.168.137.31 httpd

rocky8.8 192.168.137.32 mysql8.0.32—master

rocky8.8 192.168.137.33 mysql8.0.32—slave

停止httpd服务 复制前要保证同步的数据库一致

[root@zhyj1 ~]# systemctl stop httpd
[root@zhyj2 ~]# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| ultrax             |
+--------------------+
5 rows in set (0.00 sec)

可以看到discuz论坛创建的数据库是 ultrax 我们导出ultrax数据库

导出数据库

[root@zhyj2 /]# mysqldump -uroot -p123456 -B ultrax >/ultrax.sql

在这里插入图片描述

修改配置文件

[root@zhyj2 ~]# vim /etc/my.cnf.d/mysql-server.cnf

添加18–21行

 [mysqld]
 14 datadir=/var/lib/mysql
 15 socket=/var/lib/mysql/mysql.sock
 16 log-error=/var/log/mysql/mysqld.log
 17 pid-file=/run/mysqld/mysqld.pid
 18 server-id=1                                       #本机数据库id号
 19 log-bin=mysql-bin-master						  #启用二进制日志
 20 binlog-do-db=ultrax                           	  #可以被从服务器复制的库, 二进制需要同步的数据库名
 21 binlog-ignore-db=mysql						      #不可以被从服务器复制的库

重启mysql

[root@zhyj2 /]# systemctl restart mysqld

添加授权

[root@zhyj2 ~]# mysql -u root -p123456
mysql> create user 'slave'@'192.168.137.33' identified by '123456';
mysql> grant replication slave on *.* to 'slave'@'192.168.137.33';

查看状态信息

mysql> show master status;

在这里插入图片描述

查看二进制日志

在这里插入图片描述

查看mysql版本 注意:从数据库的版本要大于等于主库版本

[root@zhyj2 /]# mysql -V
mysql  Ver 8.0.32 for Linux on x86_64 (Source distribution)
[root@zhyj3 ~]# mysql -V
mysql  Ver 8.0.32 for Linux on x86_64 (Source distribution)
[root@zhyj3 ~]# mysql -uslave -p123456 -h192.168.137.32

测试是否可以连接到主数据库

在这里插入图片描述

导入数据库文件

[root@zhyj2 ~]# scp /ultrax.sql root@192.168.137.33:/
[root@zhyj3 ~]# mysql -uroot -p123456 < /ultrax.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

在这里插入图片描述

导入成功

修改从服务器配置文件

[root@zhyj3 ~]# vim /etc/my.cnf.d/mysql-server.cnf

添加第18行

 13 [mysqld]
 14 datadir=/var/lib/mysql
 15 socket=/var/lib/mysql/mysql.sock
 16 log-error=/var/log/mysql/mysqld.log
 17 pid-file=/run/mysqld/mysqld.pid
 18 server-id=2   #加入server-id

修改后重启mysqld服务

[root@zhyj3 ~]# systemctl restart mysqld

配置master的连接参数

[root@zhyj3 ~]# mysql -uroot -p123456
mysql> stop slave;
mysql> change master to master_host='192.168.137.32',master_user='slave',master_password='123456';
mysql> start slave;
mysql> show slave status\G   

查看状态

在这里插入图片描述

mysql的Slave_IO_Running 与主机master的通信正常

mysql的Slave_SQL_Running 从数据库sql进程运行正常

在主服务器上查看状态

[root@zhyj2 ~]# mysql -uroot -p123456
mysql> show processlist \G

在这里插入图片描述

State状态:

Source has sent all binlog to replica; waiting for more updates

源已将所有binlog发送到副本;正在等待更多更新

Mysql-优化-半同步模式

默认情况下,MySQL的复制功能是异步的,异步复制可以提供最佳的性能,主库把binlog日志发送给从库即结束,并不验证从库是否接收完毕。这意味着当主服务器或从服务器端发生故障时,有可能从服务器没有接收到主服务器发送过来的binlog日志,这就会造成主服务器和从服务器的数据不一致,甚至在恢复时造成数据的丢失。

主服务器上的I/O threads 将binlog写入二进制日志中就返回给客户端一个结果,无需等待二进制日志是否成功发送到从库和从库上是否成功完成relay log的写入以及SQL threads从relay log中提取二进制日志写入自己binlog的过程。

主库半同步配置

登录mysql为主库安装半同步插件

[root@zhyj2 ~]# mysql -uroot -p123456
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

开启半同步

mysql> set global rpl_semi_sync_master_enabled=1;
mysql> show variables like 'rpl_semi_sync_master_enabled';

在这里插入图片描述

从库半同步配置

登录mysql为从库安装半同步插件

[root@zhyj3 ~]# mysql -uroot -p123456
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

开启半同步

mysql> set global rpl_semi_sync_slave_enabled=1;
mysql> show variables like 'rpl_semi_sync_slave_enabled';

在这里插入图片描述

mysql> stop slave io_thread;
mysql> start slave io_thread;
mysql> show status like 'rpl_semi_sync%';

在这里插入图片描述

主库查看处于半同步模式的从库

[root@zhyj2 ~]# mysql -uroot -p123456
mysql> show status like 'rpl_semi_sync_master_clients%';

在这里插入图片描述

Mysql-从库-多线程优化

主从同步的模式默认是多线程,而从库的sql 线程是单线程 。从库的单线程在大数据量的时候会有延迟,将从库的线程改为多线程可以减少延迟问题。

修改 192.168.137.33 从库配置文件

添加以下19-23行

 13 [mysqld]
 14 datadir=/var/lib/mysql
 15 socket=/var/lib/mysql/mysql.sock
 16 log-error=/var/log/mysql/mysqld.log
 17 pid-file=/run/mysqld/mysqld.pid
 18 server-id=2
 19 slave-parallel-type=logical_clock     #以组方式提交
 20 slave-parallel-workers=8              #8个线程
 21 master_info_repository=table		  #存放master 信息的形式设置,默认是file
 22 relay_log_info_repository=table	      #存放relay 日志信息的形式设置,默认是file
 23 relay_log_recovery=ON	              #开启relay 日志恢复

重启mysql服务

[root@zhyj3 ~]# systemctl restart mysqld
mysql> show processlist;
mysql> use mysql

在这里插入图片描述

mysql> show tables like '%slave%';

在这里插入图片描述

master,relaylogrelaylog,worker 信息,会以表形式存储于数据库

rsync数据备份(mysql web)

192.168.137.32 (zhyj2) 源数据主机 --> 192.168.137.34 (zhyj4) 备份端主机 mysql

192.168.137.31 (zhyj1) 源数据主机 --> 192.168.137.34 (zhyj4) 备份端主机 web

rsync服务安装

[root@zhyj2 ~]# yum install rsync -y

使用非系统用户备份数据

在192.168.137.34 (zhyj4) 备份端主机创建配置文件

[root@zhyj4 ~]# vim /etc/rsyncd.conf
id = root    								#运行进程的身份
gid = root    								#运行进程的组
hosts allow = 192.168.137.0/24 				#允许登录的ip
use chroot = yes	    					#锁定家目录,被黑之后无法在目录之外创建文件
max connections = 10	    				#最大连接数
pid file = /var/run/rsyncd.pid	    		#进程pid文件
lock file = /var/run/rsync.lock	    		#参数锁文件
log file = /var/log/rsyncd.log	    		#日志文件
motd file = /etc/rsyncd.motd				#欢迎提示信息文件

[databack]								    #模块名称
path = /databack/							#路径
comment = used for databack					#描述
read only = false							#权限只读
list = yes									#是否允许查看模块信息
auth users = usrrsync						#备份的用户,和系统用户无关。
secrets file = /etc/usrrsync.txt			#存放用户的密码文件,格式是 用户名:密码。

在根目录下创建数据备份的文件夹

[root@zhyj4 ~]# mkdir /databack

创建提示信息文件和用户密码文件

[root@zhyj4 ~]# vim /etc/usrrsync.txt
[root@zhyj4 ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
[root@zhyj4 ~]# vim /etc/usrrsync.txt
usrrsync:123456

修改权限为600

[root@zhyj4 ~]# chmod 600 /etc/usrrsync.txt

创建配置文件

[root@zhyj4 ~]# vim /etc/sysconfig/rsyncd

OPTIONS=""

创建service文件,注册rsyncd以服务的模式运行

[root@zhyj4 ~]# vim /lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"

[Install]
WantedBy=multi-user.target

启动服务,查看服务日志和端口确认成功启动

[root@zhyj4 ~]# systemctl enable --now rsyncd.service
[root@zhyj4 ~]# cat /var/log/rsyncd.log 
2024/01/10 11:01:11 [5901] rsyncd version 3.1.3 starting, listening on port 873
[root@zhyj4 ~]# netstat -anutp | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      5901/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      5901/rsync          

源数据主机192.168.137.32 (zhyj2) 创建密码文件

[root@zhyj2 ~]# vim /etc/mima
123456

修改密码文件权限600

[root@zhyj2 ~]# chmod 600 /etc/mima

在rsync 命令中使用 password file 指定此文件即可无交互备份

备份数据测试

rsync -avz /tmp/ usrrsync@192.168.137.34::databack --password-file=/etc/mima

在这里插入图片描述

rsync数据备份环境部署成功

Mysql定时数据备份

编写mysql数据备份脚本

[root@zhyj2 ~]# vim mysqlbak.sh
#!/bin/bash

# 设置备份目录和文件名
backup_directory="/backup_data/mysql"
backup_log_directory="/backup_data/log"
backup_filename="backup_$(date +%Y%m%d).sql"

# 设置MySQL连接参数
mysql_host="127.0.0.1"
mysql_user="root"
mysql_password="123456"
mysql_database="ultrax"

# 创建备份目录(如果不存在)
mkdir -p "$backup_directory"
mkdir -p "$backup_log_directory"

# 执行备份命令
mysqldump -h "$mysql_host" -u "$mysql_user" -p"$mysql_password" -B "$mysql_database" > "$backup_directory/$backup_filename"

# 删除7天前数据
find "$backup_directory" -name "*.sql" -mtime +7 -exec rm {} \;

# 打包mysql脚本
tar -cf "/backup_data/backup_data_latest_7_$(date +%Y%m%d).tar.gz"  /backup_data/mysql

# 检查备份是否成功
if [ $? -eq 0 ]; then
  echo "MySQL数据库备份成功:$backup_directory/$backup_filename" >>  /backup_data/log/backup.log
else
  echo "MySQL数据库备份失败: $backup_directory/$backup_filename" >> /backup_data/log/backup.log
fi

# rsync备份
rsync -avz "/backup_data/backup_data_latest_7_$(date +%Y%m%d).tar.gz" usrrsync@192.168.137.34::databack --password-file=/etc/mima

给脚本添加执行权限

[root@zhyj2 ~]# chmod +x mysqlbak.sh

测试脚本

在这里插入图片描述

在备份端机器查看

在这里插入图片描述

添加Mysql定时备份任务

计划任务在每天凌晨3点执行Mysql数据备份到192.168.137.34

[root@zhyj2 ~]# crontab -e
0 3 * * * sh /root/mysqlbak.sh
[root@zhyj2 ~]# crontab -l

在这里插入图片描述

Web网站定时数据备份

编写web网站数据备份脚本

[root@zhyj1 ~]# vim webback.sh
#!/bin/bash

# 设置备份目录和文件名
backup_directory="/backup_data/"
backup_log_directory="/backup_data/log"
backup_filename="web_$(date +%Y%m%d).tar.gz"

# 创建备份目录(如果不存在)
mkdir -p "$backup_directory"
mkdir -p "$backup_log_directory"

# 删除7天前数据
find "$backup_directory" -name "*.tar.gz" -mtime +7 -exec rm {} \;

# 打包/var/www/html目录
tar -cf "/backup_data/web_$(date +%Y%m%d).tar.gz"  /var/www/html

# 检查备份是否成功
if [ $? -eq 0 ]; then
  echo "web数据备份成功:$backup_directory$backup_filename" >>  /backup_data/log/backup.log
else
  echo "web数据备份失败: $backup_directory$backup_filename" >> /backup_data/log/backup.log
fi

# rsync备份
rsync -avz "/backup_data/web_$(date +%Y%m%d).tar.gz" usrrsync@192.168.137.34::databack --password-file=/etc/mima

数据源主机192.168.137.31 (zhyj1)创建密码文件

[root@zhyj1 ~]# vim /etc/mima
123456

修改密码文件权限600

[root@zhyj1 ~]# chmod 600 /etc/mima

在rsync 命令中使用 password file 指定此文件即可无交互备份

测试备份脚本

[root@zhyj1 ~]# ./webback.sh 

在备份端主机查看

在这里插入图片描述

添加Web网站定时备份任务

计划任务在每天凌晨3点执行web数据备份到192.168.137.34

[root@zhyj1 ~]# crontab -e
0 3 * * * sh /root/webback.sh
[root@zhyj1 ~]# crontab -l

在这里插入图片描述

防止暴力破解ssh远程登录

管理员使用ansible(192.168.137.36)yum模块安装fail2ban

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m yum -a 'name=fail2ban state=latest'

在这里插入图片描述

修改配置文件

5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下。

[root@zhyj6 ~]# vim /etc/fail2ban/jail.conf 
280 port    = ssh            
281 enabled = true              				#启用改选项
282 filter  = sshd								#动作的相关参数,对应action.d/iptables.conf文件
283 action  = iptables[name=SSH, port=ssh, protocol=TCP]
284 logpath = /var/log/secure   				#检测的系统的登录日志文件。这里要写sshd服务日志文件。
285 bantime = 3600								#禁止用户IP访问主机1小时。
286 findtime = 300								#在5分钟内内出现规定次数就开始工作。
287 maxretry = 3								#3次密码验证失败。

在这里插入图片描述

使用ansible copy模块拷贝配置文件到所有主机

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m copy -a 'src=/etc/fail2ban/jail.conf dest=/etc/fail2ban/jail.conf backup=no force=yes'

在这里插入图片描述

启动所有主机的fail2ban服务并设置开机自启动

[root@zhyj6 ~]# ansible -i /etc/ansible/hosts 'weblist' -m service -a 'name=fail2ban state=started enabled=yes'

在这里插入图片描述

使用192.168.137.64进行测试可以看到ip已经被禁止

在这里插入图片描述

安装zabbix6进行监控

安装lanmp环境 zabbix依赖php环境

[root@zhyj5 ~]# dnf module install httpd mysql php -y

在线安装

选择自己的平台

在这里插入图片描述

在这里插入图片描述

使用ansible为所有主机安装zabbix源

[root@zhyj6 ~]# ansible 'weblist' -m shell -a 'rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm'

在这里插入图片描述

使用ansible为所有主机清理yum源缓存

[root@zhyj6 ~]# ansible 'weblist' -m shell -a 'dnf clean all' -o

在这里插入图片描述

把所有zabbix源地址替换为阿里云地址

[root@zhyj6 ~]# vim /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/6.0/rhel/8/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]
name=Zabbix Official Repository (non-supported) - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/8/$basearch/
enabled=1
gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX
gpgcheck=1

[zabbix-unstable]
name=Zabbix Official Repository (unstable) - $basearch
baseurl=https://repo.zabbix.com/zabbix/5.5/rhel/8/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[root@zhyj6 ~]# vim /etc/yum.repos.d/zabbix-agent2-plugins.repo
[zabbix-agent2-plugins]
name=Zabbix Official Repository (Agent2 Plugins) - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix-agent2-plugins/1/rhel/8/$basearch/
enabled=1
gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX
gpgcheck=1

使用ansible将配置好的zabbix源文件拷贝到所有主机

[root@zhyj6 ~]# ansible 'weblist' -m copy -a 'src=/etc/yum.repos.d/zabbix.repo dest=/etc/yum.repos.d/zabbix.repo' -o

在这里插入图片描述

[root@zhyj6 ~]# ansible 'weblist' -m copy -a 'src=/etc/yum.repos.d/zabbix-agent2-plugins.repo dest=/etc/yum.repos.d/zabbix-agent2-plugins.repo' -o

在这里插入图片描述

安装 Zabbix server,Web前端,agent客户端

[root@zhyj5 ~]# dnf install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent -y

启动数据库 加入开机自启动

[root@zhyj5 ~]# systemctl enable mysqld --now

登录mysql 设置root密码为123456

[root@zhyj5 ~]# mysql 
mysql> alter user root@localhost identified by '123456';

刷新权限后退出

mysql> flush privileges ;
mysql> exit;

使用 mysql_secure_installation 进行安全初始化配置此处省略 可参考上文

重新登录mysql

[root@zhyj5 ~]# mysql -uroot -p123456

创建初始数据库

mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
mysql> create user zabbix@localhost identified by '123456';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> set global log_bin_trust_function_creators = 1;
mysql> quit;

导入初始架构和数据,系统将提示您输入新创建的密码。

[root@zhyj5 ~]# zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix

Enter password:  123456

导入数据库后禁用global log_bin_trust_function_creators

[root@zhyj5 ~]# mysql -uroot -p123456
mysql> set global log_bin_trust_function_creators = 0;
mysql> quit;

为Zabbix server配置数据库

编辑配置文件 /etc/zabbix/zabbix_server.conf

[root@zhyj5 ~]# vim /etc/zabbix/zabbix_server.conf

DBPassword=123456

在这里插入图片描述

修改php配置文件的时区

[root@zhyj5 ~]# vim /etc/php.ini

在这里插入图片描述

启动Zabbix server和Agent进程

启动Zabbix server和agent进程,并为它们设置开机自启

[root@zhyj5 ~]# systemctl restart zabbix-server zabbix-agent httpd php-fpm
[root@zhyj5 ~]# systemctl enable zabbix-server zabbix-agent httpd php-fpm

通过web 界面安装 Zabbix Web 前端页面

浏览器访问:http://192.168.137.35/zabbix

在这里插入图片描述

在这里插入图片描述

密码 zabbix password : 123456

在这里插入图片描述

服务器的名字 zabbix server name : server-zabbix-35

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

完成安装

登录zabbix并修改中文

账号: Admin 密码:zabbix

在这里插入图片描述

User setting —> Profile —> Language —> Update 更新即可

请添加图片描述

**注意:**zabbix 被监控端都需要安装agent客户端

使用ansible为所有主机安装Agent客户端

[root@zhyj6 ~]# ansible 'weblist' -m yum -a 'name=zabbix-agent state=latest' -o

35这台机器安装过agent 所以未发生改变 其他机器均被安装

在这里插入图片描述

配置被监控的主机

我们需要修改每一台被监控主机的agent配置文件把server地址指向192.168.137.35

[root@zhyj1 ~]# vim /etc/zabbix/zabbix_agentd.conf 

改117行为 server ip 地址192.168.137.35

![

主动模式,改171行为 server ip 地址192.168.137.35

!

改182行为自己的主机名

在这里插入图片描述

启动服务

[root@zhyj1 ~]# systemctl enable zabbix-agent.service --now

其他主机配置同理 此处省略(192.168.137.32 33 34 35 36 )的配置

监控Apache服务状态

编辑Apache的主配置文件/etc/httpd/conf/httpd.conf

开启Apache server-status 状态页面

[root@zhyj1 ~]# vim /etc/httpd/conf/httpd.conf

加入如下图配置

在这里插入图片描述

修改完后重启Apache服务

[root@zhyj1 ~]# systemctl restart httpd

创建监控主机

请添加图片描述

请添加图片描述

查看主机的图形数据

请添加图片描述

图像中存在乱码

配置字体文件解决图像乱码

上传simkai.ttf到 /usr/share/zabbix/assets/fonts下替换graphfont.ttf 字体

[root@zhyj5 ~]# cd /usr/share/zabbix/assets/fonts
[root@zhyj5 fonts]# mv graphfont.ttf graphfont.ttf.bak
[root@zhyj5 fonts]# mv simkai.ttf graphfont.ttf

请添加图片描述

刷新页面,已解决乱码

监控Mysql服务状态

创建mysql用户让agent用户使用此用户获取mysql数据

[root@zhyj2 ~]# mysql -uroot -p123456
mysql> create user 'zabbix'@localhost;
mysql> alter user zabbix@localhost identified by '123456';
mysql> exit;

拷贝监控配置文件模板

[root@zhyj2 ~]# cp /usr/share/doc/zabbix-agent/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/

编辑/etc/my.cnf.d/client.cnf 加入zabbix的用户名密码

[root@zhyj2 ~]# vim /etc/my.cnf.d/client.cnf 

在这里插入图片描述

重新启动zabbix客户端

[root@zhyj2 ~]# systemctl restart zabbix-agent.service

添加mysql监控主机

请添加图片描述

添加成功

请添加图片描述

同理添加其他主机即可

请添加图片描述

配置自动发现服务以减少手工配置

手工配置繁琐且麻烦,逐个主机添加无疑增加了管理员的工作量。我们只需要设置自动发现服务即可解决手动添加主机问题,简化后期因业务扩大带来的繁琐配置。

创建自动发现规则

请添加图片描述

填写相应信息 检查system.uname 作为判断条件

请添加图片描述

创建自动发现动作

请添加图片描述

添加触发条件

请添加图片描述

请添加图片描述

设置触发后的动作

请添加图片描述

点击添加即可

请添加图片描述

添加成功

查看 监测—>自动发现 查看是否存在自动发现的主机

请添加图片描述

确认可发现主机设备

压力测试网站检测机器性能

管理员使用192.168.137.36主机 ab命令测试主机192.168.137.31 apache服务性能

安装httpd-tools

[root@zhyj6 ~]# yum install httpd-tools -y

模拟1000请求每次并发100个测试discuz论坛首页

ab -n 1000 -c 100 http://192.168.137.31/index.php

[root@zhyj6 ~]# ab -n 1000 -c 100 http://192.168.137.31/index.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.137.31 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache/2.4.37
Server Hostname:        192.168.137.31
Server Port:            80

Document Path:          /index.php
Document Length:        12292 bytes

Concurrency Level:      100
Time taken for tests:   17.636 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      13128000 bytes
HTML transferred:       12292000 bytes
Requests per second:    56.70 [#/sec] (mean)
Time per request:       1763.623 [ms] (mean)
Time per request:       17.636 [ms] (mean, across all concurrent requests)
Transfer rate:          726.93 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.0      0       9
Processing:    27 1711 352.7   1682    3015
Waiting:       13 1711 352.8   1682    3015
Total:         28 1712 351.9   1682    3020

Percentage of the requests served within a certain time (ms)
  50%   1682
  66%   1830
  75%   1911
  80%   1975
  90%   2160
  95%   2320
  98%   2467
  99%   2599
 100%   3020 (longest request)

可以看到apache每秒请求数为Requests per second: 56.70 [#/sec] (mean) 结果大于我们开始分析的42个请求/秒

结论:服务器一秒能处理56个请求/秒,可以应对平均30万PV/每天

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值