Virtual Hosts How-to

A Virtual Host is a way to have more than one web domain name on a single machine.

This is how all web servers work these days: Most any website is only one of many others on one server.

Example: www.tibetangeeks.com and www.ecotibet.org and tenzintsundue.com all have their files on the same machine at HostGator.
They each have their own document root
In all code and programming, they are totally independent sites.
But they are all on the same machine, and they all have the same ip number.

If you develop websites, it is very useful to do the same thing on your own machine.

Maybe now you do something like: http://localhost/musicsite/ and http://localhost/practice/.
Instead you can do something like: http://music/ and http://practice.

Advantages:

(This is called "name-based virtual hosting". It means that you have multiple "names" running on each IP address.)

↑ The navigation above shows a summary of how to do it.
↓ Below are all the gory details!

First: Tell apache you want to use virtual hosts

Edit this file:

fedora ubuntu mac osx mswin 7
/etc/httpd/conf/httpd.conf /etc/apache2/apache2.conf [more] /private/etc/apache2/httpd.conf [more] C:\wamp\bin\apache\Apache2.2.1\conf\httpd.conf OR C:\xampp\apache\conf\httpd.conf
(You will need to be root or use sudo to edit this file) (You can edit this file as regular user.)
1
Look for the section
# Dynamic Shared Object (DSO) Support

Scroll down, you will see many lines of library files to be loaded.
Make sure this line is uncommented:
LoadModule vhost_alias_module modules/mod_vhost_alias.so
2
Look for the section
### Section 3: Virtual Hosts

After some useful comments you will see:
# Use name-based virtual hosting.
#
# NameVirtualHost *:80


Make sure this line is uncommented, like this:
NameVirtualHost *:80
3
Then there should be a line somewhere after that, to tell apache to load your virtual host instructions:
# Load virtual host config files:
Include conf/vhosts

The httpd.conf file is a great example of how to comment. You can learn a lot about how web servers work by reading it. (Slowly slowly! There is a lot there.)

Next - To make each virtual host:

1. filesystem: make a separate folder for the new website files

Make this folder inside your web document root folder. Name it anything you want.

(What is the 'web document root'? ... that is, where you have set DocumentRoot to be, in your httpd.conf file.)

The folder path might be like this:

fedora ubuntu mac osx mswin 7
/var/web/www/musicsite/ /var/www/musicsite/ /Library/WebServer/Documents/musicsite/ C:\wamp\www\musicsite\ OR C:\xampp\htdocs\musicsite\

2. apache configuration file: tell apache where the files are for the new virtual host

2a. Where is the virtual hosts config?

fedora ubuntu mac osx mswin 7
/etc/httpd/conf/vhosts/ /etc/apache2/sites-enabled/[^.#]* /etc/apache2/sites-available/* /usr/sbin/a2ensite /usr/sbin/a2dissite [more] /private/etc/apache2/extra/httpd-vhosts.conf [more] C:\wamp\bin\apache\Apache2.2.1\conf\httpd.conf OR C:\xampp\apache\conf\httpd.conf
(You will need to be root or use sudo to edit these files) (You can edit this file as regular user.)

Open the virtual hosts config file in your favorite text editor.

2b. What to put in it?

Decide on what name you want for your virtual host. I usually think of something short, because i will probably by typing it often, in the browser address bar, and in my code.

Example: So if i am making a new virtual host for my music website, maybe i will call it music.

There will be a sample VirtualHost block in the existing file ... but i don't find it to be useful.
You can copy and paste it for a new virtual host, and then change it so it is like this:

fedora ubuntu mac osx mswin 7
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/web/www/musicsite/www ServerName music.localhost ServerAlias music ErrorLog logs/music-error_log CustomLog logs/music-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] # DocumentRoot "C:\wamp\www\music\www" # better: put on another drive! DocumentRoot "D:\websites\music\www" ServerName music.localhost ServerAlias music ErrorLog logs/music-error_log CustomLog logs/music-access_log common </VirtualHost>

2c. Now save it?

yes :)

2d. One more small thing

When you have virtual hosts set up, the first Virtualhost block has to be for localhost. You can put it in httpd.conf, or in your file that contains virtual hosts, or as a separate virtual hosts file — but it has to be loaded first.

fedora ubuntu mac osx mswin 7
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/web/www ServerName localhost.localdomain ServerAlias localhost ErrorLog logs/localhost-error_log CustomLog logs/localhost-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] # DocumentRoot "C:\wamp\www" # better: DocumentRoot "D:\websites" ServerName localhost.localdomain ServerAlias localhost ErrorLog logs/localhost-error_log CustomLog logs/localhost-access_log common </VirtualHost>

3. 'hosts' file tell the computer that our virtual host domain lives here

Now we need to tell the computer that music lives here, and is not out on the big Web.

3a. The 'hosts' file is located in:

fedora ubuntu mac osx mswin 7
/etc/hosts /etc/hosts /private/etc/hosts C:\windows\system32\drivers\etc\hosts

3b. Open up this file in your favorite text editor.

fedora ubuntu mac osx mswin 7
su to root to edit sudo to edit sudo to edit Right-click on Notepad (not Notepad++) and "run as Administrator"

3c. Notice the first line in the hosts file:

(The lines with '#' in front are "comments" - they are for you to read.)

127.0.0.1    localhost

3d. Add your virtual host "Server Alias" that you made in your virtual hosts configuration:

127.0.0.1     localhost
127.0.0.1     prac

3d. Save this file.

4. Restart apache: so that he can reread the configuration files

fedora ubuntu mac osx mswin 7
httpd -k restart OR apachectl restart sudo apachectl restart sudo apachectl restart Wamp menu → Restart all services Xampp → ???

4a. Try it!

Type music in the browser address bar, and see the files in the musicsite folder.

4b. Problems?

Next section !

Fixing problems

So - you restart apache, and ...
it won't start,
or
it starts, but you don't see the files for the music website ...

How to find out why apache didn't start.

Still can't figure out what's wrong? Apache is talking to you!

  fedora ubuntu mac osx mswin 7
1
terminal window Start → Run → type cmd
2
su sudo sudo navigate (cd) to your apache/bin directory
3
Now ask apache very nicely, to explain the problem to you:
  • To find out any syntax errors type
    httpd -t
  • To find out any errors with the virtual host setup, type
    httpd -S
  • Edit the file shown in the error output, to fix the errors,
    and start apache again.

At this point, there are so many little things that could be wrong, that i can only give the above ideas, and then: Your great brain and the great Google are there waiting for you!

More Info

Look in your apache manual, which was installed on your machine when you installed apache.
Left column: "Users Guide"
Last item in the list: "Virtual Hosts"