keepalived+haproxy实现wordpress高可用小架构

keepalived+haproxy实现wordpress高可用小架构

1.在两台充当数据库服务器上安装mysql并建立主从,添加授权账户

1)yum安装mysql数据库

[root@localhost ~]# yum install mariadb mariadb-server -y

2)修改master数据库配置文件

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server_id=1    #主id号
log_bin=/data/binlog/bin  #开启二进制日志记录并指定保存路径
innodb_file_per_table  #每个表保存在单个文件
datadir=/var/lib/mysql  #数据可路径
socket=/var/lib/mysql/mysql.sock  #套接字路径
……

3)开启主服务数据库并添加授权用户

[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -uroot -h127.0.0.1
    添加从服务授权用户:
         MariaDB [(none)]> grant replication slave  on *.* to user1@'192.168.1.106' identified by '123456';
    创建wordpress专用数据库:
        MariaDB [(none)]> create database boss;
    添加wordpress授权用户:
         MariaDB [(none)]> grant all on boss.* to boss@'192.168.1.%' identified by '123456';

4)修改从服务数据库配置文件

[mysqld]
server_id=2  #从服务的id号
read_only   #只读模式不允许修改数据内容
innodb_file_per_table
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
…..

5)开启从mysql数据库并添加主从关系

[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# msyql
MariaDB [(none)]> change master to
master_host='192.168.1.9',   
master_user='user1',    
master_password='123456',    
master_log_file='bin.000005',    
master_log_pos=245;
MariaDB [(none)]> start slave;#开启从服务
MariaDB [(none)]>show slave status\G; #查看主从建立状态    

 lave_IO_State: Waiting for master to send event
                                    Master_Host: 192.168.1.9
                                    Master_User: user1
                                    Master_Port: 3306
                                Connect_Retry: 60
                            Master_Log_File: bin.000007
                    Read_Master_Log_Pos: 245
                             Relay_Log_File: mariadb-relay-bin.000007
                                Relay_Log_Pos: 523
                Relay_Master_Log_File: bin.000007
                         Slave_IO_Running: Yes
                        Slave_SQL_Running: Yes
                            ……
                            ……

2.在两台充当调度服务器主机分别编译安装haproxy

1).安装编译haproxy需要的包

[root@localhost ~]# yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate

2)解压并编译安装haproxy-1.8.20.tar.gz

[root@localhost ~]# tar  xvf   /data/tools/haproxy/haproxy-1.8.20.tar.gz  -C /data/tools/haproxy/haproxy/

[root@localhost ~]# cd /data/tools/haproxy/haproxy-1.8.20

[root@localhost haproxy-1.8.20]# make  ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1  USE_CPU_AFFINITY=1  PREFIX=/usr/local/haproxy
            编译注释:
                            ARCH=x86_64:x86_64平台
                            TARGET=linux2628: linux的内核版本为 2.6.28, 3.x及以上版本
                            USE_PCRE=1 :支持正则表达
                            USE_ZLIB=1 :支持压缩
                            USE_SYSTEMD=1:支持systemd启动服务管理
                            USE_CPU_AFFINITY=1:CUP核心绑定
                            PREFIX=/usr/local/haproxy:安装路径

[root@localhost haproxy-1.8.20]# make install PREFIX=/usr/local/haproxy 

3)复制二进制执行程序至/usr/sbin中

[root@localhost haproxy-1.8.20]# cp haproxy /usr/sbin/

4)创建systemd启动脚本

[root@localhost haproxy-1.8.20]# vim /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAproxy Load Balancer
After=syslog.target network.target

[Service]
 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
 ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
 ExecStop=/bin/kill -USR2 $MAINPID

[Install]
 WantedBy=multi-user.target

5)创建haproxy服务器启动用户

