Ubuntu Feisty - Apache Virtual Hosts

Now we have Apache installed and running, we can configure it to serve multiple domains using Virtual Hosts.

Do note the layout used in these articles is explained here - feel free to use the directories of your choice.

Create the layout

Let's create the basic layout for each domain. In your home directory create a 'public_html' folder:

cd ~
mkdir public_html

Now for each domain we will host (I use example names of domain1.com and domain2.com) create a folder with a standard set of sub-folders:

mkdir -p public_html/domain1.com/{public,private,logs,cgi-bin,backup}


mkdir -p public_html/domain2.com/{public,private,logs,cgi-bin,backup}

That will create the folders public, private, logs, cgi-bin and backup for each of our domains (domain1.com and domain2.com).


The content of the public folder is, naturally, up to you. For this example I am going to use a very simple html file so we can check the virtual hosts work.

So for each domain:

nano public_html/domain1.com/public/index.html

The content index.html:


Repeat the process so you have a similar file for domain2.com (don't forget to change the index.html content so it shows domain2.com and not domain1.com).


OK. Now we have a basic setup for our two domains.

Let's create the vhost file for domain1:

sudo nano /etc/apache2/sites-available/domain1.com

The contents look like this:

# Place any notes or comments you have here
# It will make any customisation easier to understand in the weeks to come

# domain: domain1.com
# public: /home/demo/public_html/domain1.com/

<VirtualHost *>

  # Admin email, Server Name (domain name) and any aliases
  ServerAdmin webmaster@domain1.com
  ServerName  domain1.com
  ServerAlias www.domain1.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html
  DocumentRoot /home/demo/public_html/domain1.com/public

  # Custom log file locations
  LogLevel warn
  ErrorLog  /home/demo/public_html/domain1.com/logs/error.log
  CustomLog /home/demo/public_html/domain1.com/logs/access.log combined



Now we have the site available, we need to enable it:

sudo a2ensite domain1.com

The output of the command is:

Site domain1.com installed; run /etc/init.d/apache2 reload to enable.

Seems like good advice:

sudo /etc/init.d/apache2 reload


Now navigate to your site:


Tada! You now have the contents of public/index.html being shown:

Domain1 Home Page


Note that in the vhost file, we set a ServerAlias. Providing you have the DNS setup you can also use that address:


We'll talk about forcing one address or the other in a later article about rewrite rules.

Repeat as necessary

To create and enable domain2.com simply go through the process again:

sudo nano /etc/apache2/sites-available/domain2.com
# Enter the details for domain2.com as per the example shown above

Then enable the site and restart Apache:

sudo a2ensite domain2.com
sudo /etc/init.d/apache2 reload

Finally navigate to your second domain:



As defined in the vhosts file, each domain has its own log files:

ls /home/demo/public_html/domain1.com/logs/

The output is exactly as expected:

access.log  error.log

This makes for much easier analysis as each set of logs is self contained.


What if someone enters the IP address of the domains?

As we haven't defined a specific Virtual Host for the IP address, Apache will revert to the default Virtual Host.

In this case, we have not deleted or disabled the 000-default Virtual Host so it will display the 'It Works!' page (see here for the initial install and setup for Ubuntu Feisty).

If we had disabled the default, it would use the first vhost file (alphabetically), so the contents of domain1.com would be displayed.

This is something to keep in mind when planning your websites. Do you want a particular domain to be the default? Do you want the IP address to have completely different content?


As you can see, the basics of setting up virtual hosts is quite simple. The next article will look at some more advanced settings for the vhosts file.


Article Comments:

Mikel commented Sat Oct 25 00:01:42 UTC 2008:

Thanks a lot for this great help. I am new to Ubuntu and it was easy for me to understand your manual. But I am a little bit confused, because I had to add the defined hosts also to /etc/hosts, with your domain names it would be domain1.com domain2.com

bro commented Mon Mar 16 01:22:08 UTC 2009:

Great tutorial, thanks!

Jkosmos commented Wed Aug 26 10:13:44 UTC 2009:

I'm not sure who's is the credits, yours or google's that pointed me to you. Your article hits the core of the question-query "ubuntu virtualHosts". I was drifted for the last week all over the web, googling around with the wrong key words. Needless to mention my gratitude. Thank you φίλε!!

Craig Chapman commented Fri Jun 17 22:06:26 UTC 2011:

Great how-to. I did have one problem when following it. After setting up the vhosts I could only see my default website and none of the vhosted sites. Doing an "/etc/init.d/apache2 restart" I was presented with a warning. " [warn] NameVirtualHost *:80 has no VirtualHosts" It turns out that to resolve this you must alter your vhosts file from <VirtualHost *> to read <VirtualHost *:80>

Want to comment?

(not made public)


(use plain text or Markdown syntax)