Jun 14, 2015

How to run multiple sites on one Apache

Assuming we have two sites to configure in Apache
www.example1.com
www.example2.com

/etc/httpd/conf.d/example1_http.conf
<VirtualHost *>
        ServerAdmin webmaster@example1.com
        ServerName  www.example1.com
        ServerAlias example1.com

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example1.com/htdocs/
.....
</VirtualHost>

/etc/httpd/conf.d/example2_http.conf
<VirtualHost *>
        ServerAdmin webmaster@example2.com
        ServerName  www.example2.com
        ServerAlias example2.com

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example2.com/htdocs/
.....
</VirtualHost>

Configure Hosts File in Windows:

The Hosts file in Windows is located at the following location:
C:\Windows\System32\drivers\etc

Suppose your system IP is: 192.55.44.55

Add the following to the Hosts File:
192.55.44.55 example1.com
192.55.44.55 example2.com
192.55.44.55 www.example1.com
192.55.44.55 www.example2.com

Restart Apache

Now your Apache runs the two multiple sites

You can access example1.com and example2.com respectively

Access Default Site when Server Name/Alias mismatches

When you have multiple sites (multiple conf files) in conf.d, you can create a default conf file

If Apache finds difficulty in finding the site, it defaults to
The following defaults to www.example1.com in case of any conflicts

/etc/httpd/conf.d/aaa_http.conf
<VirtualHost *>
        ServerAdmin webmaster@example1.com
        ServerName  www.example1.com
        ServerAlias example1.com

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example1.com/htdocs/
.....
</VirtualHost>

How to simulate the conflict state where Apache not able to find the correct site conf file

For two different sites, give the same ServerName
/etc/httpd/conf.d/test1_http.conf
          ServerName  www.test.com
/etc/httpd/conf.d/test2_http.conf
          ServerName  www.test.com

If you try to access www.test.com, Apache gets CONFUSED to pick which sites conf file (whether to choose test1_http.conf or test2_http.conf)

So Apache picks the top conf file (as per naming order) defined above aaa_http.conf
So you are re-directed to www.example1.com (as defined in aaa_http.conf  

 

No comments:

Post a Comment