Clone Site With Apache For Local Development [6 Easy Steps]

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

Create sites.conf

Let’s create a new file. The sites.conf contains the config options for all the sites you want develop in your local machine.

First, You should find [apache], and decide what will be the location 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 the content and the logs of your sites are located. It also include the sites.conf. For Example: C:\Sites

Step 1 – Create sites.conf and Include It in Apache config file

First, create a new file [sites]/sites.conf. Next, include it at the end of the Apache config file – Open [apache]/conf/httpd.conf file and add the following line at the end of the file:

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

Add New Sites

To clone site with Apache for local development follow the following steps. You should repeat those steps for each new site you want to clone.

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

  • [site-domain] is the domain where the site is located.
  • [site-name] is 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

Step 2 – Create a new [sites]/[site-name] directory structure

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

  • www – this directory will contains the actual content of the site the should be served by Apache.
  • log – this directory will contains the logs related the site

Step 3 – Add VirtualHost Snippet to sites.conf

To clone site with Apache for local development, we need to add the following VirtualHost snippet to [sites]/sites.conf to [sites]/sites.conf.

VirtualHost Snippet
 <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>

Step 4 – Add mappings from domain to 127.0.0.1

To clone site with Apache for local development, we need to 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

New hosts file.

 127.0.0.1               [site-domain]

Step 5 – Restart Apache

We need to restart Apache so that Apache will use your follow the new changes. Now, we site should be served from our local machine.

Clone Site With Apache Examples

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

Step 1 – Create Directory Structure

Suppose our sites directory is C:/sites, we create the following directory structure:

Creating 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

Step 2 – Include sites.conf in httpd.conf

We include it in sites.confin [apache]/conf/httpd.conf

Include sites.conf in httpd.conf
 ...
 Include "C:/sites/sites.conf"

Step 3 – Add Virtual Snippets to sites.conf

Add the 3 Virtual Snippets to sites.conf

Add Virtual Snippets to 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>

Step 4 – Add mappings from domain to 127.0.0.1

Add mappings from domains to 127.0.0.1
 127.0.0.1               www.demo.com
 127.0.0.1               s1.demo.com
 127.0.0.1               s2.demo.com

Step 6 – Restart Apache

We need to restart Apache so that Apache will use your follow the new changes.

After Apache was restarted:

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

Now, after we clone site with Apache, we can start to develop with PHP locally

Clone Site With Apache For Local Development

Total Time 10 minutes

Create sites.conf and Include It in httpd.conf

Create a new [sites]/[site-name] directory structure

Add VirtualHost Snippet to sites.conf

Add mappings from domain to 127.0.0.1

Step 5 – Restart Apache

Leave a Reply

Your email address will not be published. Required fields are marked *