Sometimes, it is useful to replicate online site in a local machine for development purposes. In this article , we will see how to configure apache to serve clones of online sites.

Creating sites.conf

In this step we will create sites.conf. The sites.conf contains the config options for all the sites you want develop in your local machine.

First , we should find [apache] location and decide what will be the [sites] location:

  • [apache] is the location where the apache server is installed.
    • for example : C:/Program Files/apache
  • [sites] is the location where sites.conf and the content and the logs of your sites.
    • for example : C:/sites

Now , follow those steps:

(1) Open [apache]/conf/httpd.conf config file and add following line in its end:

[apache]/conf/httpd.conf
Include "[sites]/sites.conf"

(2) Create the newly included file [sites]/sites.conf

Adding a site

In this step we will add new site.

First, you should know [site-domain] and generate a unique [site-name]:

  • [site-domain] - The domain where the site is located
    • for example : www.demo.com , s1.demo.com
  • [site-name] - a unique name which identify the site. A simple strategy to generate this name:
    • For www.name.extension use name
      • For example for www.demo.com use demo
    • For subdomain.name.extension use name_subdomain
      • For example for s1.demo.com use demo_s1

Now , follow those steps:

(1) create a new [sites]/[site-name] directory, and create the following sub directories inside:

  • www - this directory will contians the actual content of the site the should be served by apache.
  • log - this directory will contians the logs related the site

(2) Add the following config snippet to [sites]/sites.conf

[sites]/sites.conf
<VirtualHost 127.0.0.1:80>
    ServerName   [site-domain]
    ErrorLog     "[sites]/[site-name]/log/error.log"
    DocumentRoot "[sites]/[site-name]/www"
    <Directory "[sites]/[site-name]/www">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

(3) Add a new mappings of IP addresses to [site-domain] in hosts file. This file is usually located in C:\Windows\System32\drivers\etc

hosts
127.0.0.1               [site-domain]

(4) Start/Restart apache

Example

Suppose, we want to develop www.demo.com , s1.demo.com , s2.demo.com on our local machine.

(1) we decide that our sites directory will be C:/sites.

(2) we create the following directory structure:

sites directory structure
C:/sites                     ├ sites.conf
├────────────demo            ├
│           ├────────────www ├ index.html ...
│           └────────────log ├ error.log , transfer.log
├────────────demo_s1         ├
│           ├────────────www ├ index.html ...
│           └────────────log ├ error.log , transfer.log
└────────────demo_s2         ├
            ├────────────www ├ index.html ...
            └────────────log ├ error.log , transfer.log

(3) We edit sites.conf:

[sites]/sites.conf
<VirtualHost 127.0.0.1:80>
    ServerName   www.demo.com
    DocumentRoot "C:/sites/demo/www"
    ErrorLog     "C:/sites/demo/log/error.log"
    TransferLog  "C:/sites/demo/log/transfer.log"
    <Directory "C:/sites/demo/www">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
<VirtualHost 127.0.0.1:80>
    ServerName   s1.demo.com
    DocumentRoot "C:/sites/demo_s1/www"
    ErrorLog     "C:/sites/demo_s1/log/error.log"
    TransferLog  "C:/sites/demo_s1/log/transfer.log"
    <Directory "C:/sites/demo_s1/www">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
<VirtualHost 127.0.0.1:80>
    ServerName   s2.demo.com
    DocumentRoot "C:/sites/demo_s2/www"
    ErrorLog     "C:/sites/demo_s2/log/error.log"
    TransferLog  "C:/sites/demo_s2/log/transfer.log"
    <Directory "C:/sites/demo_s2/www">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

(4) We include it in [apache]/conf/httpd.conf

[apache]/conf/httpd.conf
...
Include "C:/sites/sites.conf"

(5) We edit hosts file:

hosts
127.0.0.1               www.demo.com
127.0.0.1               s1.demo.com
127.0.0.1               s2.demo.com

(6) We start/restart apache.

Now, when we access :

  • http://www.demo.com , index.html under C:/sites/demo/www will be served
  • http://s1.demo.com , index.html under C:/sites/demo_s1/www will be served
  • http://s2.demo.com , index.html under C:/sites/demo_s2/www will be served