How to configure an instance of Apache virtual Host summary _linux

Source: Internet
Author: User
Tags documentation

1, based on IP address of the virtual host

Copy Code code as follows:

Listen 80
<virtualhost 172.20.30.40>
Documentroot/home/httpd/html1
ServerName www.ok1.com
Errorlog/usr/local/apache/logs/error1_log
Customlog/usr/local/apache/logs/access1_log combined
</VirtualHost>
<virtualhost 172.20.30.50>
Documentroot/home/httpd/html2
ServerName www.ok2.com
Errorlog/usr/local/apache/logs/error2_log
Customlog/usr/local/apache/logs/access2_log combined
</VirtualHost>

2, ip-based and multi-port virtual host configuration

Copy Code code as follows:

Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

<virtualhost 172.20.30.40:80>
Documentroot/www/example1-80
ServerName www.example1.com
</VirtualHost>
<virtualhost 172.20.30.40:8080>

documentroot/www/example1-8080
ServerName www.example1.com
</VirtualHost>
<virtualhost 172.20.30.50:80>
Documentroot/www/example2-80
ServerName www.example1.org
</VirtualHost>
<virtualhost 172.20.30.50:8080>
documentroot/www/example2-8080
ServerName www.example2.org
</VirtualHost>

3. Domain-based virtual host configuration on a single IP address server

Copy Code code as follows:

# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests to all IP addresses
Namevirtualhost *:80
<virtualhost *:80>
Documentroot/www/example1
ServerName www.example1.com
Serveralias example1.com. *.example1.com
# Other Directives here
</VirtualHost>
<virtualhost *:80>
Documentroot/www/example2
ServerName www.example2.org
# Other Directives here
</VirtualHost>


If you feel that the above article is not detailed enough to look at the following article:

Experiment Objective: To implement domain-based virtual host in Apache
The XAMPP version of the experiment was 1.7.7, containing the Apache version as 2.2.21

Pre-trial Preparation:

1. In order to test different domain names, in windows/system32/drivers/etc/to find the Hosts file, in which to add a number of experimental domain names, such as-

Copy Code code as follows:

127.0.0.1 test1.net
127.0.0.1 test2.net

So, when you enter the two domain names in the browser, Windows resolves them to 127.0.0.1 local addresses. That is, the XAMPP Welcome page can be accessed by accessing localhost, test1.net, and test2.net in the browser.

2. Create a directory under the Apache directory to place your different sites. In order to protect the content of the Welcome page in the original htdocs of XAMPP, the experiment established a HTDOCS1 directory with Htdocs, under which a test1.net, test2.net two subdirectories were established to place the website for the experiment. Following
Apache/htdocs1/test1.net-place test1.net site content
Apache/htdocs1/test2.net-place test2.net site content

In both directories, the new Hello World one page index.html, Content-

Copy Code code as follows:

<HTML>
<HEAD></HEAD>
<BODY>
</HTML>

Experiment steps:

1. Find apache/conf/httpd.conf, and put the

ServerAdmin
ServerName
DocumentRoot
Comment out.

2. In httpd.conf, locate the row

Include "Conf/extra/httpd-vhosts.conf"
If annotated, the note is taken. This file records the parameters of the virtual host. [the previous virtual host parameters were directly filled in httpd.conf, in order to better organize the files, separate them out, similar to some programming languages.) So include it in the httpd.conf, which is equivalent to filling its contents in the httpd.conf. ]

3. This httpd-vhosts.conf file format is basically as follows-

Copy Code code as follows:

#blah-blah
Namevirtualhost *:80
#blah-blah
#blah-blah
<virtualhost *:80>
ServerAdmin XXXXXXXX
DocumentRoot "XXXXXXXX"
ServerName XXXXXXX
Serveralias XXXXXX
ErrorLog "Logs/xxxxxx-error.log"
Customlog "Logs/xxxxxxx-error.log" combined
</VirtualHost>

What needs to be modified is the parameters in the <VirtualHost>. This can be seen in the official Apache documentation. According to the experimental domain name, can add two <virtualhost>

