Hello everyone, I am Internet old xin, this is my 21st day to participate in more text challenge, the activity details: more text challenge

@[toc]

preface

In the previous article, we learned the basic use of Apache, Apache configuration file, in our previous study, has been using IP as the way to visit the website, but in fact, we visit Baidu, visit other websites are the domain name, not IP.

Today we’re going to talk about web hosting.

What is a virtual host

The definition of Baidu Encyclopedia is as follows:

Multiple virtual hosts can be configured on an Apache server to achieve a server to provide multiple site services, which is actually to access different directories on the same server. A server host can run multiple websites, each of which is a virtual host.

We all know that everything in Linux is a directory, so when we visit a website, what exactly are we visiting? For example, we set up the DZ forum, access IP or domain name, apache configuration file specified path

As you can see in the figure above, when we visit bbs.zmkjedu.com, we actually visit the path in the yellow box, which is index.php by default

When you type in the url, it also defaults to index.php

So here we see that when we visit a website, we are actually accessing a file in a directory.

So what does virtual host mean?

Simply put, a virtual host is a directory of different access to the files.

As shown in the picture, when we visit www.zmkjedu.com and bbs.zmkjedu.com, the path we visit is different, so the files we visit are different.

The image above is also in the Apache configuration file, but in a different path to the secondary domain name zh.zmkjedu.com

So virtual host in addition to based on domain name, what kind of way

Two. Type of virtual host

There are three ways to implement Apache virtual host.

  • Ip-based virtual hosting
  • Port-based virtual host
  • Domain-based virtual hosting

The meaning of the VirtualHost parameter

<VirtualHost *:80>			Server IP address and port number
    DocumentRoot "/var/www/html/"	 # site directory
    ServerName www.cq.com	# the domain name
    ServerAlias 			# add multiple domain names to the virtual host, the above url alias

  <Directory "/var/www/html">	# Restrictions on root behavior
      Options FollowSymLinks ExecCGI	# followSymLinks indicates that symbolic links are allowed and disabled by default
      AllowOverride None 	 # indicates that reloading directory configuration files (.htaccess) is prohibited. This function is not recommended for common sites
      Order allow,deny		For this configuration, it works for directories other than the default folder for DocumentRoot in Apachede. Take example 1 below.)
      Allow from all
      #Deny from all
      Require all granted
  </Directory>
  
</VirtualHost>
Copy the code

Note that each time you write this configuration, you don’t need to write all of it, as in my example above, just three lines are fine.

Four. Virtual host actual combat

1. Ip-based virtual host

Hang different websites on different IP addresses, visit different IP addresses, and see different websites. Generally, servers do not have so many public IP addresses, and people generally use domain names to access, so in this case, we will use them for experiments, Intranet tests, but the company generally does not use them

A. Original IP address of the VM
[root@gaosh-1 ~]# ifconfig |grep inetInet addr:192.168.1.22 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: Fe80: : 20 c: 29 ff: fed9: e81/64 Scope: Link inet addr: 127.0.0.1 Mask: 255.0.0.0 inet6 addr: : : 1/128 Scope: Host/root @ gaosh - 1 -# 
Copy the code
B. Add temporary IP addresses
[root@gaosh-1 ~]# ifconfig eth0:0 192.168.1.23
[root@gaosh-1 ~]# ifconfig |grep inet Inet addr:192.168.1.22 Bcast:192.168.1.255 Mask:255.255.255.0 INET addr:192.168.1.23 Bcast:192.168.1.255 Mask: 255.255.255.0Copy the code
C. Add virtual hosts to the configuration file

[root@gaosh-1 ~]# vim /etc/httpd/conf/httpd.conf

Easy copy plate

<VirtualHost 192.168.1.23:80> DocumentRoot /var/www/ HTML /zmgaosh22 </VirtualHost> <VirtualHost 192.168.1.23:80> DocumentRoot /var/www/html/zmgaosh23 </VirtualHost>Copy the code

[root@gaosh-1 ~]# service httpd restart

D. Create a test file
[root@gaosh-1 ~]# mkdir -p /var/www/html/zmgaosh22
[root@gaosh-1 ~]# mkdir -p /var/www/html/zmgaosh23

[root@gaosh-1 ~]# echo '192.168.1.22 I am cold 22 people at Goldman sachs' > / var/WWW/HTML/zmgaosh22 / index. The HTML
[root@gaosh-1 ~]# echo '192.168.1.23 I am cold 23 people at Goldman sachs' > / var/WWW/HTML/zmgaosh23 / index. The HTML
[root@gaosh-1 ~]# 
Copy the code
Test: [root@gaosh-1 ~]# elinks - source 192.168.1.22192.168.1.22 MY name is Gao Shenghan 22 [root@gaosh-1 ~]# elinks - source 192.168.1.23192.168.1.23 I am Gao Sheng Han 23 [root@gaosh-1 ~]# 
Copy the code
2. Port-based virtual hosts
A. Modify the configuration file
[root@gaosh-1 ~]# vim /etc/httpd/conf/httpd.confListen 80 Listen 81 <VirtualHost 192.168.1.22:80> DocumentRoot /var/www/ HTML /zmgaosh22 </VirtualHost> <VirtualHost 192.168.1.23:81 > DocumentRoot/var/WWW/HTML/zmgaosh23 < VirtualHost >Copy the code
B. test
[root@gaosh-1 ~]# service httpd restart
[root@gaosh-1 ~]# elinks - source 192.168.1.22:80192.168.1.22 MY name is Gao Shenghan 22 [root@gaosh-1 ~]# elinks - source 192.168.1.23:81192.168.1.23 I am Gao Sheng Han 23 [root@gaosh-1 ~]# 
Copy the code
3. Domain-based virtual hosts
A. Modify the configuration file
<VirtualHost 192.168.1.22:80> DocumentRoot /var/www/ HTML /zmgaosh22 ServerName www.zmgaosh22.com </VirtualHost> <VirtualHost 192.168.1.23:80> DocumentRoot /var/www/ HTML /zmgaosh23 ServerName www.zmgaosh23.comCopy the code
B. Modify hosts resolution
127.0.0.1 localhost localhost.localdomain localhost4 localhost4. Localdomain4 ::1 localhost localhost.localdomain Localhost6 localhost6. localDomain6 192.168.1.22 www.zmgaosh22.com 192.168.1.23 www.zmgaosh23.comCopy the code
C. test
[root@gaosh-1 ~]# service httpd restart


[root@gaosh-1 ~]# elinks -source www.zmgaosh22.com192.168.1.22 MY name is Gao Shenghan 22 [root@gaosh-1 ~]# elinks -source www.zmgaosh23.com192.168.1.23 I am Gao Sheng Han 23 [root@gaosh-1 ~]# 
Copy the code

conclusion

Virtual host configuration based on domain name and port based use more, need to master.