關於Nginx的原理概述及詳細配置請參考博文:Centos 7部署Nginx網站服務html
[root@centos01 ~]# yum -y install prce-devel zlib-devel <!--安裝Nginx依賴--> [root@centos01 ~]# useradd -M -s /sbin/nologin nginx <!--建立管理Nginx帳戶--> [root@centos01 ~]# umount /mnt/ <!--切換Linux光盤-> [root@centos01 ~]# mount /dev/cdrom /mnt/ <!--掛載光盤--> mount: /dev/sr0 寫保護,將以只讀方式掛載 [root@centos01 ~]# scp /mnt/nginx-1.6.0.tar.gz root@192.168.100.20:/root <!--複製Nginx包到第二臺Nginx服務器--> The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes--> Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts. root@192.168.100.20's password: nginx-1.6.0.tar.gz 100% 784KB 68.2MB/s 00:00 [root@centos01 ~]# scp /mnt/haproxy-1.4.24.tar.gz root@192.168.100.30:/root <!--複製haproxy軟件包到100.30服務器--> The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes--> Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts. root@192.168.100.30's password: haproxy-1.4.24.tar.gz 100% 817KB 31.1MB/s 00:00 00:00 [root@centos01 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/ <!--解壓縮nginx包--> [root@centos01 ~]# cd /usr/src/nginx-1.6.0/ <!--進入nginx目錄--> [root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx <!--配置nginx--> [root@centos01 nginx-1.6.0]# make && make install <!--編輯及安裝nginx--> [root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--優化執行命令--> [root@centos01 ~]# echo "192.168.100.10:nginx" > /usr/local/nginx/html/index.html <!--建立nginx網站主頁,寫入測試數據--> [root@centos01 ~]# nginx <!--啓動nginx服務--> [root@centos01 ~]# netstat -anptu | grep nginx <!--監聽Nginx服務端口號--> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3685/ngin: master
[root@centos02 ~]# yum -y install pcre-devel zlib-devel <!--安裝依賴軟件包--> [root@centos02 ~]# ls anaconda-ks.cfg initial-setup-ks.cfg nginx-1.6.0.tar.gz [root@centos02 ~]# tar zxvf nginx-1.6.0.tar.gz -C /usr/src/ <!--解壓縮nginx軟件包--> [root@centos02 ~]# useradd -M -s /sbin/nologin nginx <!--建立管理nginx帳戶--> [root@centos02 ~]# cd /usr/src/nginx-1.6.0/ [root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx <!--配置nginx--> [root@centos02 nginx-1.6.0]# make && make install <!--編譯及安裝--> [root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--優化執行路徑--> [root@centos02 ~]# echo "192.168.100.20:nginx" > /usr/local/nginx/html/index.html <!--建立nginx網站主頁,寫入測試數據--> [root@centos02 ~]# nginx <!--啓動nginx服務--> [root@centos02 ~]# netstat -anptu | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6059/ngin: master
Haproxy概述及工做原理詳細配置參考博文:Haproxy搭建Web羣集概述
Centos 7基於Haproxy搭建高可用Web羣集linux
[root@centos03 ~]# yum -y install pcre-devel bzip2-devel <!--安裝依賴程序--> [root@centos03 ~]# ls anaconda-ks.cfg haproxy-1.4.24.tar.gz initial-setup-ks.cfg [root@centos03 ~]# tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/ <!--解壓縮haproxy壓縮包--> [root@centos03 ~]# cd /usr/src/haproxy-1.4.24/ [root@centos03 haproxy-1.4.24]# make TARGET=linux26 <!--編譯haproxy支持64位系統--> [root@centos03 haproxy-1.4.24]# make install <!--安裝haproxy-->
[root@centos03 ~]# mkdir /etc/haproxy <!--建立保存haproxy配置文件目錄--> [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/ <!--生成配置文件--> [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy <!--建立haproxy服務控制腳本--> [root@centos03 ~]# chmod +x /etc/init.d/haproxy <!--添加執行權限--> [root@centos03 ~]# chkconfig --add haproxy <!--添加爲系統服務--> [root@centos03 ~]# chkconfig --level 35 haproxy on <!--設置開機自動啓動--> [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/haproxy /usr/sbin/ <!--優化程序執行命令--> [root@centos03 ~]# mkdir -p /usr/share/haproxy <!--建立服務運行的臨時目錄-->
[root@centos03 ~]# vim /etc/haproxy/haproxy.cfg <!--修改haproxy主配置文件--> listen nginx 192.168.100.30:80 <!--Haproxy服務器IP地址--> balance roundrobin server web01 192.168.100.10:80 check inter 2000 fall 3 <!--第一臺Nginx的IP--> server web02 192.168.100.20:80 check inter 2000 fall 3 <!--第二臺Nginx的IP--> [root@centos03 ~]# /etc/init.d/haproxy start <!--啓動haproxy服務--> Starting haproxy (via systemctl): [ 肯定 ]
關於Firewalld防火牆的概述及詳細配置請參考博文:Centos 7的Firewalld防火牆基礎
Centos 7的firewalld防火牆地址假裝和端口轉發原理
centos 7之firewalld防火牆配置IP假裝和端口轉發案例詳解nginx
[root@centos04 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens34 <!--複製ens34網卡配置文件--> [root@centos04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34 <!--編輯ens34網卡配置文件--> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens34 DEVICE=ens34 ONBOOT=yes IPADDR=192.168.200.254 <!--外網的IP地址--> NATEMASK=255.255.255.0 DNS1=192.168.200.254 <!--添加DNS--> [root@centos04 ~]# systemctl restart network <!--從新啓動網卡服務--> [root@centos04 ~]# vim /etc/sysctl.conf <!--開啓路由功能--> net.ipv4.ip_forward = 1 [root@centos04 ~]# sysctl -p <!--刷新配置--> [root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--編輯ens32網卡配置文件--> GATEWAY=192.168.100.40 <!--網站服務器和Haproxy服務器添加網關(內網全部服務器都要添加網關)--> [root@centos01 ~]# systemctl restart network <!--從新啓動網卡服務--> [root@centos04 ~]# systemctl start firewalld.service <!--啓動防火牆--> [root@centos04 ~]# systemctl enable firewalld.service <!--設置開機自動啓動--> [root@centos04 ~]# firewall-cmd --add-interface=ens34 --zone=external <!--將ens34接口加入到external區域--> The interface is under control of NetworkManager, setting zone to 'external'. success [root@centos04 ~]# firewall-cmd --add-interface=ens32 --zone=trusted <!--將ens32接口加入到trusted區域--> The interface is under control of NetworkManager, setting zone to 'trusted'. success [root@centos04 ~]# firewall-cmd --get-active-zones <!--查看全部激活的區域--> external interfaces: ens34 trusted interfaces: ens32 [root@centos04 ~]# firewall-cmd --remove-masquerade --zone=external <!--關閉默認的IP地址假裝--> success [root@centos04 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=192.168.100.0/24 masquerade' <!--external區域配置IP地址假裝--> success [root@centos04 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 destination address=192.168.200.254/32 forward-port port=80 protocol=tcp to-addr=192.168.100.30' <!--配置端口映射;將trusted區域的192.168.100.30的80端口映射到external區域的 192.168.200.254的80端口--> success [root@centos04 ~]# firewall-cmd --zone=external --add-service=http <!--external區域容許http協議--> success [root@centos04 ~]# firewall-cmd --zone=external --add-service=dns <!--external區域容許 dns協議--> success [root@centos04 ~]# firewall-cmd --zone=external --list-all<!--查看external區域的詳細信息--> external (active) target: default icmp-block-inversion: no interfaces: ens34 sources: services: ssh http dns ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.100.0/24" masquerade rule family="ipv4" destination address="192.168.200.254/32" forward-port port="80" protocol="tcp" to-addr="192.168.100.30"
關於DNS詳細配置及概述請參考博文:CentOS7簡單搭建DNS服務web
[root@centos04 ~]# yum -y install bind bind-chroot bind-utils <!--安裝依賴軟件--> [root@centos04 ~]# echo "" > /etc/named.conf [root@centos04 ~]# vim /etc/named.conf <!--編輯DNS主配置文件--> options { listen-on port 53 { any; }; directory "/var/named"; }; zone benet.com IN { type master; file "benet.com.zone"; }; [root@centos04 ~]# named-checkconf -z /etc/named.conf <!--檢查DNS主配置文件是否錯誤--> [root@centos04 ~]# vim /var/named/benet.com.zone <!--編輯benet.com正向解析區域配置文件--> $TTL 86400 @ SOA benet.com. root.benet.com.( 2020021801 1H 15M 1W 1D ) @ NS centos04.benet.com. centos04 A 192.168.200.254 www A 192.168.200.254 [root@centos04 ~]# named-checkzone benet.com /var/named/benet.com.zone <!--檢查正向解析區域配置文件是否錯誤--> zone benet.com/IN: loaded serial 2020021801 OK [root@centos04 ~]# chmod +x /var/named/benet.com.zone <!--正向解析區域配置文件添加執行權限--> [root@centos04 ~]# chown named:named /var/named/benet.com.zone<!--修改屬組屬組--> [root@centos04 ~]# systemctl start named <!--啓動服務--> [root@centos04 ~]# systemctl enable named <!--設置服務開機自動啓動-->
一、客戶端配置IP地址、添加DNS地址vim
二、客戶端使用域名訪問centos
三、客戶端更換IP地址從新訪問服務器
———————— 本文至此結束,感謝閱讀 ————————負載均衡