apache service to build a virtual web host

One, the type of virtual web host

1. Introduction to Virtual Web Hosting

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 website construction and operating costs.

The use of 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 at the same time.

2. Three types of virtual web host supported by apache service

2.1, based on the type of domain name

Based on domain name: Use a different domain name for each virtual host, but the corresponding IP address is the same. This is the most commonly used type of virtual web host .

2.2, based on the type of IP address

Based on IP address: Use different domain names for each virtual host, and their corresponding IP addresses are also different. This method requires multiple network interfaces for the server, so the application is not very extensive.

2.3. Port-based type

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

Note : Because different types of virtual hosts have different distinguishing mechanisms, it is recommended not to use them at the same time to avoid confusion.

Second, build a domain-based virtual web host

1. Provide domain name resolution

First, you need to register the domain name of each virtual Web site with the DNS service provider, so that when you visit any one of the virtual Web sites, you will eventually visit the same IP address—the IP address of the server that actually supports all virtual Web sites.

Suppose we have two domain names www.wlm.com and www.wat.com. The corresponding IP address is 20.0.0.58. We can use two domain names as two virtual web sites served by apache and correspond to one IP address 20.0.0.58 at the same time.

2. Prepare web documents

Prepare website directories and web documents for each virtual web host. Provide a home page file containing different content for each virtual web host.

[root@localhost ~]# mkdir -p /var/www/html/wlmcom
[root@localhost ~]# mkdir -p /var/www/html/watcom
[root@localhost opt]# echo "<h1>www.wlm  web1</h1>" > /var/www/html/wlm/index.html
[root@localhost opt]# echo "<h1>www.wat  web2</h1>" > /var/www/html/wat/index.html
3. Change the configuration file

In the main configuration file of the Apache service, if you want to enable a domain-based virtual web host, you usually need to change the following two areas

  • Virtual host area configuration: Use <VirtualHost listening address>...area configuration to establish independent configuration content for each virtual web host. It should include at least the configuration items of the website name of the virtual host and the root directory of the webpage; other configuration items (such as managing mailboxes, access logs, etc.) can be added according to actual needs.

  • Permission configuration for accessing the root directory of the webpage: Use <Directory directory location>... regional configuration to set access permissions for the website directory of each virtual web host, such as allowing anyone to access. Directory access can inherit the authorization permissions of its parent directory, so you can directly authorize access permissions for the parent folder to simplify configuration. When the number of virtual web hosts is large, it is recommended to use a separate virtual host configuration file.

[root@localhost opt]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
.......                  //省略部分信息
  //可在此位置手动创建独立的访问目录权限                            
<VirtualHost *:80>           //设置域名 www.wlm.com 虚拟站点区域
    DocumentRoot "/var/www/html/wlm"    //服务访问域名网页的路径
    ServerName www.wlm.com      //服务访问的域名
    ErrorLog "logs/www.wlm.com.error_log"      //错误访问日志存放路径
    CustomLog "logs/www.wel.com.access_log" common      //访问日志存放路径
        <Directory "/var/www/html">        //设置目录访问权限
        Require all granted              //允许所有访问
        </Directory>
</VirtualHost>

<VirtualHost *:80>    //设置域名 www.wlm.com 虚拟站点区域。
    DocumentRoot "/var/www/html/wat"    //服务访问域名网页的路径
    ServerName www.wat.com    //服务访问的域名
    ErrorLog "logs/www.wat.com.error_log"     //错误访问日志存放路径
    CustomLog "logs/www.wat.com.access_log" common   //访问日志存放路径
        <Directory "/var/www/html">     //设置目录访问权限
        Require all granted   //允许所有访问
        </Directory>
</VirtualHost>

Then load these configurations by enabling "Include conf/extra/httpd-vhosts.conf" in the httpd.conf file. This can minimize the changes to the httpd.conf file and make it easier to maintain the configuration content.

[root@localhost opt]# vim /usr/local/httpd/conf/httpd.conf 
.......                  //省略部分信息
Include conf/extra/httpd-vhosts.conf //加载独立的配置文件,将行首的 "#" 删除
.......                  //省略部分信息
[root@localhost opt]# systemctl restart httpd   //重启httpd服务
4. The client accesses the virtual web host

First, add the mapping of the dns domain name in the client's hosts file, and then in the client's browser, use the website name to visit different domain names respectively, and you can see the content of the previously set webpage, which means virtual domain name-based The host configuration is successful. If you cannot see this result, you need to check the homepage files of the two sites, follow the above process to troubleshoot configuration errors, and if necessary, clear the browser cache and visit again.

