[root@master conf]# cp httpd.conf httpd.conf.bak      #备份主配置文件。

基于ip地址的虚拟主机,首先添加一个ens33:0

[root@master html]# ip addr add 192.168.1.220/24 dev ens33 label ens33:0
[root@master html]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:21:e3:49 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.222/24 brd 192.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.1.220/24 scope global secondary ens33:0
       valid_lft forever preferred_lft forever
    inet6 2409:8a0c:4d:59d0:ade0:f328:b2f5:9e47/64 scope global noprefixroute dynamic
       valid_lft 86366sec preferred_lft 14366sec
    inet6 fe80::1592:73e7:370e:7e72/64 scope link
       valid_lft forever preferred_lft forever
[root@node2 ~]# ping 192.168.1.220
PING 192.168.1.220 (192.168.1.220) 56(84) bytes of data.
64 bytes from 192.168.1.220: icmp_seq=1 ttl=64 time=1.28 ms
64 bytes from 192.168.1.220: icmp_seq=2 ttl=64 time=0.673 ms
64 bytes from 192.168.1.220: icmp_seq=3 ttl=64 time=0.687 ms
64 bytes from 192.168.1.220: icmp_seq=4 ttl=64 time=1.06 ms
^C
--- 192.168.1.220 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 0.673/0.925/1.283/0.260 ms
[root@master www]# pwd
/var/www
[root@master www]# mkdir xdl baidu   在这两个目录下分别创建两个首页文件为index.html
[root@master www]# cd xdl/
[root@master xdl]# echo "192.168.1.220" > index.html
[root@master xdl]# cd ../baidu
[root@master baidu]# echo "192.168.1.222" > index.html

修改主配置文件                     

#DocumentRoot "/var/www/html"        停止中心主机。
<VirtualHost 192.168.1.220:80>
    DocumentRoot "/var/www/xdl"
#    ServerName dummy-host2.example.com
</VirtualHost>
<Directory "/var/www/xdl">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>
IncludeOptional conf.d/*.conf
<VirtualHost 192.168.1.222:80>
    DocumentRoot "/var/www/baidu"
#    ServerName dummy-host2.example.com
</VirtualHost>
<Directory "/var/www/baidu">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

基于端口的虚拟主机                      

Listen 8080
<VirtualHost 192.168.1.222:80>
    DocumentRoot "/var/www/xdl"
#    ServerName dummy-host2.example.com
</VirtualHost>
<Directory "/var/www/xdl">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>
IncludeOptional conf.d/*.conf
<VirtualHost 192.168.1.222:8080>
    DocumentRoot "/var/www/baidu"
#    ServerName dummy-host2.example.com
</VirtualHost>
<Directory "/var/www/baidu">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

基于域名的虚拟化                      

Listen 8080
<VirtualHost 192.168.1.222:80>
    DocumentRoot "/var/www/xdl"
    ServerName www.xdl.com
</VirtualHost>
<Directory "/var/www/xdl">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>
IncludeOptional conf.d/*.conf
<VirtualHost 192.168.1.222:80>
    DocumentRoot "/var/www/baidu"
    ServerName www.baidu.com
</VirtualHost>
<Directory "/var/www/baidu">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>
02-09 22:57