CentOS7 build a virtual Web hosting (domain-based, port, IP address)

Virtual Web Host

Run multiple Web sites on the same physical server, each of which occupies a site is not independent of a real computer.

httpd supported virtual host type

  • Based virtual hosting
  • IP address-based virtual hosts
  • Based on the virtual host port

------ name-based virtual hosting to build

(1) install bind, httpd service.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(2) into the main configuration file named service, the next two positions to FIG. "Any".

[root@localhost ~]# vim /etc/named.conf

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(3) into the named service area configuration file and add information about the area two domains.

[root@localhost ~]# vim /etc/named.rfc1912.zones

zone "aaa.com" IN {
        type master;
        file "aaa.com.zone";
        allow-update { none; };
};

zone "bbb.com" IN {
        type master;
        file "bbb.com.zone";
        allow-update { none; };
};

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(4) into the "/ var / named /" directory, recursive copy "named.localhost" area data configuration file, named "aaa.com.zone", and then modify it.

[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost  aaa.com.zone
[root@localhost named]# 

[root@localhost named]# vim aaa.com.zone 
$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.52.133

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(5) then copy "aaa.com.zone" file, named "bbb.com.zone", need not be modified. Then open the named service, turn off the firewall and enhanced security features.

[root@localhost named]# cp -p aaa.com.zone bbb.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# 
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 0
[root@localhost named]# 

(6) to open a win10 virtual machine, the IP address of their DNS server, just set the IP address of the Linux system.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(7) with a win10 host to test the DNS service can resolve, successfully resolved.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(8) into the "/ etc / httpd / conf" directory, create a "extra /" directory, then enter the "extra /" directory with vim editor, create a new profile "vhost.conf", entered in the configuration file the following content.

[root@localhost named]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf  magic
[root@localhost conf]# mkdir extra
[root@localhost conf]# ls
extra  httpd.conf  magic
[root@localhost conf]# cd extra/
[root@localhost extra]# vim vhost.conf

<VirtualHost *:80>
  DocumentRoot "/var/www/html/aaa/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot "/var/www/html/bbb/"
  ServerName www.bbb.com
  ErrorLog "logs/www.bbb.com.error_log"
  CustomLog "logs/www.bbb.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(9) into the "/ var / www / html /" directory create two "aaa /", "bbb /".

[root@localhost extra]# 
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir aaa bbb
[root@localhost html]# ls
aaa  bbb
[root@localhost html]# 

(10) into the "aaa /" directory, create a site home page document, which reads as follows:

[root@localhost html]# cd aaa
[root@localhost aaa]# ls
[root@localhost aaa]# vim index.html

<h1>this is aaa web</h1>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(11) into the "bbb /" directory, create a site home page document, which reads as follows:

[root@localhost aaa]# cd ../bbb
[root@localhost bbb]# ls
[root@localhost bbb]# vim index.html

<h1>this is bbb web</h1>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(12) into the httpd service master configuration file, is about to end in our new configuration file written into the master configuration file, and then start the httpd service.

[root@localhost bbb]# vim /etc/httpd/conf/httpd.conf 

Include conf/extra/vhost.conf

[root@localhost bbb]# systemctl start httpd
[root@localhost bbb]# 

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(13) with win10 hosts to access the two domain names, respectively, can be a successful visit.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

Port-based virtual hosts to build ------

(1) On the basis of previous experiments, into the configuration file "vhost.conf", add a "www.aaa.com" domain port 8080.

[root@localhost bbb]# vim /etc/httpd/conf/extra/vhost.conf 

<VirtualHost *:80>
  DocumentRoot "/var/www/html/aaa/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot "/var/www/html/bbb/"
  ServerName www.bbb.com
  ErrorLog "logs/www.bbb.com.error_log"
  CustomLog "logs/www.bbb.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:8080>
  DocumentRoot "/var/www/html/aaa02/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa02.com.error_log"
  CustomLog "logs/www.aaa02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(2) into the "/ var / www / html" directory, create a "aaa02" directory, enter "aaa02" directory, create a site home page document, which reads as follows:

[root@localhost bbb]# cd ../
[root@localhost html]# mkdir aaa02
[root@localhost html]# cd aaa02/
[root@localhost aaa02]# vim index.html

<h1>this is aaa02 web</h1>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(3) into the main httpd service configuration file, add a listener port, while the port will listen cancellation of IPv6. Service httpd restart.

[root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf

Listen 192.168.52.133:80
Listen 192.168.52.133:8080
#Listen 80

[root@localhost aaa02]# systemctl restart httpd
[root@localhost aaa02]# 

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(4) again to access two different ports domain name with win10 host a successful visit.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

Construction of IP-based virtual host ------

(1) adding a card to the Linux host, see the IP address.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(2) into the profile "vhost.conf", the following entries:

[root@localhost aaa02]# vim /etc/httpd/conf/extra/vhost.conf

<VirtualHost 192.168.52.133:80>
  DocumentRoot "/var/www/html/aaa/"
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost 192.168.52.139:80>
  DocumentRoot "/var/www/html/aaa02/"
  ErrorLog "logs/www.aaa02.com.error_log"
  CustomLog "logs/www.aaa02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(3) respectively of "aaa" site and home page files "aaa02" site will be modified as follows:

[root@localhost aaa02]# cd ../aaa
[root@localhost aaa]# vim index.html 

<h1>this is 133 aaa web</h1>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

[root@localhost aaa]# cd ../aaa02
[root@localhost aaa02]# vim index.html 

<h1>this is 139 aaa02 web</h1>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(4) into the httpd master profile, add annotations port. Then restart the httpd service.

[root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf 

Listen 192.168.52.133:80
Listen 192.168.52.139:80
#Listen 192.168.52.133:8080
#Listen 80

[root@localhost aaa02]# systemctl restart httpd
[root@localhost aaa02]# 

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(5) to visit the site with a win10 host two different IP addresses, a successful visit. But can only be accessed by IP address, in general, visit the Web site with a domain name, the domain name then we assign different IP addresses to access the site.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(6) is first added in the configuration file "vhost.conf", the domain name "ServerName".

[root@localhost aaa02]# vim /etc/httpd/conf/extra/vhost.conf

<VirtualHost 192.168.52.133:80>
  DocumentRoot "/var/www/html/aaa/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost 192.168.52.139:80>
  DocumentRoot "/var/www/html/aaa02/"
  ServerName www.aaa02.com
  ErrorLog "logs/www.aaa02.com.error_log"
  CustomLog "logs/www.aaa02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(7) into the named service zone configuration file, add a "aaa02" regional information.

[root@localhost aaa02]# vim /etc/named.rfc1912.zones 

zone "aaa.com" IN {
        type master;
        file "aaa.com.zone";
        allow-update { none; };
};

zone "aaa02.com" IN {
        type master;
        file "aaa02.com.zone";
        allow-update { none; };
};

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(8) into the "/ var / named /" directory, recursively copy "aaa.com.zone" file, named "aaa02.com.zone", at the same time be modified as follows:

[root@localhost aaa02]# cd /var/named/
[root@localhost named]# ls
aaa.com.zone  data     named.ca     named.localhost  slaves
bbb.com.zone  dynamic  named.empty  named.loopback
[root@localhost named]# cp -p aaa.com.zone aaa02.com.zone
[root@localhost named]# vim aaa02.com.zone 

$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.52.139

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

(9) again with win10 host, access the site through the domain name to two different IP addresses, a successful visit.

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

CentOS7 build a virtual Web hosting (domain-based, port, IP address)

Guess you like

Origin blog.51cto.com/14449541/2444696