haproxy实现discuz论坛的动静分离和负载均衡
一、實(shí)驗(yàn)拓?fù)鋱D
二、實(shí)驗(yàn)步驟
A、實(shí)驗(yàn)環(huán)境準(zhǔn)備
1、在node2、node3、node4上安裝httpd
[root@localhost?~]#?yum?-y?install?httpd2、在node3和node4上安裝php 、php-mysql
[root@localhost?~]#?yum?-y?install?php?php-mysql3、在172.18.250.193上安裝mariadb,并進(jìn)行配置
[root@byq?~]#yum??-y?install?mariadb啟動(dòng)數(shù)據(jù)庫
[root@byq?~]#systemctl?start?mariadb檢查端口
[root@byq?~]#ss?-tnl State??????Recv-Q?Send-Q?Local?Address:Port???????????????Peer?Address:Port?????????????? LISTEN?????0??????50????????????????*:3306??????????????????????????*:*配置數(shù)據(jù)庫相關(guān)內(nèi)容
[root@byq?~]#mysql Welcome?to?the?MariaDB?monitor.??Commands?end?with?;?or?\g. Your?MariaDB?connection?id?is?2 Server?version:?5.5.52-MariaDB?MariaDB?ServerCopyright?(c)?2000,?2016,?Oracle,?MariaDB?Corporation?Ab?and?others.Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement.MariaDB?[(none)]>?create?database?dz; MariaDB?[(none)]>?grant?all?privileges?on?dz.*?to?'dzadmin'@'%'?identified?by?'123456'; MariaDB?[(none)]>?grant?all?privileges?on?dz.*?to?'dzadmin'@'localhost'?identified?by?'123456'; MariaDB?[(none)]>?flush?privileges; MariaDB?[(none)]>?show?databases; +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?dz?????????????????| |?mysql??????????????| |?performance_schema?| |?test???????????????| +--------------------+ 5?rows?in?set?(0.00?sec) MariaDB?[(none)]>?exit Bye遠(yuǎn)程登陸測試創(chuàng)建的數(shù)據(jù)庫用戶是否正常連接
[root@BYQ?~]#mysql?-udzadmin?-p123456?-h172.18.250.193 Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g. Your?MySQL?connection?id?is?3 Server?version:?5.5.52-MariaDB?MariaDB?ServerCopyright?(c)?2000,?2013,?Oracle?and/or?its?affiliates.?All?rights?reserved.Oracle?is?a?registered?trademark?of?Oracle?Corporation?and/or?its affiliates.?Other?names?may?be?trademarks?of?their?respective owners.Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement.mysql>4、編輯node2、node3、node4中httpd的配置文件httpd.conf
[root@localhost?~]#?vim?/etc/httpd/conf/httpd.conf修改添加如下內(nèi)容
ServerName?node2.byq.com:80 DocumentRoot?"/var/www/html/upload"ServerName?node3.byq.com:80 DocumentRoot?"/var/www/html/upload"ServerName?node4.byq.com:80 DocumentRoot?"/var/www/html/upload"這里配置完畢是無法啟動(dòng)httpd服務(wù)的,因?yàn)閡pload目錄還未生成,等下面配置完畢再啟動(dòng)httpd服務(wù)
5、在node3上下載 Discuz安裝文件
[root@BYQ?~]#wget?http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip解壓并安裝
[root@BYQ?~]#mkdir?Discuz [root@BYQ?~]#unzip?-d?/root/Discuz?Discuz_X3.3_SC_UTF8.zip將解壓后的所有文件拷貝至/var/www/html
[root@BYQ?~]#cp?-a?/root/Discuz/*?/var/www/html/給所有目錄及文件配置權(quán)限
[root@BYQ?~]#chmod?-R?777?/var/www/html/upload/*安裝 Discuz
這里就做解釋了,安裝步驟就是確認(rèn),下一步即可
將配置好的文件分別scp到node3和node4的對(duì)應(yīng)目錄下
[root@BYQ?~]#scp?-r?/var/www/html/*?172.18.249.57:/var/www/html/ [root@BYQ?~]#scp?-r?/var/www/html/*?172.18.10.11:/var/www/html/6、將node3和node4的靜態(tài)文件存放目錄掛載到node2下
#/var/www/html/upload中需要掛載的目錄如下 /data???-----數(shù)據(jù)緩存及附件 /static?-----靜態(tài)文件將node2設(shè)置為NFS服務(wù)器
[root@localhost?~]#?yum?install?nfs-utils?rpcbind [root@localhost?~]#?service?rpcbind?start編輯共享文件配置
[root@localhost?~]#?vim?/etc/exports #NFS輸出目錄為data,可以訪問這個(gè)目錄的主機(jī)為node3和node4,權(quán)限為讀寫,訪問uid為0(root),允許超過1024的端口號(hào)>連接 /var/www/html/upload/data/??172.18.10.10(rw,all_squash,anonuid=0,insecure) /var/www/html/upload/data/??172.18.10.11(rw,all_squash,anonuid=0,insecure) #NFS輸出目錄為data,可以訪問這個(gè)目錄的主機(jī)為node3和node?4,權(quán)限為讀寫,訪問uid為0(root),允許超過1024的端口號(hào)>連接 /var/www/html/upload/static/??172.18.10.10(rw,all_squash,anonuid=0,insecure) /var/www/html/upload/static/??172.18.10.11(rw,all_squash,anonuid=0,insecure)啟動(dòng)nfs服務(wù)
[root@localhost ~]# service nfs start
查看共享文件目錄
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/var/www/html/upload/static 172.18.10.11,172.18.10.10
/var/www/html/upload/data ? 172.18.10.11,172.18.10.10
啟動(dòng)httpd服務(wù)
[root@localhost ~]# service httpd ?start
7、在node3上掛載共享存儲(chǔ)目錄
[root@BYQ?~]#?yum?install?nfs-utils?rpcbind [root@BYQ?~]#?service?rpcbind?start [root@BYQ?~]#?service?nfs?start [root@BYQ?~]#mount?172.18.249.57:/var/www/html/upload/data/?/var/www/html/upload/data/ [root@BYQ?~]#mount?172.18.249.57:/var/www/html/upload/static/?/var/www/html/upload/static/ [root@BYQ?~]#showmount?-e?172.18.249.57 Export?list?for?172.18.249.57: /var/www/html/upload/static?172.18.10.11,172.18.10.10 /var/www/html/upload/data???172.18.10.11,172.18.10.108、在node4上掛載共享存儲(chǔ)目錄
[root@localhost?~]#?yum?install?nfs-utils?rpcbind [root@localhost?~]#?service?rpcbind?start [root@localhost?~]#?service?nfs?start [root@localhost?~]#?yum?install?nfs-utils?rpcbind [root@localhost?~]#?service?rpcbind?start [root@localhost?~]#?service?nfs?start [root@localhost?~]#mount?172.18.249.57:/var/www/html/upload/data/?/var/www/html/upload/data/ [root@localhost?~]#mount?172.18.249.57:/var/www/html/upload/static/?/var/www/html/upload/static/ [root@localhost?~]#showmount?-e?172.18.249.57 Export?list?for?172.18.249.57: /var/www/html/upload/static?172.18.10.11,172.18.10.10 /var/www/html/upload/data???172.18.10.11,172.18.10.109、在node上安裝haproxy
開啟日志功能
[root@localhost?~]#?vim?/etc/rsyslog.conf #打開或增加一下幾行,讓udp514端口處于監(jiān)聽狀態(tài),并將日志紀(jì)錄到指定文件中 $ModLoad?imudp $UDPServerRun?514 local2.*????????????????????????????????????????????????/var/log/haproxy.log [root@localhost?~]#?systemctl?restart?rsyslog.service [root@localhost?~]#?ss?-unl?|?grep?514 UNCONN?????0??????0????????????*:514??????????????????????*:*?????????????????? UNCONN?????0??????0???????????:::514?????????????????????:::*配置haproxy
[root@localhost?~]#yum?install?haproxy [root@localhost?~]#cp?/etc/haproxy/haproxy.cfg?/etc/haproxy/haproxy.cfg.bak? [root@localhost?~]#vim?/etc/haproxy/haproxy.cfg global????#全局配置log?????????127.0.0.1?local2????#日志紀(jì)錄位置chroot??????/var/lib/haproxy????#haproxy的工作目錄pidfile?????/var/run/haproxy.pid????#pid文件位置maxconn?????4000????????????????????#最大連接數(shù)user????????haproxy????????????????#運(yùn)行時(shí)使用的用戶身份group???????haproxy????????????????#運(yùn)行時(shí)使用的組身份daemon????????????????????????????#啟動(dòng)為守護(hù)進(jìn)程,不加此處運(yùn)行在前臺(tái)stats?socket?/var/lib/haproxy/stats????#本地訪問stats統(tǒng)計(jì)信息時(shí)以套接字方式通信defaults????#默認(rèn)配置mode????????????????????http????#已http模式運(yùn)行l(wèi)og?????????????????????global????#默認(rèn)日志為全局配置中日志的設(shè)置option??????????????????httplogoption??????????????????dontlognulloption?http-server-closeoption?forwardfor???????except?127.0.0.0/8????#除本機(jī)外所有發(fā)往服務(wù)器的請(qǐng)求首部中加入“X-Forwarded-For”首部option??????????????????redispatchretries?????????????????3timeout?http-request????10stimeout?queue???????????1mtimeout?connect?????????10stimeout?client??????????1mtimeout?server??????????1mtimeout?http-keep-alive?10stimeout?check???????????10smaxconn?????????????????3000????#前端最大并發(fā)連接數(shù)listen?static????#設(shè)置統(tǒng)計(jì)報(bào)告頁面bind?*:9000????#監(jiān)聽在本機(jī)9000端口stats?enable????#打開stats?hide-version????#隱藏haproxy版本stats?uri?/haadmin?admin??#統(tǒng)計(jì)頁面路徑stats?realm?"HAProxy\?Static"????#打開統(tǒng)計(jì)頁面的認(rèn)證功能stats?auth?byq:byq123?????????#進(jìn)入統(tǒng)計(jì)頁面所使用的賬號(hào)byq和密碼byq123stats?auth?administrator:administratorstats?admin?if?TRUE????????????#條件滿足時(shí)進(jìn)入管理級(jí)別frontend??dz????#前端設(shè)置bind?*:80????#監(jiān)聽在80端口acl?url_static??path_beg??-i??/data?/static?/p_w_picpaths?/javascript?/stylesheets????#url開頭為這些的靜態(tài)內(nèi)容acl?url_static??path_end??-i??.jpg?.gif?.png?.css?.js?.html?.ico????#url結(jié)尾帶為這些的靜態(tài)內(nèi)容use_backend?staser??if?url_static????#如果靜態(tài)內(nèi)容符合url_static的條件,就調(diào)度到staser中的服務(wù)器default_backend?????????????dyser????#其他默認(rèn)調(diào)度到dyser中的服務(wù)器backend?dyser????#后端動(dòng)態(tài)內(nèi)容服務(wù)器設(shè)置cookie?srv?insert?nocachebalance?????roundrobinserver??????node3???????172.18.10.10:80?checkserver??????node4???????172.18.10.11:80?checkbackend?staser????#后端靜態(tài)內(nèi)容服務(wù)器設(shè)置balance?????roundrobinserver??????node2???????172.18.249.57:80?check[root@localhost?~]#?service?haproxy?start修改node2、node3、node4的http配置文件,讓他們能收到真實(shí)訪問的ip[root@localhost?~]#?vim?/etc/httpd/conf/httpd.conf #修改一下內(nèi)容LogFormat?"%{X-Forwarded-For}i?%l?%u?%t?\"%r\"?%>s?%b?\"%{Referer}i\"?\"%{User-Agent}i\""?combined [root@localhost?~]#?service?httpd?restart #node3和node4執(zhí)行同樣的操作轉(zhuǎn)載于:https://blog.51cto.com/12876758/1951302
總結(jié)
以上是生活随笔為你收集整理的haproxy实现discuz论坛的动静分离和负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机架构及开机过程
- 下一篇: python-------装饰器