Setting Up A Name Based Virtual Host (vHost)

For Your Local Dev or Live Site

Having a local mirror copy of your live site is great if you want to work on it without risking or breaking your live site, and is also good for developing your future site, making a theme or simply learning the ropes before you take the plunge and buy a domain and hosting.

So to begin, start with choosing a ccTLD or imaginary TLD domain name for your site. Chose a ccTLD code element that can be used without restriction, a user assigned code in the range between .qm-.qz or .xa-.zz. Or you can chose .aa. I like .aa .zz or .xx best since they are easy to type. Choosing one of these will not conflict with the assigned domains available on the web. Another option is to not use any, so instead of example.zz you can just use example, but using a real ccTLD or an imaginary TLD is cool and your local site becomes so much real.

Unix, Linux and BSD:

On unix like operating systems, Mac included I’d guess, start by editing your /etc/hosts file and enter something like this:

# vHosts
127.0.0.1       redivide.zz
127.0.0.1       www.redivide.zz
127.0.0.1       example.zz
127.0.0.1       www.example.zz
127.0.0.1       example-1.zz
127.0.0.1       www.example-1.zz

Next enter these vHost directives in /etc/apache2/vhosts.d/vhosts.conf (on my Suse Linux) or whatever you path to apache is:

# Listen for virtual host requests on all IP addresses, localhost, or any other address
    NameVirtualHost 127.0.0.1:80


    ServerAdmin admin@redivide.zz
    DocumentRoot /home/redivide.zz
    ServerName redivide.zz
    ServerAlias redivide.zz *.redivide.zz
#    ErrorLog /home/logs/redivide.zz-error_log
#    CustomLog /home/logs/redivide.zz-access_log common



    ServerAdmin admin@example.zz
    DocumentRoot /home/example.zz
    ServerName example.zz
    ServerAlias example.zz *.example.zz
#    ErrorLog /home/logs/example.zz-error_log
#    CustomLog /home/logs/example.zz-access_log common



    ServerAdmin admin@example-1.zz
    DocumentRoot /home/example-1.zz
    ServerName example-1.zz
    ServerAlias example-1.zz *.example-1.zz
#    ErrorLog /home/logs/example-1.zz-error_log
#    CustomLog /home/logs/example-1.zz-access_log common

Having separate logs is optional if you don’t care about stats, and why should you on a localhost site, so they can be commented out.

In a real live shared IP server, the DocumentRoot would likely be /home/ClientName/public_html/sitename.com or something like that. This should fix the common error that people have been having with vHosts as well:
[warn] _default_ VirtualHost overlap on port 80, the first has precedence


Windows:

On Windows get XAMPP, if you don’t have it already, from ApacheFriends.org Apache Friends. XAMPP is the best local server for Windows that includes PHP, Perl, and Tomcat for Java. Ruby and Python can also be installed with a little work.

First edit your C:\WINDOWS\system32\drivers\etc\hosts file and add something like this:

# vHosts
127.0.0.1       redivide.zz
127.0.0.1       www.redivide.zz
127.0.0.1       example.zz
127.0.0.1       www.example.zz
127.0.0.1       example-1.zz
127.0.0.1       www.example-1.zz

Open the file \xampp\apache\conf\extra\httpd-vhosts.conf in a text editor and at the end paste this:

# Listen for virtual host requests on all IP addresses, localhost, or any other address
    NameVirtualHost 127.0.0.1:80


    ServerAdmin admin@redivide.zz
    DocumentRoot c:/xampp/vhost/redivide.zz
    ServerName redivide.zz
    ServerAlias redivide.zz *.redivide.zz
#    ErrorLog c:/xampp/vhost/logs/redivide.zz-error.log
#    CustomLog c:/xampp/vhost/logs/redivide.zz-access.log common



    ServerAdmin admin@example.zz
    DocumentRoot c:/xampp/vhost/example.zz
    ServerName example.zz
    ServerAlias example.zz *.example.zz
#    ErrorLog c:/xampp/vhost/logs/example.zz-error.log
#    CustomLog c:/xampp/vhost/logs/example.zz-access.log common



    ServerAdmin admin@example-1.zz
    DocumentRoot c:/xampp/vhost/example-1.zz
    ServerName example-1.zz
    ServerAlias example-1.zz *.example-1.zz
#    ErrorLog c:/xampp/vhost/logs/example-1.zz-error.log
#    CustomLog c:/xampp/vhost/logs/example-1.zz-access.log common

On Windows, full paths should be used, and back slashes \ should be replaced with unix forward slashes /. This is the easiest method to have a live site and a backup mirror dev site where you can try different things.

For more vHosts examples visit: apache.org
For vHost directives go to: apache.org

If you need to use the default apache htdocs, with the 127.0.0.1 IP, likely it will stop working and will get redirected to the first site in the vHosts file, then you have to change the vHost IP from 127.0.0.1 to 127.0.0.2 in your hosts file. A LAN IP works just as well. Don’t forget to change the NameVirtualHost directive as well or else you get the error above. Another option is to vHost the default htdocs as well.

Now download your live Wordpress site, make a vHost for it, import your db via phpMyAdmin and edit the wp-config.php. After all this is done enter YourSiteName.zz in Firefox.

Note: This post has been mangled for some time because the code highlighter broke in an update. Sorry about that.




About this entry