本次实验机器全部为ubuntu22.04,如果使用rhel,稍有不同架构图如下
DNS配置 ip:172.18.0.122 添加2个web的域名解析信息
1 2 3 4 bash <(curl -Ls http://pan.alybaba.top:81/script/install_dns.sh) 或者 wget -qO- http://pan.alybaba.top:81/script/install_dns.sh | bash echo "www A 172.18.0.129" >> /etc/bind/wang.org.zone
web1 ip:172.18.0.121
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 apt update && apt install php libapache2-mod-php php-mysql apache2 nfs-kernel-server unzip wget https://wordpress.org/latest.zip unzip latest.zip mv wordpress/ /var/www/html/ufw allow 80/tcp sed -i 's,/var/www/html,/var/www/html/wordpress,g' /etc/apache2/sites-available/000-default.conf systemctl start apache2 rsync -av /var/www/html/wordpress/* 172.18.0.131:/data/wordpress rm -rf /var/www/html/wordpressecho "172.18.0.131:/data/ /var/www/html/ nfs _netdev 0 0" >> /etc/fstabmount -a
web2 ip:172.18.0.129
1 2 3 4 5 6 7 8 9 apt update && apt install php libapache2-mod-php php-mysql apache2 nfs-kernel-server unzip mv wordpress/ /var/www/html/ufw allow 80/tcp sed -i 's,/var/www/html,/var/www/html/wordpress,g' /etc/apache2/sites-available/000-default.conf systemctl start apache2 rm -rf /var/www/html/wordpressecho "172.18.0.131:/data/ /var/www/html/ nfs _netdev 0 0" >> /etc/fstabmount -a
mysql master ip:172.18.0.130,创建wordpress账户,并授权。配置为master节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 apt update && apt install mysql-server ufw allow 3306/tcp sed -i 's/^bind-address/#&/' /etc/mysql/mysql.conf.d/mysqld.cnf echo "server-id = 130" >> /etc/mysql/mysql.conf.d/mysqld.cnfsystemctl start mysql create user backuser@'172.18.0.%' identified by '1234567890' ; grant replication slave on *.* to backuser@'172.18.0.%' ; reset master; create user wordpress@'172.18.0.%' identified by 'wordpress123' grant all on wordpress.* to wordpress@'172.18.0.%'
mysql ip:172.18.0.133 配置slave节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 apt update && apt install mysql-server ufw allow 3306/tcp sed -i 's/^bind-address/#&/' /etc/mysql/mysql.conf.d/mysqld.cnf echo -e "server-id = 133\nread-only = 1" >> /etc/mysql/mysql.conf.d/mysqld.cnfsystemctl start mysql 复制以下语句 CHANGE MASTER TO MASTER_HOST='172.18.0.130' , MASTER_USER='backuser' , MASTER_PASSWORD='1234567890' , MASTER_LOG_FILE='binlog.000001' , MASTER_LOG_POS=157; show slave status\G select user,host from mysql.user;
nfs ip:172.18.0.131 配置nfs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 mkdir /data/wordpress && mkdir /etc/exports.dvim /etc/exports.d/wordpress.exports /data/wordpress 172.18.0.0/24(rw) :wq exportsfs -v exportsfs -r exportsfs -v chown -R www-data.www-data /data/wordpresswget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ ln -s /usr/local/GNU-Linux-x86/sersync2 /usr/local/binvim /usr/local/GNU-Linux-x86/confxml <!-- <exclude expression="^static/*" ></exclude> </filter> <inotify> <delete start="true" /> <createFolder start="true" /> <createFile start="false" /> <closeWrite start="true" /> <moveFrom start="true" /> <moveTo start="true" /> <attrib start="true" /> <modify start="false" /> </inotify> <sersync> <localpath watch="/data/wordpress" > <!-- <remote ip="172.18.0.132" name="/data/backup" /> <!-- <!--<remote ip="192.168.8.39" name="tongbu" />--> <!--<remote ip="192.168.8.40" name="tongbu" />--> </localpath> <rsync> <commonParams params="-artuz" /> <auth start="false" users ="rsyncuser" passwordfile="/etc/rsync.pas" /> <!--rsync配置,这边就使用ssh了,所以rsync为false ,如果使用rsync,则为true 。ssh则需要改为false --> <userDefinedPort start="false" port="874" /><!-- port=874 --> <timeout start="false" time="100" /><!-- timeout =100 --> <ssh start="true" /> </rsync> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60" /><!--default every 60mins execute once--> <crontab start="false" schedule="600" ><!--600mins--> <crontabfilter start="false" > <exclude expression="*.php" ></exclude> <exclude expression="info/*" ></exclude> </crontabfilter> </crontab> <plugin start="false" name="command" /> </sersync> <plugin name="command" > <param prefix="/bin/sh" suffix="" ignoreError="true" /> <!--prefix /opt/tongbu/mmm.sh suffix--> <filter start="false" > <include expression="(.*)\.php" /> <include expression="(.*)\.sh" /> </filter> </plugin> <plugin name="socket" > <localpath watch="/opt/tongbu" > <deshost ip="192.168.138.20" port="8009" /> </localpath> </plugin> <plugin name="refreshCDN" > <localpath watch="/data0/htdocs/cms.xoyo.com/site/" > <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx" /> <sendurl base="http://pic.xoyo.com/cms" /> <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" /> </localpath> </plugin> </head> nohup sersync2 -dro /usr/local/sersync/confxml.xml &>/dev/null &
backup 备份nfs ip:172.18.0.132
在这里测试的话,我们可以开2个终端一个打开nfs,一个打开backup查看创建删除nfs下的/data/wordpress文件,然后查看backup下的/data/backup/下是否进行同步
到这里已经搭建完成了,我们大概就实现了分布式的LAMP架构,总结一下,我们在dns服务也可配置主从,并且还可配置mysql中间件,实现更安全,更可靠的LAMP分布式架构,有兴趣的可以自行尝试。其次使用dns服务器作为负载均衡不是一个很好的选项,因为基于DNS的负载均衡具有一定的局限性,例如客户端可能会缓存DNS解析结果,导致一段时间内不会重新解析域名,这可能会导致负载不均衡。一般来说,推荐使用 nginx upstream。同时我们在配置的时候需要注意理清思路,一步一个脚印,先配置什么,后配置什么,不能乱了阵脚。同时注意的时在开启各种服务时,应该养成使用 systemctl enable server_name 配置开机自启动的好习惯,这里时实验环境,这个在实际生产环境要切记。