基于多IP地址Web服务
【Centos7.4版本】
!!!测试环境我们首关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld [root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config [root@localhost ~]# setenforce 0
一、配置多IP地址Web服务
1、首先先看一下本地网卡的信息
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cat ifcfg-ens32 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=10.0.0.129 //我本机主网卡P地址 NETMASK=255.255.255.0 GATEWAY=10.0.0.2 DNS1=8.8.8.8 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens32 UUID=e4b11756-1775-4b6a-adbf-95f3f24f941e DEVICE=ens32 ONBOOT=yes
2、我们使用ens32网卡作为主网卡,然后复制一个子网卡ens32:2(子网卡冒号后面的数字代表子网卡的编号,可任意取)
[root@localhost network-scripts]# cp ifcfg-ens32 ifcfg-ens32:2
3、编辑子网卡的配置信息
[root@localhost network-scripts]# vim ifcfg-ens32:2 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=10.0.0.22 //改与主网卡同网段的其他地址信息(但不能使用该网段的三个不难使用的IP) NETMASK=255.255.255.0 GATEWAY=10.0.0.2 //我的虚拟机NAT网卡的网关默认的是10.0.0.2 DNS1=8.8.8.8 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens32:2 //名字可改也可以不改,影响不大 UUID=e4b11756-1775-4b6a-adbf-95f3f24f941e DEVICE=ens32:2 //块设备的名字必须改成子网卡的名字 ONBOOT=yes
4、重启网络服务,查看网卡配置信息
[root@localhost network-scripts]# systemctl restart network [root@localhost network-scripts]# ifconfig ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.129 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::fe04:212a:5e53:cec4 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:b3:89:a5 txqueuelen 1000 (Ethernet) RX packets 23547 bytes 29611303 (28.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5069 bytes 654850 (639.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens32:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.22 netmask 255.255.255.0 broadcast 10.0.0.255 ether 00:0c:29:b3:89:a5 txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 152 bytes 13128 (12.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 152 bytes 13128 (12.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
5、使用YUM安装HTTP服务
[root@localhost ~]# yum install -y httpd
6、启动http服务,设置开机自启
[root@localhost ~]# systemctl start httpd [root@localhost ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service
7、查看http监听端口是否开启(监听端口没开的话,后面就访问不了主机信息)
[root@localhost ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1005/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1387/master tcp6 0 0 :::80 :::* LISTEN 14164/httpd tcp6 0 0 :::22 :::* LISTEN 1005/sshd tcp6 0 0 ::1:25 :::* LISTEN 1387/master udp 0 0 0.0.0.0:68 0.0.0.0:* 10151/dhclient udp 0 0 0.0.0.0:15648 0.0.0.0:* 10151/dhclient udp 0 0 127.0.0.1:323 0.0.0.0:* 722/chronyd udp6 0 0 :::23051 :::* 10151/dhclient udp6 0 0 ::1:323 :::* 722/chronyd
8、要编辑多IP的虚拟主机,首先看看配置模板
[root@localhost ~]# rpm -qa | grep http httpd-tools-2.4.6-93.el7.centos.x86_64 httpd-2.4.6-93.el7.centos.x86_64 [root@localhost ~]# vim /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf ............ //上面的我省略了 <VirtualHost *:@@Port@@> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/var/log/httpd/dummy-host.example.com-error_log" CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:@@Port@@> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log" CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common </VirtualHost> //上面的最后内容就是虚拟主机的配置模板
9、创建每个IP对应的主目录,并写入测试信息
[root@localhost ~]# mkdir /var/www/test1 [root@localhost ~]# mkdir /var/www/test2 [root@localhost ~]# echo '<h1>This host port is 10.0.0.129</h1>' > /var/www/test1/index.html [root@localhost ~]# echo '<h1>This host port is 10.0.0.22</h1>' > /var/www/test2/index.html
10、编辑http主配置文件,编辑两个<VirtualHost>内容
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf .......... //上面的内容我省略了 <VirtualHost 10.0.0.129:80> documentroot "/var/www/test1/" ErrorLog "/var/log/httpd/10.0.0.129-error_log" CustomLog "/var/log/httpd/10.0.0.129-access_log" common </VirtualHost> <VirtualHost 10.0.0.22:80> documentroot "/var/www/test2/" errorlog "/var/log/httpd/10.0.0.22-error_log" CustomLog "/var/log/httpd/10.0.0.22-access_log" common </VirtualHost> //上面的就是需要添加的虚拟主机配置信息
11、重启httpd服务
[root@localhost ~]# systemctl restart httpd
12、在浏览地址栏分别输入两个IP地址:10.0.0.129和10.0.0.22