Virtual Hosting allow web servers to host more than one website on a sing machine. This is how sharing hosting works. I become pretty handy as well while develloping different web project on the same machine and allows you to access to your local repository using addresses such as http://dev.mysite.com instead of http://localhost/~myuser/myproject/ :) .
This tutorial is based on a machine runnning ubuntu/linux but should be the same on any debian based distribution and almost the same on other distributions.
First of all, you need an apache server ready to run on your machine, if it is not yet install, open a terminal and type
$sudo apt-get install apache2-utils apache2-common
Once the server is installed, it is time to get into apache 2 configuration.
Let's open apache's main configuration file, name /etc/apache2/apache2.conf. A search for the word bring us to the following line:
This mean that when starting apache, it will look for files in /etc/apache2/sites-enabled/.
Lets go there and see what is in.
lrwxrwxrwx 1 root root 36 2005-12-27 01:42 000-default -> /etc/apache2/sites-available/default
Well, this only links to the file in directory /etc/apache2/sites-available/ . You might wonder what is the point in doing such. Well, this simply allows you, mainly when you are using your box as a web server, to:
- Have a simple file
- Do be able to edit or create a new host by creating/editing a file from /etc/apache2/sites-available/
- In case your web server doesn't restart because of misconfiguration, you can simply remove the link from the file in /etc/apache2/sites-enabled/ pointing to the malformed file in /etc/apache2/sites-available/
Now let say you want to be able to map the domain name dev.example.com to you local machine, using the code file in /home/myuser/public_html/example.com/.
While in /etc/apache2/sites-available, create a new file (let say )
$sudo vi example.com.conf
Now add the following lines:
#We want to be able to access the web site using www.dev.example.com or dev.example.com
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/example.com-access.log combined
People who don't want to bother knowing how the site enabling system works might just jump to the end of the article to find debian built-in command syntax. If you want to know how it works, or do not use a debian based distro, carry on.
Now, we specified a new host to apache but it is not yet linked to the repertory where apache actually look for virtual hosts. Let go to:
and create a link to the file we just created:
$sudo ln -s /etc/apache2/sites-available/example.com.conf example.com.conf
Now apache is almost ready to restart, but before doing so, we must inform our linux system that and are not to be looked for on the net, but on the local machine instead.
To do so, simply edit /etc/hosts and add the new host names at the end of the line beginning by 127.0.0.1, which is localhost.
In the end, your file should look like:
And now we are done, simply reload apache:
sudo /etc/init.d/apache2 reload
Open your web browser and enter the following address dev.example.com. Magic, it runs the same as when you were using http://localhost/~myuser/example.com but it is far more usefull when devellopping a web service and want to be able to develop applications on your machine just like it is where the real web site.
As you can see from the comments, many people pointed out that you can use a debian specific command (so if you are not using a debian based system, don't expect to find that command :) ).
to simply type:
sudo a2ensite mysiteavailable-site
sudo a2dissite mysiteavailable-site
where mysiteavailable-site is the name of the virtual hos you want to enable/disable, so in out example: example.com.conf
Hope this helped.