Copy Code code as follows:

<virtualhost *:80>
ServerAdmin adm@test1.net
DocumentRoot "C:/xampp/htdocs1/test1.net"
ServerName test1.net
Serveralias www.test1.net
ErrorLog "Logs/test1-error.log"
Customlog "Logs/test1-access.log" combined

<directory "C:/xampp/htdocs1/test1.net" >
Order Allow,deny
Allow from all
</Directory>
</VirtualHost>
<virtualhost *:80>
ServerAdmin adm@test2.net
DocumentRoot "C:/xampp/htdocs1/test2.net"
ServerName test2.net
Serveralias www.test2.net
ErrorLog "Logs/test1-error.log"
Customlog "Logs/test1-access.log" combined

<directory "C:/xampp/htdocs1/test2.net" >
Order Allow,deny
Allow from all
</Directory>
</VirtualHost>

Note that if you do not define the accessibility of your directory in each virtualhost, you will encounter access forbidden! even the original localhost.

4. As a result of the comments dropped in the httpd.conf in the servername, DocumentRoot, etc., in order to still be able to localhost access to the original XAMPP Welcome page, is adding a virtualhost, as follows-

Copy Code code as follows:

<virtualhost *:80>
ServerAdmin Adm@localhost
DocumentRoot "C:/xampp/htdocs"
ServerName localhost

ErrorLog "Logs/localhost-error.log"
Customlog "Logs/localhost-access.log" combined

<directory "C:/xampp/htdocs" >
Order Allow,deny
Allow from all
</Directory>
</VirtualHost>

To avoid errors, place it in the first virtualhost position.

At this point, the Apache virtual host configuration is completed based on domain name. You can access the XAMPP Welcome page through http://localhost and access your respective home pages through Http://test1.net and http://test2.net.

#
# Virtual Hosts
#

# If you are want to maintain multiple domains/hostnames on your
# machine can setup virtualhost containers for them. Most configurations
# Use only name-based virtual hosts so the server doesn ' t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please, the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before your try to setup virtual hosts.
#
# with the command line option ' s ' to verify your virtual host
# configuration.

#
# use name-based virtual hosting.
#
Namevirtualhost *:80

#
# VirtualHost Example:
# Almost any Apache directive could into a virtualhost container.
# the ' The ' the ' the ' the ' the ' virtualhost ' is used ' for all requests
# match a ServerName or serveralias in any <VirtualHost> blocks.
#

<virtualhost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs"
    ServerName localhost
    Serveralias www.skydao.com
    ErrorLog "Logs/localhost-error.log"
    Customlog "Logs/localhost-access.log" combined

    <directory "E:/skydao/apache2/htdocs"
    order Allow,deny
    allow to all
    </ directory>   
</virtualhost>

<virtualhost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/project1"
    ServerName project1.com
     Serveralias www.project1.com
    ErrorLog "Logs/project1-error.log"
    Customlog "Logs/project1-access.log" combined

    <directory "e:/skydao/apache2/htdocs/ Project1 "
    order Allow,deny
    allow from all
    < /directory>   
</virtualhost>

<virtualhost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/zendtest/public"
    ServerName zendtest.com
     serveralias www.zendTest.com
    directoryindex index.php
    < Directory/>
Options followsymlinks
allowoverride None
Order Allow,deny
Allow to all
     </Directory>   
</virtualhost>

<virtualhost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/testrewrite"
    ServerName testrewrite.com
     Serveralias www.testRewrite.com
    # directoryindex index.php
    <directory/>
Options followsymlinks
allowoverride None
Order Allow,deny
Allow to all
     </Directory>   
</virtualhost>

<virtualhost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/test"
    ServerName test.com
    Serveralias www.test.com
    ErrorLog "Logs/zendtest-error.log"
    customlog " Logs/zendtest-access.log "Combined

    <directory" E:/skydao/apache2/htdocs/test ">
    order Allow,deny
    allow to all
    </directory >   
</virtualhost>

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.