Apache Default Virtual Host

Source: Internet
Author: User


As mentioned in the previous section, you can access the two files of 1.php and index.html below/usr/local/apache2.4/htdocs/, which is why?
Enter command vim/usr/local/apache2.4/conf/httpd.conf, search/htdocs, enter, see,

The contents of the red box are displayed because the parameter documentroot defines where the site's directory is. Then search/servername, enter, see,

The contents of the red box are displayed, because the parameter ServerName defines the domain name of the website, but this ServerName can be accessed with any domain name.
Windows and Linux Htdocs are basically similar, so here's a demonstration, how to write Htdocs,hosts file path on Windows, see

Right-click the Hosts file, select Open With, then select Notepad, click OK, go to see,

Add a line of IP address and domain name, see red Box,

and then save. Save this side there will be some problems, need administrator privileges to save, this way can try, copy a file to the desktop, and then make the above changes, and then delete the original file, the desktop changes on the file in.
When you're done, open windows cmd and try to ping the two domain names, see,

As you can see, IP is 192.168.218.128, that's right. Under normal circumstances, do not change the Hosts file, see this is the case,

As you can see, the IP address becomes the IP of the official website obviously.
The above is the role of the hosts this file, you can access to the IP point. This condition can be used when DNS is not yet in effect.
In the www.abc.com domain name point to 192.168.218.128 this IP, to visit this site, see,

is also "It works!", then visit, www.abc.com/1.php, enter, see,

Including www.123.com/1.php, is the same, see,

The above operation of the two domain name and IP address, we do not go to the configuration file definition, just define the ServerName www.example.com:80 This domain name, why can be accessed?

This is called the Apache default host, that is, any domain name, as long as the point to the IP, will be access to this site, this site is called the default virtual host. If nothing is done, just use the ServerName www.example.com:80 and DocumentRoot defined in the httpd.conf configuration file,

This will not be very good management, after all, a server can run multiple domain names, not necessarily pointing to this side. So, we need to open a virtual host configuration file, enter command vim/usr/local/apache2.4/conf/httpd.conf, search/extra, enter, find,

In the red box is the virtual host, the following line to remove the #, save and exit. In other words, the front htdocs, DocumentRoot, and ServerName are all not necessary, because this configuration file can only define a ServerName, it is certainly not appropriate.
Now open a level two configuration file, is the configuration file of the virtual host, in this configuration file can be defined multiple ServerName, input command vim/usr/local/apache2.4/conf/extra/ Httpd-vhosts.conf, enter, turn down, see,

The two-box content is based on <virtualhost:80> begins with the <virtualhost:80> end, indicating a host, that is, a Web site. Here need to make some changes, ServerAdmin used to define the administrator's mailbox, do not matter, you can delete. DocumentRoot defines where the site and directory are, and can be redefined. ServerName defines the domain name of the website, only one domain name can be written. Serveralias defines the alias of a Web site and can write multiple domain names. ERRORLOG Specifies the error log, Customlog specifies the access log. This is the definition of a Web site, specifically see,

Save and exit, the virtual host defined here will expire once the www.example.com defined in httpd.conf has been in effect.

Then refine the settings just now, see,

Now you need to create a file in the corresponding site root directory, enter the command vim/data/wwwroot/abc.com/index.php, return, see,

Enter the contents, save and exit. Enter command vim/data/wwwroot/111.com/index.php, enter, see,

Save and exit. Then check the syntax of the configuration file and load the configuration file.

The above preparation is complete, then test the virtual host.
First, if the domain name is not bound to the htdocs, Ping will be connected to the external network, and not in the local network, see,

So, to visit www.abc.com this site on this machine, you can edit the vi/etc/hosts, specify a bit. There's another way, this way. Introduce a new command curl,-x option, enter command curl-x192.168.218.128:80 abc.com, carriage return, see,

The effect is very obvious, no matter what domain name points to come, access is abc.com, is the virtual host configuration inside the first virtual host, is the default virtual host. The so-called default virtual host, is to access what domain name, as long as the resolution to this IP, will visit this site, access to the site's corresponding virtual host configuration, that is abc.com. Then visit example.com, see,

Here 111.com is the second virtual host domain name, access to the www.111.com, not 111.com domain name, so access to the first virtual host up. As long as the search domain is not on the configuration we set, it will be matched to the first virtual host. The first one is called the default virtual host configuration file.
This way we can also find that as long as the VirtualHost configuration file on this side of the virtual host, the httpd.conf configuration file before the ServerName and Docmentroot will be invalidated.
So many of the above are two conclusions:
(1), in the main profile httpd.conf, opened the VirtualHost virtual host configuration file, this time the virtual host configuration file is effective, and the master configuration file defined in the ServerName and Docmentroot will be invalidated.
(2), virtual host configuration file, once opened, inside can define a lot of VirtualHost virtual host configuration file, each VirtualHost inside have corresponding ServerName and docmentroot, each VirtualHost Represents a site, which is a virtual host. In the virtual host there is a special case, there is a default virtual host, regardless of any domain name, as long as not on the other virtual host, will be resolved to this IP, will be access to this default virtual host.

Apache Default Virtual Host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.