The latest version of Apache virtual host configuration and Application learning Tutorial

Source: Internet
Author: User
Tags bind mixed mkdir

To configure the Apache virtual host, we need to do this in the following steps:

1, check the Apache virtual host module

2, open the Apache virtual host function

3, httpd-vhosts.conf documents detailed

4, according to the IP configuration of the virtual host

5, according to the port configuration virtual host

6, according to the domain name configuration virtual Host

First, check the Apache virtual host module

Apache to configure the virtual host, you need to first see if Apache compiles the Vhost_alias_module module. Of course, Apache has already compiled the module by default, and we can see whether the module has been compiled by the following command, as follows:

/usr/local/apache2/bin/apachectl-m

Second, open the Apache virtual host function

To turn on the Apache Virtual Host feature, we need to modify the Apache configuration file http.conf. Open the Apache installation directory, locate the httpd.conf file, and remove the # before include conf/extra/httpd-vhosts.conf. As follows:

VI httpd.conf

The purpose of this row is to import the httpd-vhosts.conf files in the conf/extra/directory into the virtual host configuration.

All of our configuration of the virtual host is done in the httpd-vhosts.conf file below. As follows:

Apache Virtual Host configuration is mainly divided into three kinds, respectively, according to the IP address, according to the port, according to the domain name.

These three kinds of virtual host, we mainly explain the domain based virtual host. Ip-based and Port based in the production environment is less used, we have only a general explanation of its configuration.

Note that prior to this, we will also modify the httpd.conf file to remove the configuration options for directory access. Otherwise, a 403 error will be reported when accessing the virtual host. As follows:

The httpd.conf file removes the order Deny,allow and the deny from all lines and modifies the following:

Options FollowSymLinks

AllowOverride None

It's worth noting that when we enable the virtual host for Apache, all user requests are processed by the corresponding virtual host. If Apache does not find the corresponding virtual host, the request is handed to the first virtual host in the configuration file for processing.

Also, if you are configuring a domain based virtual host, we must enable the Namevirtualhost *:80 line. As follows:

Three, httpd-vhosts.conf document detailed

The contents of the httpd-vhosts.conf file are as follows:

Namevirtualhost *:80

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot "@ @ServerRoot @@/docs/dummy-host.example.com"

ServerName dummy-host.example.com

Serveralias www.dummy-host.example.com

ErrorLog "Logs/dummy-host.example.com-error_log"

Customlog "Logs/dummy-host.example.com-access_log" common

NAMEVIRTUALHOST Specifies the IP address or domain name used by the virtual host, but preferably an IP address. Namevirtualhost is a necessary instruction when using a domain based virtual host. Namevirtualhost can define multiple. All requests that meet the Namevirtualhost or label definitions are treated as virtual hosts, and the primary server will ignore them. Namevirtualhost defines a request that is not defined by the label, the server cannot find the corresponding virtual host and will not be able to handle it. So each namevirtualhost-defined parameter must have at least one match.

If you set Namevirtualhost or *:80, all requests for port 80 will be processed by the virtual host, and the request will point to a virtual host based on the domain name. If there is a request from port 80, and the requested domain name is not configured as a virtual host, it will point to the first virtual host. This will make it impossible for the primary server to receive any requests from port 80. To do this, you also configure a virtual host for the primary server.

To configure a domain based virtual host, you must use the NAMEVIRTUALHOST directive, which specifies which IP addresses and ports for the current server can accept related access requests for the virtual host, and that these IP addresses and ports must be included in the address and port that the server listens to.

If you want to use all the IP addresses on the server, you can use the directive: Namevirtualhost *.

If Apache listens on more than one port (for example, a regular 80-port and SSL-443 port), you must specify a port in the Namevirtualhost directive, such as: Namevirtualhost *:80.

We can use the VirtualHost configuration segment to add a virtual host, if there are more than one virtual host, we can add more than one of these configuration segments.

In the VirtualHost configuration segment, we can use configuration directives to set up the virtual host, except for a few special instructions, such as creating a process, where almost all instructions can appear in the VirtualHost configuration segment. If some directives are not set, the virtual host uses the same global directives as the default settings for the primary server scope. Of course, the VirtualHost configuration segment should contain at least two configuration directives, ServerName and DocumentRoot, to specify the domain name of the virtual host and the path to the site document directory. As follows:

DocumentRoot "/www/a.ilanni.com"

ServerName a.ilanni.com

ServerAdmin Administrator mailbox.

DocumentRoot Site Directory (note: If the path in the site directory has spaces, enclose the path at both ends with double quotes).

ServerName The domain name to bind (required).

Serveralias the alias of the virtual host to bind to. (Optionally, if more than one domain name, the middle is separated by a space, if not, remove the line). Support *,? Two wildcard characters, such as *.abc.com, indicate that any one abc.com level two domain name is accessible.

Customlog the user log file (optionally, if not required, remove the row).

ErrorLog error log (optional, if not required, remove the line).

Four, according to the IP configuration virtual host

Apache based on IP virtual host in the actual production environment is less used, mainly in the case of single card with multiple IP.

To test this feature, we need to add an IP to the server's NIC. First look at the IP address of the server, as follows:

Ifconfig eth0|grep "inet addr" |awk ' {print $} ' |cut-d:-f2

