1 There are 3 ways to implement Apache virtual host.
- IP-based virtual hosting
- port-based virtual hosting
- Domain-Based Web Hosting
2.1 Preparations for enabling virtual hosts
2.1.1 Install httpd
[root@mail httpd]# yum install httpd -y
2.1.2 Disable the default host mode
[root@mail httpd]# vim /etc/httpd/conf/httpd.conf Comment the following line #DocumentRoot "/var/www/html"
2.2 IP-based virtual host configuration
2.2.1 Add multiple IPs to the host
[root@localhost conf.d]# ip addr show dev eth0 #View original IP
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:77 :77:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
inet6 fe80::20c:29ff:fe77:777d/64 scope link
valid_lft forever preferred_lft forever
[root @localhost conf.d]# ip addr add 192.168.137.201/24 dev eth0 #Add an IP
[root@localhost conf.d]# ip addr show dev eth0 #View the added IP information, there are 2 IP addresses at this time . 200,201
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.201/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe77:777d/64 scope link
valid_lft forever preferred_lft forever
2.2.2 Add virtual host configuration file
[root@mail conf.d]# cd /etc/httpd/conf.d/ #Enter the configuration directory [root@mail conf.d]# vim virtualhost.conf #Create a configuration file, edit the content as follows [root@mail conf.d]# cat virtualhost.conf #View and check the configuration file <VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200" ServerName www.test200.com </VirtualHost> <VirtualHost 192.168.137.201:80> DocumentRoot "/var/www/test201" ServerName www.test201.com </VirtualHost>
[root@mail conf.d]# cd /var/www #Switch directory [root@mail www]# mkdir test200 test201 #Create directory [root@mail www]# echo test200 >>./test200/index.html #Create a homepage with IP 200 [root@mail www]# echo test201 >>./test201/index.html #Create a homepage with IP 200
2.2.3 Testing
[root@localhost www]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
We use elinks for testing, of course, use Browser testing is the same [root@localhost conf]# elinks -source 192.168.137.200 test200 [root@localhost conf]# elinks -source 192.168.137.201 test201
2.3 Port-based virtual host configuration
2.3.1 Add a listening port to the main configuration file
[root@localhost conf]# vim /etc/httpd/conf/httpd.conf On the basis of the original line Listen 80, add a line Listen 8080
2.3.2 Add 8080 port virtual configuration
[root@localhost conf.d]# cat virtualhost.conf <VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200" ServerName www.test200.com </VirtualHost> <VirtualHost 192.168.137.201:80> DocumentRoot "/var/www/test201" ServerName www.test201.com </VirtualHost> #The following content is added based on the above configuration. <VirtualHost 192.168.137.201:8080> DocumentRoot "/var/www/test201-8080" ServerName www.test201-8080.com </VirtualHost>
[root@localhost conf.d]# cd /var/www/ #Switch directory [root@localhost www]# mkdir test201-8080 #Create directory [root@localhost www]# echo "test201-8080" >>./test201-8080/index.html #Create homepage
2.3.2 Testing
[root@localhost www]# service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName [ OK ] [root@localhost conf]# elinks -source 192.168.137.201:80 test201 [root@localhost conf]# elinks -source 192.168.137.201 test201 [root@localhost conf]# elinks -source 192.168.137.201:8080 test201-8080
2.4 Domain-based virtual host configuration
2.4.1 Add the virtual host configuration of the domain name
[root@localhost conf.d]# vim virtualhost.conf #Edit virtual host configuration file [root@localhost conf.d]# cat virtualhost.conf #The content is as follows, the red part is added on the basis of the above NameVirtualHost 192.168.137.200:80 <VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200" ServerName www.test200.com </VirtualHost> <VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200net" ServerName www.test200.net </VirtualHost> <VirtualHost 192.168.137.201:80> DocumentRoot "/var/www/test201" ServerName www.test201.com </VirtualHost> <VirtualHost 192.168.137.201:8080> DocumentRoot "/var/www/test2018080" ServerName www.test2018080.com </VirtualHost>
[root@localhost conf.d]# !ser service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName [ OK ]
[root@localhost conf.d]# cd /var/www #Switch directory [root@localhost www]# mkdir test200net #Create directory [root@localhost www]# echo "test200net" >>./test200net/index.html #Create homepage
2.4.2 Testing
2.4.2.1 Add domain name resolution
Here we do not provide dns to parse, simply use the hosts file area to parse.
[root@localhost www]# vim /etc/hosts Edit the hosts file and add two lines [root@localhost www]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.137.200 www.test200.com 192.168.137.200 www.test200.net
You can test it next
[root@localhost www]# elinks -source http://www.test200.com #test.com domain test200 [root@localhost www]# elinks -source http://www.test200.net #test.net domain test200net