Linux的hosts文件路径为:/etc/hosts  
20.0.0.58    www.wlm.com      //添加内容
20.0.0.58    www.wat.com
window的hosts文件路径为:C:\Windows\System32\drivers\etc\hosts
20.0.0.58    www.wlm.com         //添加内容
20.0.0.58    www.wat.com

The test result shows:
the test page whose domain name is www.wlm.com is the test page
Insert picture description here
of www.wat.com
Insert picture description here

Third, build a virtual web host based on IP address

The process of constructing an IP address or port-based virtual host is similar to that of a domain-based virtual host. You also need to provide domain name resolution, prepare web documents, adjust the httpd configuration, restart the httpd service, and then access the virtual host in the client for testing. . The main difference is that different types of virtual hosts have slightly different configurations in the httpd.conf file.

For virtual hosts based on IP addresses, each virtual web host uses a different IP address, but all provide web browsing services through the same httpd server. Because of this, the servers used to support these virtual web hosts also need a large number of network interfaces, which are often inconvenient in practical applications. Therefore, IP address-based virtual hosts are not as widely used as domain-based virtual hosts.

Change the httpd.conf file configuration

[root@localhost opt]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
.......                  //省略部分信息
  //可在此位置手动创建独立的访问目录权限                            
<VirtualHost 20.0.0.58>           //基于20.0.0.58IP的虚拟web主机配置
    DocumentRoot "/var/www/html/wlm"    //服务访问域名网页的路径
    ServerName www.wlm.com      //服务访问的域名
    ErrorLog "logs/www.wlm.com.error_log"      //错误访问日志存放路径
    CustomLog "logs/www.wel.com.access_log" common      //访问日志存放路径
        <Directory "/var/www/html">        //设置目录访问权限
        Require all granted              //允许所有访问
        </Directory>
</VirtualHost>

<VirtualHost 20.0.0.55>    //基于20.0.0.55的虚拟web主机的配置
    DocumentRoot "/var/www/html/wat"    //服务访问域名网页的路径
    ServerName www.wat.com    //服务访问的域名
    ErrorLog "logs/www.wat.com.error_log"     //错误访问日志存放路径
    CustomLog "logs/www.wat.com.access_log" common   //访问日志存放路径
        <Directory "/var/www/html">     //设置目录访问权限
        Require all granted   //允许所有访问
        </Directory>
</VirtualHost>
[root@localhost opt]# vim /usr/local/httpd/conf/httpd.conf 
.......                  //省略部分信息
Include conf/extra/httpd-vhosts.conf //加载独立的配置文件,将行首的 "#" 删除
.......                  //省略部分信息
[root@localhost opt]# systemctl restart httpd   //重启httpd服务

Virtual web host based on IP20.0.0.58 Virtual web host
Insert picture description here
based on 20.0.0.50
Insert picture description here

Fourth, build a port-based virtual web host

Port-based virtual hosts are usually only used for the same website. The website names and IP addresses are often the same, but different TCP ports are used to provide service access to different web content. When accessing a web server with a port other than 80 in the browser, you need to clearly indicate the server's port number, such as visiting http://www.wat.com:55/ or http://20.0.0.58.55/.

[root@localhost opt]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
<VirtualHost 20.0.0.58:80>    //基于80端口的虚拟web主机配置
    DocumentRoot "/var/www/html/wlm"
    ServerName www.wlm.com
    ErrorLog "logs/www.wlm.com.error_log"
    CustomLog "logs/www.wel.com.access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>

<VirtualHost 20.0.0.58:55>   //基于55端口的web主机配置
    DocumentRoot "/var/www/html/wat"
    ServerName www.wat.com
    ErrorLog "logs/www.wat.com.error_log"
    CustomLog "logs/www.wat.com.access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>

When configuring a port-based virtual web host, you need to specify the TCP port number to be monitored through multiple Listen configuration items, and you need to change the httpd.conf configuration file.

[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf   //编辑配置文件
// 使用vim编辑器末行模式/搜索Listen 80服务器默认监听的端口,在其下一行添加配置
.......                  //省略部分信息
Listen 80
Listen 55     //添加监听55端口
.......                  //省略部分信息
Include conf/extra/httpd-vhosts.conf //加载独立的配置文件,将行首的 "#" 删除
.......                  //省略部分信息
[root@localhost opt]# systemctl restart httpd   //重启httpd服务

Visit the web virtual machine based on port 80
Insert picture description here

Access to the web virtual machine based on port 55.
Insert picture description here
Note: I personally recommend a domain-based virtual web host for the production environment

Guess you like

Origin blog.csdn.net/wulimingde/article/details/108431078