Setting Up A Name Based Virtual Host (vHost)

Apache Foundation logoI always liked to have a local mirror copy of your live site, which 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 (i.e. sitename.whatever, or just sitename) 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.

However a domain is not necessary. The hostname or any other name will do.

Unix, Linux and BSD Virtual Hosts:

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

1
2
3
4
5
6
7
# 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Listen for virtual host requests on all IP addresses, localhost, or any other address
    NameVirtualHost 127.0.0.1:80
 
<virtualhost redivide.zz: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
</virtualhost>
 
<virtualhost example.zz:80>
    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
</virtualhost>
 
<virtualhost example-1.zz:80>
    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
</virtualhost>

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 this common error that people have been having with vHosts as well:
[warn] _default_ VirtualHost overlap on port 80, the first has precedence



Windows Apache Virtual Hosts:

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 Apache MySQK, and PHP. Perl, and Tomcat for Java can also be installed as an addon. Ruby and Python can be installed as well, but with more work.

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

1
2
3
4
5
6
7
# 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Listen for virtual host requests on all IP addresses, localhost, or any other address
    NameVirtualHost 127.0.0.1:80
 
<virtualhost redivide.zz: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
</virtualhost>
 
<virtualhost example.zz:80>
    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
</virtualhost>
 
<virtualhost example-1.zz:80>
    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
</virtualhost>

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.


About this entry