Virtual Web host
Run multiple Web sites on the same server, each of which is not independent
Take up a real computer
Types of virtual hosts supported by httpd
1. Domain name based virtual host
2. Virtual host based on IP address
3. Port based virtual host
Demonstration of examples
Domain name based virtual host
Step 1: environment deployment
1. Install DNS package bind and httpd services to test
[root@localhost ~]# yum install bind httpd -y ...
2. Provide domain name resolution for virtual host
[root@localhost ~]# vim /etc/named.conf / / enter the main configuration file options { listen-on port 53 { any; }; //Replace the original 127.0.0.1 with any ... allow-query { any; }; //Replace localhost with any [root@localhost ~]# vim /etc/named.rfc1912.zones / / enter the zone configuration file zone "accp.com" IN { //First forward domain name type master; file "accp.com.zone"; //Point to the area data configuration file accp.com.zone allow-update { none; }; }; zone "kgc.com" IN { //Second forward domain name type master; file "kgc.com.zone"; //Point to the zone data configuration file kgc.com.zone allow-update { none; }; }; [root@localhost ~]# cd /var/named / / / enter the / var/named / directory [root@localhost named]# cp -p named.localhost accp.com.zone / / copy the template file named.localhost as the area data configuration file of accp.com.zone. [root@localhost named]# vim accp.com.zone / / edit the zone data configuration file $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.235.129 //Add resolution A record and point to the IP address of this host [root@localhost named]# cp -p accp.com.zone kgc.com.zone / / copy the zone data configuration file whose accp.com.zone is kgc.com.zone, without modification. [root@localhost named]# systemctl start named / / start the domain name resolution service [root@localhost named]# systemctl stop firewalld.service / / turn off the firewall [root@localhost named]# setenforce 0 / / turn off enhanced security
Step 2: use the client to test the DNS Service
1. Modify the DNS server address of the client to the address of the virtual host.
2. Open the cmd command prompt of the client and use the nslookup command plus the domain name to obtain the resolved address.
Step 3: set related configuration files of virtual host
1. Configure the configuration file of the virtual host
[root@localhost named]# cd /etc/httpd/conf / / enter the conf directory of the configuration file [root@localhost conf]# mkdir extra / / create an extension directory [root@localhost conf]# cd extra / / / enter the directory [root@localhost extra]# vim vhost.conf / / edit the virtual host configuration file. You can define it yourself. <VirtualHost *:80> //Label * all ip 80 ports DocumentRoot "/var/www/html/accp/" //Site directory ServerName www.accp.com //Site domain name ErrorLog "logs/www. accp.com.error_log" //Site error log file, logs is the soft link (relative path) of / var/log/httpd CustomLog "logs/www.accp.com.access_1og" common //Site access log <Directory "/var/www/html/"> //Control directory permissions Require all granted //Allow all access </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/ www/ html/kgc/" ServerName www. kgc. com ErrorLog "logs/www.kgc.com.error_1og" CustomLog "logs/www.kgc.com.access_log" common <Directory "/var/www/html/"> Require all granted </Directory> </VirtualHost>
2. Configure web page
[root@localhost extra]# cd /var/www/html / / / enter the site directory [root@localhost html]# mkdir accp kgc [root@localhost html]# cd accp/ [root@localhost accp]# vim index.html <h1>this is accp web</h1> [root@localhost accp]# cd ../kgc/ [root@localhost kgc]# vim index.html <h1>this is kgc web</h1>
3. Enter httpd.conf configuration file and declare that it contains the virtual host sub configuration file created previously
[root@localhost kgc]# cd /etc/httpd/conf [root@localhost conf]# vim httpd.conf ... Include conf/extra/vhost.conf //Access this entry at the last line to declare that the virtual host sub configuration file is included [root@localhost extra]# systemctl start httpd / / start httpd service
Step 4: use the client to access the web page
II. Port based virtual host
1. Edit the virtual host configuration file
[root@localhost extra]# vim vhost.conf ... //Omit part of the previously set configuration <VirtualHost *:8080> //Add port 8080 of accp domain name DocumentRoot "/var/www/html/accp02" ServerName www.accp.com ErrorLog "logs/www.accp02.com.error_log" CustomLog "logs/www.accp02.com.access_log" common <Directory "/var/www/html/"> Require all granted </Directory> </VirtualHost>
2. Create a new 8080 port web site
[root@localhost extra]# cd /var/www/html/ [root@localhost html]# mkdir accp02 [root@localhost html]# cd accp02/ [root@localhost accp02]# vim index.html <h1>this is accp02 test web</h1>
3. Modify the listening address of httpd.conf configuration file
vim /etc/httpd/conf/httpd.conf //Enter profile ... Listen 192.168.235.129:80 Listen 192.168.235.129:8080 //Find the Listen entry and add the 8080 port listener [root@localhost accp02]# systemctl restart httpd / / restart the service
4. Use client web page
III. IP based virtual host
1. First add a second network card, and then it will automatically get an IP address.
2. Configure the configuration file of the virtual host
[root@localhost accp02]# cd /etc/httpd/conf/extra/ [root@localhost extra]# ls vhost.conf [root@localhost extra]# vim vhost.conf 1 <VirtualHost 192.168.235.129:80> 2 DocumentRoot "/var/www/html/accp/" 3 ServerName www.accp.com 4 ErrorLog "logs/www.accp.com.error_log" 5 CustomLog "logs/www.accp.com.access_1og" common 6 <Directory "/var/www/html/"> 7 Require all granted 8 </Directory> 9 </VirtualHost> 10 11 <VirtualHost 192.168.235.142:80> 12 DocumentRoot "/var/www/html/accp02" 13 ServerName www.accp.com 14 ErrorLog "logs/www.accp02.com.error_log" 15 CustomLog "logs/www.accp02.com.access_log" common 16 <Directory "/var/www/html/"> 17 Require all granted 18 </Directory> 19 </VirtualHost>
3. Modify the listening address of httpd.conf configuration file
[root@localhost extra]# vim /etc/httpd/conf/httpd.conf ... Listen 192.168.235.129:80 Listen 192.168.235.142:80 //Add listening of another IP [root@localhost extra]# systemctl restart httpd / / restart the service
4. Use client web page