How to configure the instance summary of apache virtual host

  • 2020-05-07 20:49:34
  • OfStack

1. Virtual host based on ip address


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 and multi-port based virtual host configuration


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. Virtual host configuration based on domain name on a server with a single IP address


# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on 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, you can take a look at the following article:

Experimental objective: to implement virtual host based on domain name in apache
The experimental XAMPP version was 1.7.7, and the apache version was 2.2.21

Preparation before experiment:

1. In order to test different domain, under Windows/System32 / drivers etc/find hosts file, add some experimental domain, such as...


127.0.0.1   test1.net
127.0.0.1   test2.net

When the two domains are entered in the browser, Windows resolves them to 127.0.0.1 local address. That is, the localhost, test1.net, test2.net welcome pages are accessible in the browser.

2. Create a directory under apache to place your different sites. In order to protect the welcome page contents of XAMPP's original htdocs, the htdocs1 directory was set up at the same level as htdocs, under which the test1.net, test2.net subdirectories were set up for the experiment. The following -
apache/htdocs1 / test1 net - placed test1. net website content
apache/htdocs1 / test2 net - placed test2. net website content

  creates a new hello world1 page index.html, content - in each of these directories


<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~,  This is a [ The corresponding website name, in order to distinguish ].net</H1></BODY>
</HTML>  

Experimental steps:

1. Find apache/conf/httpd. conf, will be among them

ServerAdmin
ServerName
DocumentRoot
Commented out.

2. In httpd.conf, find the row

  Include "conf/extra/httpd-vhosts.conf"
If annotated, unannotate. This file records the parameters of the virtual host. [previously, the virtual host parameters were directly filled in httpd.conf, which was separated to better organize the files, similar to some programming languages. Therefore, include in httpd.conf is equivalent to filling its contents in httpd.conf.]

3. The httpd-vhosts.conf file format is 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 < VirtualHost > The parameter in. This can be found in the apache official documentation. Depending on the experimental domain name, you can add two < VirtualHost > :


<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 Directory in each VirtualHost, you will encounter Access Forbidden! Even the original localhost.

4. Since ServerName, DocumentRoot, etc. in httpd.conf were commented out, in order to still access the original XAMPP welcome page with localhost, we are adding one VirtualHost, 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 configuration of apache virtual host based on domain name is completed. Can pass http: / / localhost visit XAMPP welcome page, through http: / / test1 net and http: / / test2 net visit their home page.

#
# Virtual Hosts
#

# If you want to maintain multiple domains/hostnames on your
# machine you 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 see the documentation at
# < URL:http://httpd.apache.org/docs/2.2/vhosts/ >
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any < VirtualHost > block.
#

< 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 from 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 from 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 from 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 from all
      < /Directory >      
< /VirtualHost >


Related articles: