In this tutorial I’ll walk you through setting up 3 Apache virtual hosts running on a single Ubuntu server. An Apache virtual host, as explained by Apache, refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. Virtual hosts can be “IP-based” or “name-based”. By default, Ubuntu already has this capability enabled, so things are much easier to configure these days. Enough with the intro, this is how you do it:
We are going to create 3 sites, site1.com, site2.com and site3.com
1. Create the folders that will host your new sites. By default, Apache in Ubuntu serves from /var/www. So create these:
mkdir /var/www/site1 mkdir /var/www/site2 mkdir /var/www/site3
2. Copy the current default setting found in /etc/apache2/sites-available/default and name it the same as your new site.
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1 cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site2 cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site3
3. Edit the new config files for each site using your preferred text editor. Add the line ServerName server1 right below the ServerAdmin line and change both DocumentRoot and Directory to point to your new sites.
This is what it should look like (you’ll do exactly the same for each of your 3 new sites, repeat this step for as many new sites as you’ll be creating):
/etc/apache2/sites-available/site1
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName site1
DocumentRoot /var/www/site1
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/site1/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
4. After you have edited the config files for each of the 3 or how many virtual hosts you are creating, just tell Apache to start serving the new domains and stop serving the default:
sudo a2ensite site1 sudo a2ensite site2 sudo a2ensite site3 sudo a2dissite default
5. Now reload apache and you should be able to get to each of your new domains:
sudo /etc/init.d/apache2 reload
Congratulations, you have done it, 3 sites running off of the same Apache server. Hopefully this tutorial helped you. Thank you for stopping by and please share with others, after all, code should be free.![]()

Great easy article. I just have 2 problems. 1st I did something not here. I added ” 127.0.0.1 localhost site1 site2 site3″ to /etc/hosts . 2 when i look for “site1/” on firefox it gives me an error something about not being unable to determine IP address. However when i type site1/ on chrome the index file opens up beutifully.
@Hugo You might want to clear firefox’s cache. It should work, especially since it works from chrome, so you know it isn’t a configuration error.
Yep clearing firefox’s cache work. Thanks!
@Hugo Not a problem, I’m glad it worked
Hello dude, i am newbie and dont know how exactly it works . I have followed this steps and now i want to test it but not sure how ? i have domain something like 127.123.32.232:2107(its not a localhost machine). i have created new site with name vhtest.
can u help me, and let me know how can i test new host.
i put something like this when i test other things 127.123.32.232:2107/abc
@Amit Thanks for stopping by my site. The easiest way for you to achieve this would be creating an entry in your /etc/hosts file and add an entry as 127.123.32.232:2107 vhtest
then you will be able to reach it by going to http://vhtest
This would ensure you can reach the server although you don’t have the domain registered, but you’ll obviously only going to be able to reach it from whichever machine you have added the entry above to the /etc/hosts file.
Dan: thanks for the reply and solution. one more question , my server is 127.123.32.232:2107 and my files are located in web/ directory, my problem is i want to remove/hide “web” from url but want to point to the correct file location. i somehow managed to hide “web” from url using htacess but its not poining to correct location i mean it tries to open a file from path like this 127.123.32.232:2107/index.php, actually it should open the path from 127.123.32.232:2107/web/index.php
I dont know if this is ur area of expertise.
@Amit If you would like to serve 127.123.32.232:2107/web/index.php you’d have to have these entries in the sites available file for your virtualhost vhtest, then restart apache, (I’m assuming you have apache serving from /var/www)
(Make sure to remove the double quotes from the code below)
DocumentRoot /var/www/web
“< " Directory /var/www/web/ ">”
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
“< "/Directory ">“
I’m grateful you made the post. It’s caerled the air for me.
@Kaylynn Thanks for visiting my site, please feel free to come back. I’m glad I was able to help you.
Really very very intresting article related to Apache Virtual Hosts
Pingback: Cheat Sheet for Adding Virtual Hosts in Ubuntu | Island of Lost Circuits
Pingback: FB App Programming (Part 1) – Heroku « Summer and winter and springtime and harvest
Pingback: Set Virtual Hosts. « Codeperl's Knowledge Sharing System
Pingback: Multiple copies of wordpress on ubuntu server « aaronprice04
Hi there, I already follow your tutorial but how to call the site on browser? when I try http://site1 it nothing, did I missing something?
thank you
@ranggadablues Please add the ip address and hostname to your /etc/hosts file
How about using Webmin or Zentyal, is it easier to configure your’s every need?
@angkung Thanks for stopping by and taking your time to post a comment. Webmin and zentyal might be easier and some people might prefer it, I haven’t used them in the past and since I’m not sure how they work (since I haven’t used them), I wanted to write this tutorial instead. Thank you for the heads up though, maybe someday I’ll have time to check those out and write another tutorial on this topic, this time using webmin and zentyal.
Pingback: My Lubuntu install on laptop | Sam's PHP How-To
Good Tutorial
@Ashwin Thanks
Thanks, I’m coming from CentOS and Ubuntu is a little weird to me, nor had I done virtual hosts. By far the simplest and most useful description.
@Stephen Thanks, glad I could help
Спасибо большое. Правда я делал все почти день. Для меня все было сложно из-за не знания английского языка, но я все равно сделал свои 3 сайта на домашнем компьютере с Ubuntu. Всегда был один только сайт по умолчанию. А теперь три сайта. Большое тебе спасибо дружище!!!
Thanks and appreciating your publication in Apache Virtual Hosts.
@Nishantha Thanks for the comments. Glad I was able to help.
@Михаил Thanks for the nice comments, I understand how difficult it can be find certain tutorials on the net, glad I could help.
Pingback: Primeiros Passos com Laravel 4 | Zizaco.net
Amazing work! Option sort of facts which should be distributed on the internet. A sense of shame on the internet without longer setting this informative article increased! Think about it over in addition to visit my internet site. Thank you Equals)
@Directory100 Thanks for the comments, feel free to share my links with everyone. I’m glad I was able to help.