Apache Virtual Hosts in Ubuntu in 5 easy steps

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):


<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName site1
	DocumentRoot /var/www/site1
	<Directory />
		Options FollowSymLinks
		AllowOverride All
	<Directory /var/www/site1/>
		Options -Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all

	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


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.foscode.com | because code should be free

45 thoughts on “Apache Virtual Hosts in Ubuntu in 5 easy steps

  1. Great easy article. I just have 2 problems. 1st I did something not here. I added ” 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.

  2. @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.

  3. 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 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

  4. @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 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.

  5. Dan: thanks for the reply and solution. one more question , my server is 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, actually it should open the path from
    I dont know if this is ur area of expertise.

  6. @Amit If you would like to serve 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 ">“

  7. @Kaylynn Thanks for visiting my site, please feel free to come back. I’m glad I was able to help you.

  8. Pingback: Cheat Sheet for Adding Virtual Hosts in Ubuntu | Island of Lost Circuits

  9. Pingback: FB App Programming (Part 1) – Heroku « Summer and winter and springtime and harvest

  10. Pingback: Set Virtual Hosts. « Codeperl's Knowledge Sharing System

  11. Pingback: Multiple copies of wordpress on ubuntu server « aaronprice04

  12. 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

  13. @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.

  14. Pingback: My Lubuntu install on laptop | Sam's PHP How-To

  15. Спасибо большое. Правда я делал все почти день. Для меня все было сложно из-за не знания английского языка, но я все равно сделал свои 3 сайта на домашнем компьютере с Ubuntu. Всегда был один только сайт по умолчанию. А теперь три сайта. Большое тебе спасибо дружище!!!

  16. Thank you. True, I did almost all day. For me it was difficult because no knowledge of English, but I still made ​​my 3 sites on the home computer with Ubuntu. Was always only one default site. And now the three sites. Thank you very much my friend!

    @Михаил Thanks for the nice comments, I understand how difficult it can be find certain tutorials on the net, glad I could help.

  17. Pingback: Primeiros Passos com Laravel 4 | Zizaco.net

  18. 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)

  19. @Directory100 Thanks for the comments, feel free to share my links with everyone. I’m glad I was able to help.

  20. You have a great tutorial. thank you.I have a problem relating the vhost tutorial to suit my purpose. Suppose I want 2 sites on my ubuntu server with this 2 subdomain abc.ralph.com and def.ralph.com. Can this tutorial serve and how do I go by it.Thanks again you are a great teacher with your simplicity.I will subscribe you now.

  21. Thanks for the concise instructions!

    One question: If my domain names are registered at Godaddy and I point them to my remote Ubuntu server, can I still use the FQDM or do I need to an IP address?

    Thank you in advance!

  22. At first, thanks for this tutorial, nice and easy cleany written and understandable above all.
    However i have a question related to one of the latest comments i think,
    and i hope you can help me out, or atleast shed a light on this:
    situation is as follows:
    I have 1 machine running Ubuntu Server 12.04.2 and has local LAN IP
    Apache Webserver version 2.xx installed (the one that came with ubuntu) LAMP functioning
    (but LAMP is not related to my question)
    I have userdir_mod enabled, which allows me to serve websites from ‘user directories’ public_html
    And i want to virtually host them behind different (external registered) domains
    so for example, i have created user ‘ foobar ‘ and he has the abillity to serve webcontent from the folder:
    then i created user ‘barfoo’ and he has the abillity to serve webcontent from the folder:
    both have different independant websites.
    and i respectively created virtual hosts for them (virtual host foobar with DocumentRoot /home/foobar/public_html,
    and virtual host barfoo with DocumentRoot /home/barfoo/public_html)
    this far this works.
    HOWEVER, i have for example registered the domains foobar.com, and barfoo.com at ‘GODADDY’
    but i only have 1 main ip i can make it point to ofcourse, (my main ISP-IP or locally seen
    since they run off godaddy and not local.
    while i ofcourse want to point http://www.foobar.com to the virtual host foobar , and http://www.barfoo.com, towards virtual host barfoo.
    I think one of the only ways to get this done, is running a DNS nameserver? at home (for example on that same machine)
    so i can point both godaddy’ s domains towards the 1 ip the server has, and the name server figures then hopefully out,
    which navigation requests gets routed to which virtual host? or how is this done? if it’ s possible in the first place
    already, i really don’ t know , but i would like to figure that one out ,
    and was hoping you had some experience with this and could help me out.
    since i tried to explain my question as easy as possible, and wouldn’t know how to formulate it another way

    Anyway, Thanks in advance!

  23. Hi Dan,

    Could you tell me host configure forward prosxy server in ubuntu …

  24. Hi,

    I am having one problem with virtual host setup…

    I created virtual host & enabled but it is redirecting to default server ..?

    Please help to resolve this issue..is there any virtual host configuration issue or anything we need to do for proper redirection ?

    Please help me to get it done!


  25. Pingback: Apache Virtual Hosts | MingNet

  26. About to create a number of new hosts using this clear tutorial, I have no doubt it will work! However I have on small query. I have already setup a host on the default. Your instruction says to stop serving the default, is this necessary or do I have to do it? I presume that stopping it means I will no longer be able to access it.

  27. This is a good tip especially to those fresh to the blogosphere.

    Simple but very precise info… Many thanks for sharing this one.
    A must read post!

  28. Y esa amenaza surge de la funesta combinación de frivolidad académica liberal y ansía de los gestores financieros que se traduce en ineficacia de comportamiento de los mercados libres
    de control.

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>