Ubuntu LTS - 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 the examples 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}

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). Again, feel free to use whatever folder names you are comfortable with.

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
  LogLevel warn
  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 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 and restart Apache:

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

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 Apache directory contents or the Apache welcome page (depending on how you set up the default domain - see here for the initial Apache install and setup for Ubuntu LTS).

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:

Dewey Bushaw commented Mon Dec 03 18:37:57 UTC 2007:

Hey guys, I just wanted to point out that I was having trouble getting my sites to load. Basically I had 5 domains but each domain would show the first domain in my sites-enabled section. To solve this my buddy had me put in my httpd.conf file:

NameVirtualHost *

This solved my problem and now each site can access its Virtual Host file.

Clive commented Wed Aug 26 07:48:03 UTC 2009:

Pickled Onion. Your tuts are really useful, for which I thank you.

This one has me a little stumped, though. I configured 3 domains as you described and then went to look at them. I get:

RTSP/1.0 400 Bad Request Server: DSS/6.0.3 (Build/526.3; Platform/Linux; Release/Darwin Streaming Server; State/Development; ) Cseq: Connection: Close

I then deleted them and started again and am getting the exact same error.

Do you know what I might have done wrong?

Clive commented Wed Aug 26 09:52:27 UTC 2009:

The answer was staring me in the face. Sorry :)

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)