Apache builds virtual host configuration (based on domain name, port, and IP)

mark

Introduction :

​ A virtual web host refers to running multiple web sites on the same server, each of which does not actually occupy the entire server independently, so it is called a "virtual" web host. Virtual Web hosting services can make full use of the hardware resources of the server, thereby greatly reducing the cost of website construction and operation.

Using httpd service can be very convenient to build a virtual host server, only need to run a httpd service to support a large number of Web sites.

The virtual host types supported by the httpd service include the following three:

Based on domain name : use a different domain name for each virtual host, but the corresponding IP address is the same;

Based on IP address : Use different domain names for each virtual host, and their corresponding IP addresses are also different;

Port-based : This method does not use domain names and IP addresses to distinguish different site content, but uses different TCP ports, so users need to specify the port number when browsing different virtual sites to access.

Among these types of virtual web hosts, domain-based virtual hosts are the most widely used; IP-based and port-based virtual hosts are generally only suitable for internal companies.

One: Virtual Web Hosting

  • Run multiple web sites on the same server, each of which does not occupy a real computer independently

1.1: Implementation steps of virtual host based on domain name host:

  • Provide domain name resolution for virtual hosts

  • First, you need to build a DNS service to provide domain name resolution and build a DNS service

1.2: Types of virtual hosts supported by httpd

  • Domain name-based virtual hosting

  • Virtual host based on IP address

  • Port-based virtual hosting

2: Build a virtual domain-based virtual host

Experimental steps

Add two network cards

IP is
20.0.0.41
20.0.0.50 respectively
mark

Configure network card information

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=staic
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=20.0.0.50
PREFIX=24
GATEWAY=20.0.0.2
DNS1=20.0.0.2

[root@localhost network-scripts]#systemctl start network   #重启网卡

Install Apache and DNS service

[root@localhost ~]# yum install bind httpd -y
...省略内容

Configure DNS

  • /etc/named.conf ##Main configuration file
  • /etc/named.rfc1912.zones ##Zone configuration file
  • /var/named/named.localhost ##Regional data configuration file

Configure the dns global configuration file /etc/named.conf

