Virtual Host settings in Apache

Source: Internet
Author: User
For the WWW server configured with Apache HTTP server, you only need to configure the configuration file httpd. conf and httpd-vhosts.conf to modify, you can achieve the virtual host service, that is, provide the services of multiple hosts on the same machine, the following describes in detail its basic principle, the specific setting method. Of course, to master these skills, you need to carefully compare the notes in the configuration file to experiment slowly.
I. Principles of WWW Server Virtual Host
A www server VM is a WWW server that uses a physical machine to act as multiple host names. For example, by a machine at the same time to provide http://www.company1.com, http://www.company2.com and other WWW services, and browse these WWW site users do not feel this way with different machines to provide different services.
The advantage of using WWW Virtual Hosts is that some small-scale websites share the same physical machine with other websites, which can reduce system operation costs and reduce management difficulty. In addition, individual users can also use this virtual hosting method to create www servers with their own independent domain names. Currently, many domestic companies provide this free service.
WWW Virtual Hosts can work in two ways:
1.1 IP address-based VM Mode
In this way, different host names are resolved to different IP addresses, and these IP addresses are set on the machines that provide the VM service. The server determines the Virtual Host Service requested by the user based on the target IP address of the user request, so as to further process the service.
Disadvantages: the IP address-based virtual host method requires that multiple IP addresses be set up on the machine that provides the virtual host service, which wastes IP addresses and limits the number of virtual hosts that a machine can accommodate. Therefore, this method is rarely used. However, this is the only way to use HTTP 1.0 to support virtual hosts.
1.2 host name-based VM
Because the IP address-based VM has the following disadvantages, the support for host name-based VM is added to HTTP 1.1. Specifically, when the customer Program When sending a request to the WWW server, the host name that the client wants to access is also passed to the WWW server through the "host:" statement in the request header. For example, www.company1.com and www.company2.com all correspond to the same IP address (that is, the same machine provides services for the two virtual domain names). When the customer program needs to access http://www.company1.com/index.html, the request header contains the following content:
GET/index.html HTTP/1.1
HOST: www.company1.com
.....
After receiving the request, the WWW server program can check the "host:" statement to determine the service of the virtual host requested by the client program, and then further process the request.
Advantage: as long as one IP address is set on the machine that provides the VM service, theoretically it can provide services to countless virtual domain names, which consumes less resources and facilitates management. This method is basically used to provide the VM service.
Disadvantage: it cannot be used in earlier versions of HTTP 1.0. In fact, the current browser basically supports the host name-based virtual host mode.
Ii. Virtual Host setting technology under Apache server
2.1 Apache WWW server Overview
Apache WWW server is currently the most widely used WWW server software on the Internet. With flexible configuration, it can accomplish almost what you want. This article describes how to set up various virtual hosts in Apache.
2.2 IP address-based VM settings for Apache WWW Server
To use this virtual host method, you must first set an IP address for each virtual host on the server. These IP addresses can be completed by adding multiple NICs or setting up multiple IP addresses on one Nic. With multiple IP addresses, you can set Apache in either of the following ways:
2.2.1 run an Apache copy for each VM
In this way, each Apache program can be run by a single user, so each virtual host does not affect each other. When setting such a VM, you only need to set a configuration file for each Apache copy. The only thing you need to note is that you must use the "listen" statement, each copy of Apache is forced to receive service requests only on its own IP address.
Advantage: each virtual host does not interfere with each other, and the security is high.
Disadvantage: it occupies a large amount of system resources.
2.2.2 multiple virtual hosts share the same Apache
In this way, each virtual host shares the same Apache, so there is a certain impact between each virtual host, especially when executing CGI programs, it may bring some serious security problems. When setting such a VM, you only need to set the following information for each VM:
<Virtualhost www.company1.com>
DocumentRoot/home/company1
...
</Virrualhost>
Advantage: less system resources are occupied than the previous method.
Disadvantage: low security. Each virtual host still needs one IP address.
2.3 simple domain name-based VM settings on the Apache WWW Server
In this way, each virtual host shares the same Apache. Therefore, when a CGI program is running, the security is not high. When setting such a VM, you only need to set the following information for each VM:
Namevirtualhost 111.222.33.44; receives services from the VM at this IP address.
<Virtualhost 111.222.33.44>
Servername www.company1.com
DocumentRoot/www/company1
</Virtualhost>
<Virtualhost 111.222.33.44>
Servername www.company2.com
DocumentRoot/www/company2
</Virtualhost>
Advantage: A single IP Address can provide a large number of VM services.
Disadvantage: poor security. To maintain these virtual hosts, you must change the configuration file and restart the Apache process. Therefore, it is not suitable for large-scale VM services.
2.4 simple port-based VM settings on the Apache WWW Server
In 2.3, the virtual host based on the host IP address is actually resolved to port 80 of the Host IP address. To use each port of the host, we can also set different virtual hosts on different ports of the host. For example Code A virtual host based on port 8888 and port 8989 is created on the local machine.
Listen 8989.
Listen 8888.
Namevirtualhost FIG: 8989
Namevirtualhost FIG: 8888
<Virtualhost *: 8989>
Serveradmin webmaster@dummy-host2.example.com
DocumentRoot "E:/XAMPP/htdocs/Zen-cart"
Servername 127.0.0.1: 8989
#??? Errorlog @ rel_logfiledir @/8989-error_log
#??? Customlog @ rel_logfiledir @/8989_log common
</Virtualhost>
<Virtualhost *: 8888>
Serveradmin webmaster@dummy-host2.example.com
DocumentRoot "E:/XAMPP/htdocs/WordPress"
Servername 127.0.0.1: 8888
#??? Errorlog @ rel_logfiledir @/8888-error_log
#??? Customlog [...]

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.