[root@localhost ~]# useradd -r -s  /sbin/nologin haproxy -u 88
[root@localhost ~]# groupmod  -g 88  haproxy
6)修改改配置文件
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
……
uid 88   #执行haproxy的用户身份
gid 88   #所属的组
……
listen webpro
bind 192.168.1.23:80  #绑定虚拟VIP及端口
mode tcp
server web1 192.168.1.108:80  #绑定nginx后端服务
server web2 192.168.1.109:80  #绑定nginx后端服务

listen mysql_3306 
bind 192.168.1.23:3306         
mode tcp
server web3 192.168.1.9:3306  #绑定mysql数据库

7)启动haproxy调度服务

[root@localhost ~]# systemctl start haproxy

3.在两台充当调度服务器主机分别安装keepalived并配置

1)yum安装

[root@localhost ~]#  yum insatll keepallived -y

2)修改配置文件

! Configuration File for keepalived

global_defs {
     notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
     }
     notification_email_from Alexandre.Cassen@firewall.loc
     smtp_server 192.168.200.1
     smtp_connect_timeout 30
     router_id s1
     vrrp_skip_check_adv_addr
     #vrrp_strict
     vrrp_garp_interval 0
     vrrp_gna_interval 0
}

vrrp_instance VIP1 {
        state MASTER                     #另外一台主机改为BACKUP
        interface ens33
        virtual_router_id 23
        priority 100                 #另外一台备份服务优先级改为80,要不比100低
        advert_int 2
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
        192.168.1.23 dev ens33 label ens33:0
             }
        unicast_src_ip 192.168.1.110            #另外一台备份服务的单播  ip位置与master的ip位置对调         
 unicast_peer {
        192.168.1.107
                                    }

}

3) 启动keepalived

[root@localhost ~]# systemctl start keepalived

4.在两台后端服务器主机上编译安装nginx

1)解压nginx-1.14.2.tar.gz源码压缩文件

[root@localhost tools]# tar xvf nginx-1.14.2.tar.gz
[root@localhost tools]# cd nginx-1.14.2/

2)准备nginx的 编译环境

 [root@localhost nginx-1.14.2]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget  ntpdate  gcc gcc-c++ glibc glibc-devel 
pcre pcre-devel openssl  openssl-devel systemd-devel net-tools iotop bc  zip unzip zlib-devel bash-completion 
nfs-utils automake libxml2  libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
     若出现以下不能解决依赖包关系,需要配置yum仓库,需添加update下载仓库

keepalived+haproxy实现wordpress高可用小架构

        [root@localhost nginx-1.14.2]# vim /etc/yum.repos.d/base.repo
             ……
             ……
            [update]
            name=aliyun_centos_update
            baseurl=https://mirrors.aliyun.com/centos/7.6.1810/updates/x86_64/
            gpgcheck=0

3)编译安装nginx

[root@localhost nginx-1.14.2]# ./configure --prefix=/apps/nginx  --user=nginx   --group=nginx  --with-http_ssl_module --with-http_v2_module  
--with-http_realip_module --with-http_stub_status_module   --with-http_gzip_static_module --with-pcre --with-stream 
--with-stream_ssl_module --with-stream_realip_module --with--http_perl_module
[root@localhost nginx-1.14.2]#  make&&make install

4)创建nginx的进程启动用户

        [root@centos7 ~]# useradd -r -s /sbin/nologin nginx -u 23

5)创建自定义脚本文件

[root@centos7 ~]#vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
  # Nginx will fail to start if /run/nginx.pid already exists but has the wrong
 # SELinux context. This might   happen when running `nginx -t` from the cmdline.  https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /apps/nginx/logs/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
#KillSignal=SIGQUIT
#TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

6)测试nginx是否能启动

[root@centos7 ~]#systemctl start nginx
[root@centos7 ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
     Active: active (running) since Wed 2019-06-12 10:57:07 CST; 1s ago
    Process: 8875 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)

5.两台端服务nginx编译安装php-7.3.2.tar.xz

1)安装php的编译环境

[root@centos7 ~]#yum install libxml2-devel bzip2-devel libmcrypt-devel

