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, 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}

and

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

index.html

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:

<html>
  <head>
    <title>domain1.com</title>
  </head>
  <body>
    <h1>domain1.com</h1>
  </body>
</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).

vhosts

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
  ErrorLog  /home/demo/public_html/domain1.com/logs/error.log
  CustomLog /home/demo/public_html/domain1.com/logs/access.log combined

</VirtualHost>

a2ensite

Now we have the site available, we need to actually 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

Navigate

Now navigate to your site:

http://domain1.com

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

Domain1 Home Page

ServerAlias

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

http://www.domain1.com

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:

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

Finally navigate to your second domain:

http://domain2.com
or
http://www.domain2.com

Logs

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

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

Default

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 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?

Summary

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.

PickledOnion.

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 mysite.net ServerAlias mysite.net www.mysite.net

For whatever reason, www.mysite.net loads properly, yet mysite.net 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)

(optional)

(use plain text or Markdown syntax)