Web 前端采用 Nginx/HAProxy+Keepalived 作负载均衡器;后端wordpress采用 MySQL数据库一主多从或读写分离架构实现。
项目架构图:
基于项目设计架构图, 采用共7台CentOS7.9-mini系统的VMware虚拟机。
broadcast input
快捷键: ctr+alt+b
bashvi /etc/selinux/config
SELINUX=disabled
systemctl disable firewalld
reboot
bashsestatus systemctl status firewalld
bashcd /etc/yum.repos.d/
ls
mkdir back
mv *.repo back
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
/etc/yum.repos.d/
目录。ls
命令列出当前目录下的文件列表,确认有多个 .repo
结尾的文件。mkdir back
命令创建一个名为 "back" 的新文件夹。mv *.repo back
命令将当前目录下所有以 .repo
结尾的文件移动到新创建的 "back" 文件夹中。curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
命令从阿里云的镜像地址下载 CentOS-Base.repo
文件并保存到 /etc/yum.repos.d/
目录中。
这里你执行了两个命令来清理和更新 YUM 软件包缓存:yum clean all
:这个命令用于清理 YUM 软件包缓存。在这个过程中,它会清理所有已下载的元数据和软件包缓存,以便后续重新下载最新的信息。yum makecache
:这个命令用于重新生成 YUM 软件包缓存。它会重新下载软件包列表和元数据,并将其缓存起来,以便后续的软件包安装和更新操作能够更快速地进行。bashyum install ntp ntpdate -y ntpdate cn.pool.ntp.org hwclock --systohc
yum install ntp ntpdate -y
:这个命令使用 YUM 包管理器安装了 NTP(Network Time Protocol)和 ntpdate 软件包。选项 -y
表示在安装过程中自动应用所有的确认提示,以便无需手动确认安装。ntpdate cn.pool.ntp.org
:这个命令使用 ntpdate 工具从中国的 NTP 服务器(cn.pool.ntp.org)同步系统时间。hwclock --systohc
:这个命令将系统时间写入硬件时钟,以便在系统重启后能够保持正确的时间。date
命令检查时间是否已经同步成功bashyum install gcc gcc-c++ wget telnet net-tools make unzip vim -y
gcc
:GNU Compiler Collection,用于编译和构建 C 语言程序。gcc-c++
:C++ 编译器,用于编译和构建 C++ 程序。wget
:用于从网络上下载文件的工具。telnet
:用于通过 Telnet 协议远程登录到其他服务器的工具。net-tools
:提供了一些网络管理工具,如 ifconfig、netstat 等。make
:用于自动化构建和管理程序的工具。unzip
: 是一个常用的命令行工具,用于解压缩zip格式的压缩文件。vim
: 是一款功能强大的文本编辑器,常用于在终端环境下编辑文本文件。bashscp mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar root@192.168.6.135:/root
bashtar xf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C /opt/
cd /opt
yum localinstall *
systemctl start mysqld systemctl enable mysqld grep "password" /var/log/mysqld.log mysql -u root -p ALTER USER 'root'@'localhost' IDENTIFIED BY 'Gyc0109@123';
systemctl start mysqld
: 启动MySQL服务。systemctl enable mysqld
: 设置MySQL服务开机自启动。grep "password" /var/log/mysqld.log
: 在MySQL日志文件中查找包含"password"的内容。mysql -u root -p
: 使用root用户身份登录MySQL数据库。ALTER USER 'root'@'localhost' IDENTIFIED BY 'Gyc0109@123';
: 更改MySQL中root用户在本地主机上的密码为Gyc0109@123
。mysql 5.6以后的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一 样,但是查看到直接copy data文件夹后server_uuid是相同的,
show variables like '%server_uuid%';
解决办法:找到/var/lib/mysql文件夹下的auto.cnf文件,修改里面的uuid值,保证 各个db的uuid不一样,重启db即可
systemctl restart mysqld.service
bash```bash vim /etc/my.cnf server-id=134 log-bin=mysql-bin systemctl restart mysqld
sqlcreate user 'copy'@'%' identified with mysql_native_password by 'Gyc0109@123';
GRANT REPLICATION SLAVE ON *.* TO 'copy'@'%';
flush privileges;
sqlshow master status\G;
bashvim /etc/my.cnf server-id=135 relay_log = mysql-relay read_only = 1 log_bin = mysql-bin systemctl restart mysqld
bashvim /etc/my.cnf server-id=136 relay_log = mysql-relay read_only = 1 log_bin = mysql-bin systemctl restart mysqld
sqlCHANGE MASTER TO
master_host = '192.168.6.134',
master_user = 'copy',
master_password = 'Gyc0109@123',
master_log_file = 'mysql-bin.000001',
master_log_pos = 825;
start slave;
启动复制线程。bashSHOW SLAVE STATUS\G
sqlshow databases;
show databases;
bashyum install php php-fpm php-cli php-common php-gd php-mbstring php-mysql php-pdo php-devel phpxmlrpc php-xml php-bcmath php-dba php-enchant -y
php -v
查看是否安装成功, 安装成功则输出版本号bash// 添加 Remi 存储库
yum install -y yum-utils
yum install -y epel-release
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
// 启用所需的 PHP 版本
yum-config-manager --enable remi-php74
// 升级 PHP
yum install -y php php-fpm php-cli php-common php-gd php-mbstring php-mysqlnd php-pdo php-xml php-xmlrpc php-bcmath php-dba php-enchant
// 检查 PHP 版本
php -v
bashyum install yum-utils
cd /etc/yum.repos.d/
vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum clean all
yum makecache
bashyum install nginx -y
bashnginx -v
bashsystemctl start nginx
bashcd /etc/nginx/conf.d/
vim default.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
vim /usr/share/nginx/html/index.php <?php phpinfo(); ?>
bashsystemctl restart php-fpm systemctl restart nginx
IP/index.php
创建用户:
sqlCREATE USER 'root'@'%' IDENTIFIED BY 'Gyc0109@123';
授权所有主机通过root用户进行访问:
sqlGRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
刷新权限:
sqlFLUSH PRIVILEGES;
sqlcreate database wordpress;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.01 sec)
bashcd /etc/nginx/conf.d/
vim default.conf
server {
listen 80;
server_name localhost;
location / {
root /www/wordpress;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /www/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx -t
检查nginx配置mkdir /www
wget https://cn.wordpress.org/wordpress-6.6.1-zh_CN.tar.gz
下载bashtar -zxf wordpress-6.6.1-zh_CN.tar.gz
mv wordpress /www
bashcd /www/wordpress/
cp wp-config-sample.php wp-config.php
vim wp-config.php
//根据自己设置的数据库,对应修改下面配置
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );
/** Database username */ 用户名
define( 'DB_USER', 'root' );
/** Database password */ 密码
define( 'DB_PASSWORD', 'Gyc0109@123' );
/** Database hostname */ ('192.168.6.134'为主数据库IP)
define( 'DB_HOST', '192.168.6.134' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
bashsystemctl restart php-fpm systemctl restart nginx
访问web01服务器IP:
访问web02服务器IP:
bashyum install haproxy keepalived ipvsadm -y
bashvim /etc/haproxy/haproxy.cfg
log 127.0.0.1 local2 info
bashfrontend web_lb #前台
bind *:80
stats uri /ha_stats
default_backend web_back
backend web_back #后台负载均衡
balance roundrobin
server web01 192.168.6.137:80 check weight 1
server web02 192.168.6.138:80 check weight 1
bashsystemctl restart haproxy systemctl status haproxy
bashcp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
bash
```bash
! Configuration File for keepalived
global_defs {
router_id director1 # 辅助改为director2
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy_status.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface ens33 # VIP绑定接口
virtual_router_id 80 # MASTER,BACKUP一致
priority 100 # 辅助改为50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.6.131 # 虚拟IP
}
track_script {
check_haproxy
}
}
bash! Configuration File for keepalived
global_defs {
router_id director2 # 辅助改为director2
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy_status.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface ens33 # VIP绑定接口
virtual_router_id 80 # MASTER,BACKUP一致
priority 50 # 辅助改为50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.6.131 # 虚拟IP
}
track_script {
check_haproxy
}
}
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Haproxy失败,则关闭 本机的Keepalived
bashvim /etc/keepalived/check_haproxy_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ]; then
/etc/init.d/keepalived stop
fi
bashsystemctl start keepalived
ip addr
命令可以看到此时虚拟ip在master服务器上
发现此时虚拟IP出现在了BackUp备机上
本文作者:GYC
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!