Debian Etch - Apache Virtual Hosts

Now we have Apache installed and running, let's configure it to serve two different 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 (in this case, and create a folder with a standard set of sub-folders:

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


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

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


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/

The content index.html:


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


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/

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:
# public: /home/demo/public_html/

<VirtualHost *>

  # Admin email, Server Name (domain name) and any aliases

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

  # Custom log file locations
  ErrorLog  /home/demo/public_html/
  CustomLog /home/demo/public_html/ combined



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

sudo a2ensite

The output of the command is:

Site 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 simply go through the process again:

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

Then enable the site:

sudo a2ensite
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 public_html/

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.

If we had disabled the default, it would use the first vhost file (alphabetically), so the contents of 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:

Jon commented Tue Feb 26 23:30:10 UTC 2008:

PickledOnion... I'd love to see that article on apache rewrite rules. These articles are brilliant! - precise, clear and straight to the point.

Cheers mate!

stephen commented Sat Mar 22 17:42:46 UTC 2008:

Good article! Thanks

david commented Mon Jul 07 07:53:06 UTC 2008:

I'm running into a bizarre problem.

I've got a virtual host set up like so:

ServerName ServerAlias

For whatever reason, loads properly, yet loads the default settings.

Any idea what might be causing this?

I'm running Debian Etch & Apache 2

david commented Mon Jul 07 08:27:00 UTC 2008:

Interestingly enough, I changed my VirtualHost from *:80 to myip:80 and now the problem is fixed.

I don't know that that was the real issue as I may've inadvertently fixed some other problem with my configuration in the meantime, but I thought I'd pass it along...

Chris commented Fri Jul 18 20:35:52 UTC 2008:

Thank you so much for your help. These articles are concise and accurate.

oscar commented Thu Sep 27 06:54:25 UTC 2012:

Excellent article!!

Want to comment?

(not made public)


(use plain text or Markdown syntax)