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!
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.)
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\ |
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.
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 root@localhost 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 root@localhost # 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> |
yes :)
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 root@localhost DocumentRoot /var/web/www ServerName localhost.localdomain ServerAlias localhost ErrorLog logs/localhost-error_log CustomLog logs/localhost-access_log common </VirtualHost> | <VirtualHost *:80> ServerAdmin root@localhost # 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> |
Now we need to tell the computer that music lives here, and is not out on the big Web.
fedora | ubuntu | mac osx | mswin 7 |
---|---|---|---|
/etc/hosts | /etc/hosts | /private/etc/hosts | C:\windows\system32\drivers\etc\hosts |
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" |
(The lines with '#' in front are "comments" - they are for you to read.)
127.0.0.1 localhost
127.0.0.1 localhost 127.0.0.1 prac
fedora | ubuntu | mac osx | mswin 7 |
---|---|---|---|
httpd -k restart OR apachectl restart | sudo apachectl restart | sudo apachectl restart | Wamp menu → Restart all services Xampp → ??? |
Type music in the browser address bar, and see the files in the musicsite folder.
So - you restart apache, and ...
it won't start,
or
it starts, but you don't see the files for the music website ...
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:
|
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!
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"
All content not copyright by anyone else is
copyright © 2003–2012 James Walker.
License for use is the GNU Free Documentation License.
Find it:
here in the
License directory
or
at the Free Software Foundation,
www.fsf.org