2)解压php-7.3.2.tar.xz

[root@centos7 ~]#cd /data/php
[root@centos7 php]# tar xvf php-7.3.2.tar.xz

3)编译安装

[root@centos7 php]# cd php-7.3.5/

[root@centos7 php-7.3.2]# ./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo 

[root@centos7 php-7.3.2]# make  && make install

4)设置fcgi-php的三个配置文件和一个服务脚本文件:

主配置:/app/php/etc/php-fpm.conf
子配置:/app/php/etc/php-fpm.d/ www.conf
修改时区配置文件: /etc/php.ini 
脚本文件:/etc/init.d/php-fpm

[root@centos7 php-7.3.2]# cp php.ini-production  /etc/php.ini

[root@centos7 php-7.3.2]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@centos7 php-7.3.2]# chmod +x /etc/init.d/php-fpm  #脚本文件添加执行权限

[root@centos7 php-7.3.2]# cd /app/php/etc/
[root@centos7 etc]# cp php-fpm.conf.default    php-fpm.conf  #主配置文件不需要修改
[root@centos7 etc]# cd /app/php/etc/php-fpm.d/    #修改子配置文件

[root@centos7 php-fpm.d]# cp www.conf.default www.conf
[root@centos7 php-fpm.d]# vim www.conf
user = nginx      #进程启动用户为nginx                                                                                                  
group =nginx    #属组为nginx组
listen = 127.0.0.1:9000  #监听本地9000端口

5)启动php-fpm服务

[root@centos7 php-fpm.d]# service php-fpm start

6.两台后端服务nginx的wordpress初始化

1)将wordpress压缩文件的wordpress目录压到nginx的根域目录下

[root@centos7 tools]# tar xvf wordpress-5.0.3-zh_CN.tar.gz -C  /apps/nginx/html/

2)修改属主和属组为nginx用户

[root@centos7 html]# chown -R nginx.nginx wordpress/

3)修改wordpress的配置文件填入数据库信息

[root@centos7 wordpress]#cp   wp-config-sample.php    wp-config.php
[root@centos7 wordpress]#vim wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'boss');

/** MySQL数据库用户名 */
define('DB_USER', 'boss');

/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');

/** MySQL主机 */
define('DB_HOST', '192.168.1.23');    #虚拟VIP

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
……
……

4)将wordpress整个目录复制到另一台后端服务

[root@centos7 html]# scp -p -r wordpress/*  192.168.1.109:/apps/nginx/html/

5)修改nginx的配置文件并重启nginx

[root@centos7 html]#vim /apps/nginx/conf/nginx.conf
……
location / {
                root  html/wordpress;
                index  index.php index.html index.htm;
                }
                location ~ \.php$ {
                 root        /apps/nginx/html/wordpress;
                 fastcgi_pass  127.0.0.1:9000;
                 fastcgi_index  index.php;
                 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                 include      fastcgi_params;
                 }
……

[root@centos7 html]# systemctl restart nginx

7.在NFS存储服务器上创建共享文件

1)安装nfs-utils

[root@localhost ~]# yum install nfs-utils -y

2)修改配置文件添加共享文件

[root@localhost ~]# vim /etc/exports
/data/NFS/ 192.168.1.108(rw,no_root_squash) 192.168.1.109(rw,no_root_squash)

3)启动共享服务

[root@localhost ~]# systemctl start nfs

4)查看开启的共享目录

[root@localhost ~]# exportfs -v
/data/NFS       192.168.1.108(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
/data/NFS       192.168.1.109(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)

8.将NFS共享文件挂载到两台后端的wordpress的uploads目录下,实现两台后端服务图片文件共享

mount 192.168.1.101/data/NFS  /apps/nginx/html/wordpress/wp-content/uploads

9.wordpress浏览器访问测试

keepalived+haproxy实现wordpress高可用小架构

转载于:https://blog.51cto.com/14234542/2407743

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值