[root@localhost ~]# vim /etc/named.conf
 11 
 12 options {
    
    
 13         listen-on port 53 {
    
     any; };  ##进来后把监听地址改为any
 14         listen-on-v6 port 53 {
    
     ::1; };
 15         directory       "/var/named";         ##目录
 16         dump-file       "/var/named/data/cache_dump.db";
 17         statistics-file "/var/named/data/named_stats.txt";
 18         memstatistics-file "/var/named/data/named_mem_stats.txt";
 19         recursing-file  "/var/named/data/named.recursing";
 20         secroots-file   "/var/named/data/named.secroots";
 21         allow-query     {
    
     any; };    ##把localhost改为any 任何都可以访问地址

Configure zone profile

[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "abc.com" IN {
    
    
        type master;
        file "abc.com.zone";
        allow-update {
    
     none; };
};
zone "dba.com" IN {
    
    
        type master;
        file "dba.com.zone";
        allow-update {
    
     none; };
};

Configure zone data file

[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
benet.com.zone  dynamic       named.ca     named.localhost  slaves
data            kgc.com.zone  named.empty  named.loopback
[root@localhost named]# cp -p named.localhost abc.com.zone
[root@localhost named]# vim abc.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       20.0.0.41                  #添加解析地址

[root@localhost named]# cp -p abc.com.zone dba.com.zone

Restart the network card to turn off the protection function

[root@localhost named]# setenforce 0
[root@localhost named]# iptables -F
[root@localhost named]# systemctl start named

Client test works

mark

mark

Create virtual host configuration file

  • The directory file is in /etc/httpd/conf/extra/, and the simple setting file name is vhost.conf
#切换到httpd目录
[root@localhost named]# cd /etc/httpd/
[root@localhost httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run
[root@localhost httpd]# ls conf.d/
autoindex.conf  README  userdir.conf  welcome.conf
[root@localhost httpd]# ll
总用量 0
drwxr-xr-x. 2 root root  37 77 13:35 conf
drwxr-xr-x. 2 root root  82 77 13:35 conf.d
drwxr-xr-x. 2 root root 146 77 13:35 conf.modules.d
lrwxrwxrwx. 1 root root  19 77 13:35 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root  29 77 13:35 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root  10 77 13:35 run -> /run/httpd
#/var/log/httpd是日志文件目录   logs是软连接
#这里注意的是服务没启动是没有文件的
[root@localhost httpd]# systemctl start httpd
[root@localhost httpd]# ls /var/log/httpd/
access_log  access_log-20200802  error_log  error_log-20200802
[root@localhost httpd]# ls logs/
access_log  access_log-20200802  error_log  error_log-20200802

/etc/httpd/conf/extra/vhost.conf file

  • It means that all ip addresses can be accessed through port 80
  • DocumentRoot "is a web site directory"
  • ServerName "Site Service Domain Name"
  • Errorlog "Specify the error log path"
  • Customlog "Specify access log path" followed by common extension tools
  • Specify the directory name of the detailed configuration, you can find that it is the parent directory of the web site directory,
    allowing all access rights of all user hosts // This will expand more configuration attributes later
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
httpd.conf  magic
[root@localhost conf]# mkdir shuai
[root@localhost conf]# ls
httpd.conf  magic  shuai
[root@localhost shuai]# vim vhost.conf
<VirtualHost *:80>
  DocumentRoot "/var/www/html/abc"
  ServerName www.kgc.com
  ErrorLog "logs/www.abc.com.error_log"
  CustomLog "logs/www.abc.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

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

Create a site with two web pages

  • Home page file index.html is in the site directory of /var/www/html
[root@localhost shuai]# cd /var/www/html/
[root@localhost html]# ls
index.html
[root@localhost html]# mkdir abc dba
[root@localhost html]# ls
abc  index.html  dba
[root@localhost html]# cd abc/
[root@localhost abc]# vim index.html
<h1>this is abc web</h1>
[root@localhost abc]# cd ../dba/
[root@localhost dba]# vim index.html
<h1>this is dba web</h1>

Need to add the path of shuai to the main configuration file, start to read to be recognized

[root@localhost kgc]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf  magic  shuai
[root@localhost conf]# cd shuai/
[root@localhost shuai]# ls
vhost.conf
[root@localhost shuai]# vim /etc/httpd/conf/httpd.conf

Include conf/shuai/vhost.conf

Start the service, check the port

[root@localhost shuai]# systemctl start httpd
[root@localhost shuai]# netstat -natp | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      28532/httpd         

The next step for the client to verify

mark

mark

Experiment 2: Port-based construction of virtual hosts

  • Modify the configuration file of the virtual web host and add a port parameter of 8080. In order to distinguish the sites, the site files need to be modified and distinguished, and will not be overwritten
[root@localhost ~]# vim /etc/httpd/conf/shuai/vhost.conf 

<VirtualHost *:8080>                                     #修改端口信息
  DocumentRoot "/var/www/html/dba02"
  ServerName www.dba02.com
  ErrorLog "logs/www.dba02.com.error_log"
  CustomLog "logs/www.dba02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>

Create a site

[root@localhost ~]# mkdir /var/www/html/dba02
[root@localhost ~]# vim /var/www/html/dba02/index.html

<h1>this is dba02</h1>

Configure listening port

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

mark

It is strongly recommended to modify the port information, and you must not modify it to a port between 1 and 1024, because these ports have been used by system services

Restart the service and verify that the port is open

root@localhost ~]# systemctl restart httpd      #重载httpd服务
[root@localhost ~]# netstat -ntap | grep httpd   #两个监听端口已开启
tcp        0      0 20.0.0.41:8080          0.0.0.0:*               LISTEN      31057/httpd         
tcp        0      0 20.0.0.41:80            0.0.0.0:*               LISTEN      31057/httpd         

Check the verification, the IP address is the same, the port is different

mark

Three: Construct virtual host based on IP experiment

  • Build 2 virtual Web sites
  1. www.abc.com, IP address is 20.0.0.41

  2. www.abc01.com, IP address is 20.0.0.50

Modify the configuration file of the virtual web host

When virtual web hosts with different IPs are used in the server, the IP address in the /etc/httpd/conf/extra/vhost.conf file needs to be represented to a fixed IP address, and the * wildcard symbol should not be used to avoid confusion
<VirtualHost 20.0.0.41:80>
  DocumentRoot "/var/www/html/abc"
  ServerName www.abc.com
  ErrorLog "logs/www.abc.com.error_log"
  CustomLog "logs/www.abc.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

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

Create a new virtual web site

[root@localhost html]# cat abc/index.html 
<h1>this is abc web</h1>
[root@localhost html]# cat dba/index.html 
<h1>this is dba web</h1>

Modify the main configuration file and add a listening address

Listen 20.0.0.41:80
Listen 20.0.0.50:80
#Listen 80

Restart httpd service

[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -ntap | grep httpd
tcp        0      0 20.0.0.50:80            0.0.0.0:*               LISTEN      92579/httpd         
tcp        0      0 20.0.0.41:80            0.0.0.0:*               LISTEN      92579/httpd         

Client authentication

mark

mark

4: Create a hyperlink within the site

Modify the homepage file

[root@localhost html]# vim /var/www/html/abc/index.html

<html>
<head>
 <titele>hello boy</tite>
</head>
<body>
<h1><a href="http://www.abc.com/index.html">hello world</a></h1>
</body>
</html>

Restart service

[root@localhost html]# systemctl restart httpd

Client authentication

[External link image is being transferred...(img-xnDeh0Wa-1597279409877)]

[External link pictures are being transferred...(img-laGYLdWF-1597279409877)]

4: Create a hyperlink within the site

Modify the homepage file

[root@localhost html]# vim /var/www/html/abc/index.html

<html>
<head>
 <titele>hello boy</tite>
</head>
<body>
<h1><a href="http://www.abc.com/index.html">hello world</a></h1>
</body>
</html>

Restart service

[root@localhost html]# systemctl restart httpd

mark

Guess you like

Origin blog.csdn.net/weixin_47151643/article/details/107971428