安裝nginx
實驗環(huán)境:192.168.11.10:NGINX負載均衡教馆,動靜分離
192.168.11.20:安裝httpd或者nginxweb服務器逊谋,安裝php
192.168.11.21:安裝httpd或者nginxweb服務器土铺,安裝php
192.168.11.22:安裝httpd或者nginxweb服務器,安裝php
可以自己下載NGINX壓縮包也可以yum安裝悲敷,下面用tar包演示安裝
官網(wǎng)下載1.18版本再通過xshell的rz傳輸進去
1. yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel make --安裝依賴包
2. 可以自己下載NGINX壓縮包也可以yum安裝,下面用tar包演示安裝后德,官網(wǎng)下載1.18版本再通過xshell的rz傳輸進去,用壓縮包安裝的方法不會自動創(chuàng)建用戶探遵,
所以我們第一步創(chuàng)建用戶:useradd -s /sbin/nologin -M nginx,第二步解壓:tar -zxvf nginx-1.18.0.tar.gz
第三步開啟全部模塊:./configure --prefix=/usr/local/nginx --user=nginx--group=nginx --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre
第四步編譯:make涯穷,服務器CPU6核的話可以make -j 6,第五步安裝:make install藏雏,第六步進入安裝后的目錄:cd /usr/local/nginx/,
看到有四個文件夾就可[root@LVS nginx]# ls
conf html logs sbin
3. netstat -atunp | grep 80 --查看是否有NGINX的80端口
./nginx -s reload --重載
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ 創(chuàng)建快捷方式到/usr/sbin/
nginx -s stop --關(guān)閉
4. [root@LVS sbin]# nginx -t --檢查是否有錯誤赚瘦,顯示以下信息表示沒錯
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5. vim /usr/local/nginx/conf/nginx.conf --打開配置文件
location部分添加如下:
43 location / {
44 root html;
45 index index.html index.htm;
46 if ($request_uri ~* \.html$){
47 proxy_pass http://htmlservers;
48 }
49 if ($request_uri ~* \.php$){
50 proxy_pass http://phpservers;
51 }
52 proxy_pass http://picservers;
53 }
在最后的花括號上一行添加如下:
123 upstream htmlservers {
124 server 192.168.11.21:80;
125 server 192.168.11.22:80;
126 }
127 upstream phpservers {
128 server 192.168.11.20:80;
129 }
130 upstream picservers {
131 server 192.168.11.21:80;
132 server 192.168.11.22:80;
133 }
6. nginx -s reload --重載一下
安裝haproxy
- 實驗環(huán)境:192.168.11.10:haproxy負載均衡服務器
192.168.11.20:rs1奏寨,裝好httpd,固定ip病瞳,同網(wǎng)段可ping通,可ping百度
192.168.11.21:rs2套菜,裝好httpd,固定ip逗柴,同網(wǎng)段可ping通,可ping百度
192.168.11.22:rs3渣蜗,裝好httpd屠尊,固定ip袍睡,同網(wǎng)段可ping通,可ping百度
1. tar -zxvf haproxy-2.4.4.tar.gz --先從其他地方下載好tar包用xshell的rz傳輸過去斑胜,假設我是在/opt下解壓的
2. cd haproxy-2.4.4/ --安裝的步驟可參考vim README和vim Makefile文檔
3. make TARGET=linux-glibc PREFIX=/usr/local/haproxy --編譯到指定路徑
4. make install PREFIX=/usr/local/haproxy --指定安裝路徑嫌吠,可以用echo $?查詢上條命令錯誤1或成功0
5. cd /usr/local/haproxy/
6. mkdir etc --存配置文件的文件夾
7. vim /usr/local/haproxy/etc/haproxy.cfg --新建配置文件,內(nèi)容如下凭戴,全為小寫:
1 global
2 log 127.0.0.1 local0
3 #Log 127.0.0.1 Local1 notice
4 #Log loghost local0 info
5 maxconn 4096
6 chroot /usr/local/haproxy
7 uid 99 #所屬運行的用戶uid
8 gid 99
9 #所屬運行的用戶組
10 daemon #以后臺形式運行haproxy
11 nbproc 1
12 #啟動1個haproxy實例。# #工作進程數(shù)量(CPU數(shù)量)么夫,實際工作中肤视,應該設置成和CPU核>心數(shù)-一樣档痪。這樣可 以發(fā)揮出最大的性能邢滑。
13 pidfile /usr/local/haproxy/run/haproxy.pid #將 所有進程寫入pid文件
14 #debug #調(diào)試錯誤時用
15 #quiet #安靜
16 defaults
17 log global
18 log 127.0.0.1 local3 #日志文件的輸出定向。產(chǎn)生的日志級別為Local3.系統(tǒng)中Local1-7, 用戶自己定義
19 mode http
20 #工作模式困后,所處理的類別,默認采用http模式,可配置成tcp作4層消息轉(zhuǎn)發(fā)
21 option httplog
22 #日志類別摇予,記載http日志
23 option httpclose #每次請求完畢后主動關(guān)閉htt通道, haproxy不支持keep- alive,只能模擬這種模式的實現(xiàn)
24 #每次請求完畢后主動關(guān)閉htt通道,haxy不支持備存宁昭,只能模擬這種模式的實現(xiàn) option
25 option dontlognull #不記錄空連接,產(chǎn)生的日志
26 #不記錄空連接久窟,產(chǎn)生的日志 option
27 option forwardfor #如果后端服務器需要獲得客戶端真實ip需要配置的參數(shù)本缠,可以從Http Header中獲得客戶端i
28 option redispatch #當serverid對應的服務器掛掉后,強制定向到其他健康服務器
29 #當serverid對應的服務器掛掉后,強制定向到其他健康服務器
30 retries 2 #2次連接失敗就認為服務器不可用稀颁,主要通過后面的check檢查
31 #2次連接失敗就認為服務器不可用,主要通過后面的檢查檢查 maxconn 2000
32 maxconn 2000 #最大連接數(shù)
33 balance roundrobin #負載均衡算法
34 stats uri /haproxy- stats #haproxy監(jiān)控頁面的訪問地址#可通過http://LocaLhost: 80/haproxy-stats
35 timeout connect 5000 #連接超時時間棱烂。單位:ms毫秒
36 timeout client 5000 #客戶端連接超時時間
37 timeout server 5000 #服務器端連接超時時間
38 mode http
39 option httpchk GET /index.html
40 #健康檢測#注意實際工作中測試時,應該下載某-一個 頁面來進行測試颊糜,因此這個頁面應該是個小頁面,而不要用首頁面.這里是每隔一-秒檢查一次頁面.
41 frontend http
42 bind 0.0.0.0:80
43 default_backend http_back
44 backend http_back
45 server rs1 192.168.11.20:80 weight 3 check --權(quán)重可以設成不一樣
46 server rs2 192.168.11.21:80 weight 3 check
47 server rs3 192.168.11.22:80 weight 3 check
8. 制作啟動haproxy服務的腳本:
cd /opt/haproxy-2.4.4/examples/ --/opt下解壓的tar包衬鱼,會有原始文件夾
9. cp haproxy.init /etc/init.d/haproxy --復制啟動腳本的例子到/etc/init.d/haproxy
步驟8和9可以合并cp /opt/haproxy-2.4.4/examples/haproxy.init /etc/init.d/haproxy
10. chmod 777 /etc/init.d/haproxy --給755或者干脆給777權(quán)限
11. vim /etc/init.d/haproxy --編輯啟動腳本憔杨,修改如下內(nèi)容:
1 #!/bin/sh
2 #
3 # chkconfig: - 85 15
4 # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited \
5 # for high availability environments.
6 # processname: haproxy
7 # config: /usr/local/haproxy/etc/haproxy.cfg
8 # pidfile: /usr/local/haproxy/run/haproxy.pid
9
10 # Script Author: Simon Matter <simon.matter@invoca.ch>
11 # Version: 2004060600
12
13 # Source function library.
14 if [ -f /etc/init.d/functions ]; then
15 . /etc/init.d/functions
16 elif [ -f /etc/rc.d/init.d/functions ] ; then
17 . /etc/rc.d/init.d/functions
18 else
19 exit 0
20 fi
21
22 # Source networking configuration.
23 . /etc/sysconfig/network
24
25 # Check that networking is up.
26 [ ${NETWORKING} = "no" ] && exit 0
27
28 # This is our service name
29 BASENAME=`haproxy`
30
31 BIN=/usr/sbin/haproxy
32
33 CFG=/usr/local/haproxy/etc/haproxy.cfg
34 [ -f $CFG ] || exit 1
35
36 PIDFILE=/usr/local/haproxy/run/haproxy.pid
37 LOCKFILE=/usr/local/haproxy/run/haproxy
38
39 RETVAL=0
對照以上內(nèi)容修改,內(nèi)容中有些文件夾還沒有消别,所以下面還得創(chuàng)建文件夾,不然腳本會報錯
12. mkdir /usr/local/haproxy/run --腳本里有這個文件夾寻狂,所以得創(chuàng)建
13. [root@LVS examples]# ls /usr/local/haproxy/ --創(chuàng)建好了
doc etc run sbin share
14. chown nobody -R /usr/local/haproxy/ --給nobody用戶啟動腳本執(zhí)行權(quán)限
15. ls /etc/init.d/ --查看是否為綠色,綠色為可執(zhí)行蒿偎,不是綠色就chmod 755或者777給權(quán)限
16. vim /etc/rsyslog.conf --編輯haproxy日志文件,添加內(nèi)容local3诉位,local0:
72 # Save boot messages also to boot.log
73 local7.* /var/log/boot.log
74 local3.* /var/log/haproxy.log
75 local0.* /var/log/haproxy.log
76
77
78 # ### begin forwarding rule ###
17. systemctl restart rsyslog --重啟日志服務
18. /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg --啟動haproxy服務
這里查看一下80端口是否被haproxy占用
[root@LVS init.d]# netstat -atunp | grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 48072/haproxy
udp 0 0 0.0.0.0:40442 0.0.0.0:* 48072/haproxy
udp 0 0 0.0.0.0:56966 0.0.0.0:* 47920/haproxy
19. kill -9 PID --例如 kill -9 47705菜枷,關(guān)閉該服務
20. /etc/init.d/haproxy start/stop/status --也可利用腳本啟動,關(guān)閉啤誊,查看狀態(tài)
21. http://192.168.11.10/haproxy-stats --啟動完服務,在瀏覽器輸入監(jiān)控網(wǎng)頁
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者