分別在兩臺MySQL主機上安裝MySQL
1.使用一鍵安裝腳本安裝MySQLphp
[root@master ~]# tar xf mysql-5.6.34-onekey-install.tar.gz [root@master ~]# vim mysql-install.sh
1.修改配置文件html
[root@master ~]# vim /etc/my.cnf [mysqld] datadir=/data/mysql log-bin=/data/bin/mysql-bin binlog-format=row server-id=1
2.建立二進制日誌目錄修改權限mysql
[root@master ~]# mkdir /data/bin [root@master ~]# chmod -R 700 /data/bin [root@master ~]# chown -R mysql.mysql /data/bin
3.啓動MySQLlinux
[root@master ~]# service mysqld start Starting MySQL... SUCCESS!
4.受權主從複製帳戶nginx
[root@master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.27.%' IDENTIFIED BY '111111';"
5.查看二進制日誌位置c++
[root@master~]# mysql -e "SHOW MASTER LOGS;" +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 334 | +------------------+-----------+
1.修改配置文件web
[root@slave ~]# vim /etc/my.cnf [mysqld] server-id=2 read-only
2.啓動服務sql
[root@slave ~]# service mysqld start
3.change master to數據庫
mysql> CHANGE MASTER TO MASTER_HOST='192.168.27.31', MASTER_USER='repluser', MASTER_PASSWORD='111111', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1;
4.啓動複製線程vim
mysql> START SALVE;
在主服務器上導入測試數據庫
[root@master ~]# mysql < hellodb_innodb.sql
查看從服務器是否複製數據
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
主從配置完畢
分別在2臺web服務器上安裝nginx和php-fpm
安裝編譯所需的各類軟件
yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel bzip2 vim lrzsz tree screen lsof tcpdump wget ntpdate net-tools iotop bc zip unzip nfs-utils expat-devel bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel cmake ncurses-devel gnutls-devel libxml2-devel libevent-devel libaio-devel
1.解壓nginx源碼包
[root@web1 ~]# tar xf nginx-1.14.2.tar.gz
2.檢查編譯環境
[root@web1 ~]# cd nginx-1.14.2 [root@web1 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
3.編譯安裝
[root@web1 nginx-1.14.2]# make && make install
4.爲應用程序文件創建軟鏈接
[root@web1 nginx-1.14.2]# ln -s /apps/nginx/sbin/nginx /sbin/nginx
5.配置服務啓動腳本
[root@web1 nginx-1.14.2]# vim /lib/systemd/system/nginx.service [Service] Type=forking PIDFile=/apps/nginx/logs/nginx.pid ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
6.建立啓動用戶
[root@web1 nginx-1.14.2]# useradd -u2000 www
7.修改配置文件
[root@web1 nginx-1.14.2]# vim /apps/nginx/conf/nginx.conf user www; #服務啓動時以www用戶爲工做進程 pid /apps/nginx/logs/nginx.pid;
8.啓動服務
[root@web1 nginx-1.14.2]# nginx
1.解壓縮源碼包
[root@web1 ~]# tar xf php-7.1.30.tar.gz
2.檢查編譯環境
[root@web1 ~]# cd php-7.1.30 [root@web1 php-7.1.30]# ./configure --prefix=/apps/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
3.編譯模塊並複製模塊到指定目錄
[root@web1 php-7.1.30]# make && make install
4.複製環境配置文件,修改時區
[root@web1 php-7.1.30]# cp php.ini-production /etc/php.ini [root@web1 php-7.1.30]# sed -i '/;date.tim/s@.*@data.timezone = "Asia/Shanghai"@' /etc/php.ini
5.配置服務啓動腳本
[root@web1 php-7.1.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@web1 php-7.1.30]# chmod +x /etc/init.d/php-fpm [root@web1 php-7.1.30]# chkconfig --add php-fpm
6.複製模板配置文件
[root@web1 php-7.1.30]# cp /apps/php/etc/php-fpm.conf.default /apps/php/etc/php-fpm.conf [root@web1 php-7.1.30]# cp /apps/php/etc/php-fpm.d/www.conf.default /apps/php/etc/php-fpm.d/www.conf
1.修改nginx主配置文件導入其餘配置文件
[root@web1 ~]# vim /apps/nginx/conf/nginx.conf include /apps/nginx/conf/server/*.conf;
2.建立新的server段配置文件
[root@web1 ~]# mkdir /apps/nginx/conf/server [root@web1 ~]# vim /apps/nginx/conf/server/mylinuxops.conf server { server_name www.mylinuxops.com; listen 80; location / { root /data/www; index index.php index.html; } location ~ \.php$ { root /data/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
3.修改php-fpm配置文件
[root@web1 ~]# vim /apps/php/etc/php-fpm.d/www.conf user = www group = www listen = 127.0.0.1:9000
4.重啓服務
[root@web1 ~]# nginx -s reload [root@web1 ~]# service php-fpm start Starting php-fpm done
建立測試頁面
[root@web1 ~]# mkdir /data/www [root@web1 ~]# vim /data/www/index.php <?php phpinfo(); ?>
瀏覽器訪問
1.安裝keepalived
[root@lvs1 ~]# yum install keepalived -y
2.修改配置文件
[root@lvs1 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { root@mylinuxops.com } notification_email_from root@mylinuxops.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id s1.mylinuxops.com vrrp_skip_check_adv_addr vrrp_iptables #vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 27 priority 100 advert_int 2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.27.100 dev ens33 label ens33:0 192.168.27.101 dev ens33 label ens33:1 } unicast_src_ip 192.168.27.11 unicast_peer { 192.168.27.12 } } virtual_server 192.168.27.100 80 { delay_loop 5 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.27.21 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 5 retry 3 delay_before_retry 3 } } real_server 192.168.27.22 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 5 retry 3 delay_before_retry 3 } } } virtual_server 192.168.27.101 3306 { delay_loop 5 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.27.31 3306 { weight 1 TCP_CHECK { connect_port 3306 connect_timeout 5 retry 3 delay_before_retry 3 } } }
重啓服務
[root@lvs1 ~]# systemctl restart keepalived
查看是否存在lvs規則
[root@lvs1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.27.100:80 wrr -> 192.168.27.21:80 Route 1 0 0 -> 192.168.27.22:80 Route 1 0 0 TCP 192.168.27.101:3306 wrr -> 192.168.27.31:3306 Route 1 0 1
[root@lvs2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { root@mylinuxops.com } notification_email_from root@mylinuxops.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id s1.mylinuxops.com vrrp_skip_check_adv_addr vrrp_iptables #vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 27 priority 80 advert_int 2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.27.100 dev ens33 label ens33:0 192.168.27.101 dev ens33 label ens33:1 } unicast_src_ip 192.168.27.12 unicast_peer { 192.168.27.11 } } virtual_server 192.168.27.100 80 { delay_loop 5 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.27.21 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 5 retry 3 delay_before_retry 3 } } real_server 192.168.27.22 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 5 retry 3 delay_before_retry 3 } } } virtual_server 192.168.27.101 3306 { delay_loop 5 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.27.31 3306 { weight 1 TCP_CHECK { connect_port 3306 connect_timeout 5 retry 3 delay_before_retry 3 } } }
重啓服務
[root@lvs2 ~]# systemctl restart keepalived
查看是否存在lvs規則
[root@lvs1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.27.100:80 wrr -> 192.168.27.21:80 Route 1 0 0 -> 192.168.27.22:80 Route 1 0 0 TCP 192.168.27.101:3306 wrr -> 192.168.27.31:3306 Route 1 0 1
在web一、web二、mysql分別執行lvs_dr_rs腳本
[root@web1 ~]# bash lvs_dr_rs.sh start
腳本內容
#!/bin/bash vip=192.168.27.100 #mysql將地址改成 192.168.27.101 mask='255.255.255.255' dev=lo:1 case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
分別在後端服務器上建立測試頁面
[root@web1 ~]# echo "<h1>`hostname`</h1>" > /data/www/index.html
客戶端上測試
[root@localhost ~]# curl www.mylinuxops.com/index.html <h1>web1</h1> [root@localhost ~]# curl www.mylinuxops.com/index.html <h1>web2</h1>
暫停lvs1上的keepalived測試vip漂移
[root@lvs1 ~]# systemctl stop keepalived
查看lvs2上是否有vip
[root@lvs2 ~]# ifconfig ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.27.100 netmask 255.255.255.255 broadcast 0.0.0.0 ether 00:0c:29:00:a5:1d txqueuelen 1000 (Ethernet)
測試訪問是否正常
[root@localhost ~]# curl www.mylinuxops.com/index.html <h1>web2</h1> [root@localhost ~]# curl www.mylinuxops.com/index.html <h1>web1</h1>
測試數據鏈接
[root@localhost ~]# mysql -urepluser -p111111 -h192.168.27.101 -P3306 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 143 Server version: 5.6.34-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
建立出須要共享的目錄,並對其進行配置
[root@image ~]# mkdir /data/upload [root@image ~]# vim /etc/exports /data/upload *(rw,no_root_squash)
設置爲開機啓動
[root@image ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
1.在MySQL-Master上受權一個用於數據庫操做的帳戶,建立一個wordpress的數據庫
[root@master ~]# mysql -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'192.168.27.%' IDENTIFIED BY'111111';" [root@master ~]# mysql -e "CREATE DATABASE wordpress;" [root@master ~]# mysql -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | | wordpress | +--------------------+
2.分別在後端兩個web服務器上解壓wordpress
[root@web1 ~]# tar xf wordpress-5.0.1-zh_CN.tar.gz
3.將解壓後的數據複製到站點目錄
[root@web1 ~]# cp -a wordpress/* /data/www/
4.複製work
[root@web1 ~]# cp /data/www/{wp-config-sample.php,wp-config.php} [root@web1 ~]# vim /data/www/wp-config.php // ** MySQL 設置 - 具體信息來自您正在使用的主機 ** // /** WordPress數據庫的名稱 */ define('DB_NAME', 'wordpress'); /** MySQL數據庫用戶名 */ define('DB_USER', 'wpuser'); /** MySQL數據庫密碼 */ define('DB_PASSWORD', '111111'); /** MySQL主機 */ define('DB_HOST', '192.168.27.100'); /** 建立數據表時默認的文字編碼 */ define('DB_CHARSET', 'utf8'); /** 數據庫整理類型。如不肯定請勿更改 */ define('DB_COLLATE', ''); /**#@+ * 身份認證密鑰與鹽。 * * 修改成任意獨一無二的字串! * 或者直接訪問{@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org密鑰生成服務} * 任何修改都會致使全部cookies失效,全部用戶將必須從新登陸。 * * @since 2.6.0 */ define('AUTH_KEY', 'Q8B&gE6k?}}bh|:23l/8Pq;#q$?4HT^-riv^Tnk5UcwDIf#g`I3+RJ~^+O7>nUk6'); define('SECURE_AUTH_KEY', '?{2tjkt_KN(+:d|tWA}Th5fi!-y$xVO^sk#>F~{?B$X|#9)6S(~h`0KY?6CY|0jL'); define('LOGGED_IN_KEY', 'v:_a0a:2Eh^fj9-`&P+.i*V0#-M#jGjw<1*p/9Tea7y=q}R=Hy,Q,9qBX5Kx!ybV'); define('NONCE_KEY', '^5g3j_7:H,_=m/T/ mB`JQxUgXFFjEkhJ)OV qy&|W}aoxQ11t_j;D;yl(w;MV46'); define('AUTH_SALT', '8ZW}E[tXfRDd}$h02-hAgG]zZg4/NX}kM(K_M_Y|[fUz/C!.9|:lfcexu91kIe5q'); define('SECURE_AUTH_SALT', ']r+bt+7&_KkBE~!V+;}8fw|a8,B+-H.ELKN.}!qhFL,LZ+Vj=p0@y5gi5Fo^F'); define('LOGGED_IN_SALT', 'jGwl&sWdAh.dNiGSy`qV.-6,DzaFYE;xG;Js*ZgM(E|7a57y(_?]^-u7>;)R<UjH'); define('NONCE_SALT', '*Zl:=N-W!+B8kbaoY`-q)Mq8r7xK|I1^IT0;.ZMiB-Fh$?OtmD/+[![`I@)p~~Lw'); /**#@-*/
將此文件複製到web2服務器上
[root@web1 ~]# scp /data/www/wp-config.php 192.168.27.22:/data/www
分別在web服務器上掛載nfs,將圖片存儲到nfs上
[root@web1 ~]# mkdir /data/www/wp-content/uploads [root@web1 ~]# mount 192.168.27.23:/data/upload /data/www/wp-content/uploads
測試登陸