Ifconfig eth2|grep "inet addr" |awk ' {print $} ' |sed ' s/addr://g '

You can see that the current server IP is 192.168.1.213, and then add an IP address of 192.168.1.215. Eth0 network card to increase the IP address, can be implemented through the NIC configuration file. As follows:

cp/etc/sysconfig/network-scripts/ifcfg-eth0/etc/sysconfig/network-scripts/ifcfg-eth0:0

cat/etc/sysconfig/network-scripts/ifcfg-eth0:0

/etc/init.d/network restart

Ping 192.168.1.215

After the IP address has been added, we will now modify the configuration file httpd-vhosts.conf for the virtual host. As follows:

Cat Httpd-vhosts.conf|grep-v ^$|grep-v ^#

After the httpd-vhosts.conf has been modified, we need to check that the virtual host is configured correctly, using the-s command. As follows:

/etc/init.d/httpd–s

You can see from the image above that the virtual host is configured correctly.

Create a home page file for the virtual host 192.168.1.215 as follows:

mkdir/www/192.168.1.215

echo "This is Web-server 192.168.1.215" >/www/192.168.1.215/index.html

Cat/www/192.168.1.215/index.html

Access the 192.168.1.215 virtual host. As follows:

or through the ELinks test, as follows:

Elinks-dump http://192.168.1.215

You can see the virtual host 192.168.1.215, you can already access the normal.

Now visit 192.168.1.213 to see the actual effect. As follows:

Five, configure the virtual host according to the port

The Apache default is to listen for port 80, which can be viewed through the Apache configuration file httpd.conf. As follows:

Cat/usr/local/apache2/conf/httpd.conf|grep 80

Apache Port based virtual host in the actual production environment is also rare, generally for the company's internal staff to provide access, such as the background of the page, CMS release, phpMyAdmin and so on.

Now we start to configure the port based virtual host, before we need to modify two files one is Apache configuration file httpd.conf, the second is Apache virtual host configuration file httpd-vhosts.conf.

Now we use the port 8088 to set up the virtual host. Modify Httpd.conf. As follows:

Vim/usr/local/apache2/conf/httpd.conf

After this file has been modified, we will revise the httpd-vhosts.conf. As follows:

Cat Httpd-vhosts.conf|grep-v ^$|grep-v ^#

Create a home page file for the virtual host 192.168.1.215:8088 as follows:

mkdir 192.168.1.215:8088

echo "He is web-server 192.168.1.215:8088" >index.php

Cat index.php

Now access the virtual host 192.168.1.215:8088, as follows:

Or use ELinks, as follows:

Elinks-dump http://192.168.1.215:8088

Six, according to the domain name configuration virtual Host

Apache virtual hosts are frequently used and are the most common in the production environment.

To configure a domain based virtual host, we still need to edit the httpd-vhosts.conf file. As follows:

Vim httpd-vhosts.conf

: 45,48s/192.168.1.215:8088/a.ilanni.com/g

After the above modifications, we will create the virtual host a.ilanni.com default home page. As follows:

Mkdir/www/a.ilanni.com

echo " " >/www/a.ilanni.com/index.php

cat/www/a.ilanni.com/index.php

After the configuration is complete, reload Apache configuration file, that is, gracefully restart Apache. As follows:

/ETC/INIT.D/HTTPD Graceful

Access the virtual host a.ilanni.com as follows:

Or use ELinks, as follows:

Elinks-dump http://a.ilanni.com

The above figure, you can see the current virtual host a.ilanni.com, has been able to access the normal.

Seven, mixed configuration virtual host

Above we introduce the Apache ip-based, port based and domain based virtual host configuration. Below we give the Apache mixed virtual host configuration, the configuration file is as follows:

ServerAdmin webmaster@dummy-host.example.com

DocumentRoot "/www/b.ilanni.com:8088"

ServerName b.ilanni.com:8088

ErrorLog "Logs/b.ilanni.com:8088-error_log"

Customlog "Logs/b.ilanni.com:8088-access_log" common

Problem with Virtual host mixed:

1, the virtual host mixer can be understood as follows: a row of namevirtualhost directives define all the virtual hosts as a group, and the group with an ip-based virtual host peer. That is, the entire group defined by a row of Namevirtualhost is considered an ip-based virtual host.

2. The port specified by the virtual host must be listen defined. If the virtual host does not specify a port, it is considered to be port 80. If Namevirtualhost * is defined in this way, it refers to all defined ports for all addresses.

3, more specific address definition is preferred. For example, the namevirtualhost directive defines *:80, and an ip-based virtual host is defined as 192.168.0.1:80, then if there is a request to 192.168.0.1:80, then the request is prioritized 192.168.0.1 : 80 virtual hosts defined. So in order to avoid confusion, do not define mutually overlapping or contained address ranges.

4, a virtual host, can also be based on domain names and ip-based. As the last virtual host in the previous example. A request that conforms to both definitions is referred to the same virtual host. Sometimes it is possible to differentiate between an intranet and a virtual host, because requests from intranets may be different from requests from the extranet, but they need to point to the same virtual host.

Using the "_default_" virtual host, this virtual host can be understood as an ip-based virtual host. As follows:

Documentroot/www/default

This virtual host will take over requests that do not match the IP and port of other virtual hosts. However, the primary server will not process any requests. Therefore, it is necessary to configure the primary server as a 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.