Three implementation methods of Virtual Host under linux7
Same IP address, different port number
Virtual host 1: host IP address is 172.16.30.20, port number is 80 (default port), DocumentRoot is / var/www/vhost1, create the virtual host site home page file under the DocumentRoot directory.
[root@rhel7 ~] # cd / var/www/ [root@rhel7 www] # mkdir vhost1 [root@rhel7 www] # cd vhost1/ [root@rhel7 vhost1] # vim index.html [root@rhel7 vhost1] # cat index.htmlthis is the vhost1
Virtual host 2: the host IP address is 172.16.30.20, the port number is 8080 MagneDocumentRoot is / var/www/vhost2, create the virtual host site home page file under the DocumentRoot directory.
[root@rhel7 ~] # cd / var/www/ [root@rhel7 www] # mkdir vhost2 [root@rhel7 www] # cd vhost2/ [root@rhel7 vhost2] # vim index.html [root@rhel7 vhost2] # [root@rhel7 vhost2] # cat index.htmlthis is the vhost2
Edit the virtual machine host profile httpd-vhosts.conf
[root@rhel7 ~] # vim / etc/httpd/conf.d/httpd-vhosts.conf DocumentRoot "/ var/www/vhost1" Listen 8080 DocumentRoot "/ var/www/vhost2" [root@rhel7 ~] # systemctl restart httpd [root@rhel7 ~] # firewall-cmd-add-port=8080/tcp-permanentsuccess [root@rhel7] # firewall-cmd-- reloadsuccess
Access Test:
2. Different IP addresses and same port number
Virtual host 1: host IP address is 172.16.30.20, port number is 80 (default port), DocumentRoot is / var/www/vhost1, create the virtual host site home page file under the DocumentRoot directory.
Virtual host 2: host IP address is 172.16.30.200, port number is 80 (default port), DocumentRoot is / var/www/vhost2, create the virtual host site home page file in the DocumentRoot directory.
[root@rhel7 ~] # vim / etc/sysconfig/network-scripts/ifcfg-ens33TYPE=EthernetBOOTPROTO=noneDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=ens33UUID=bde41fa3-f559-4de2-ba9a-857fed211aacDEVICE=ens33ONBOOT=yesDNS1=127.0.0.1ZONE=publicIPADDR=172.16.30.10PREFIX=24IPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPADDR1=172.16.30.100PREFIX1=24 [root@rhel7 ~] # vim / etc/httpd/conf.d/httpd-vhosts.conf DocumentRoot "/ var/www/vhost1" DocumentRoot "/ var/www/vhost2" [root@rhel7 ~] # systemctl restart httpd
Access Test:
Same IP address, same port number, different FQDN
Virtual host 1: host IP address is 172.16.30.20, port number is 80 (default port), FQDN is vhost1.example.com,DocumentRoot is / var/www/vhost1, create the virtual host site home page file under the DocumentRoot directory.
Virtual host 2: the host IP address is 172.16.30.20, the port number is 80 (default port), and the FQDN is vhost2.example.com,DocumentRoot/var/www/vhost2. Create the virtual host site home page file under the DocumentRoot directory.
[root@rhel7 ~] # vim / etc/httpd/conf.d/httpd-vhosts.conf DocumentRoot "/ var/www/vhost1" ServerName vhost1.example.com DocumentRoot "/ var/www/vhost2" ServerName vhost2.example.com
Note: the corresponding parsing content needs to be added to the DNS server.
Access Test:
Remember that you need to restart the service every time you modify the configuration file.