Adding VirtualHosts to Apache2 on Ubuntu or Debian Print

  • 0

This help file assumes you have Apache2 setup and working with a default config (Fresh install is OK!).

In this lesson we will be using an example domain name ( Replace this example domain with your domain anytime you see it. The end of this document will explain how to add multiple hosts.

Giving Apache2 Its Power

  • Creating the File System Layout

The first step is to decide how you want to name your file root. Below is an example that will work just fine.

    cd /var/www
    mkdir -p vhosts/ (this is where you will put your html.)
    mkdir -p vhosts/ (this will be for your log files)
    chmod -R 755 vhosts/
  • Making A Default Page

You can put whatever you want in your document root but, to have a proper, functioning site, you will need either index.html or index.htm (or index.php!)

First, lets create an example index.html:

    cd vhosts/
    nano index.html

add the following into the file:

            Look at me. Setting up Apache2 like a Pro! 

OK. The setup is complete and we're ready to go ahead and add the domain to Apache.

  • Configuring NameVirtualHost

With virtual hosts, people often forget to configure NameVirtualHost.

For each port that Apache listens to, we need to define a NameVirtualHost. The issue that can catch people lies in the fact that you can only define it once per port.

In itself, that’s no problem but the default vhost has already defined a generic NameVirtualHost - adding another one will cause warnings and errors (and sometimes unexpected behavior).

The easiest thing to do is to remove the generic NameVirtualHost from the default vhost file and then add some specific ones to the main apache config file.

That way, if we ever delete or change the default vhost we don’t have to worry about where the NameVirtualHost setting is defined and so on.

OK. Let’s move into the apache directory and then open the default vhost:

    cd /etc/apache2/
    sudo nano sites-available/default

Delete the ‘NameVirtualHost *’ line and change the next ‘VirtualHost’ line so the file begins like this:

    <virtualhost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/

Once that is done, we can open the main apache2.conf:

    nano apache2.conf

Make sure the following line is in the file and not commented out:

    Include /etc/apache2/sites-enabled/

Also make sure the following is in the file:

    NameVirtualHost *:80

    <ifmodule mod_ssl.c>
        NameVirtualHost *:443

If the above code is not in apache2.conf, I found that it would cause errors when the second vhost was enabled.

Let’s reload Apache now:

    sudo /etc/init.d/apache2 reload

  • Making A Custom Virtual Host

We’ve set up the basic structure and content, now we're ready to add this configuration to our webserver.

Let’s go ahead and create the configuration file for

    nano /etc/apache2/sites-available/

The format of this file is very basic, you can view an example here:

    # Place any notes or comments you have here
    # Documenting changes helps track problems!
    # domain:
    # public: /var/www/vhosts/
    <virtualhost *:80>
      # Admin email, Server Name (domain name) and any aliases
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html
      DocumentRoot /var/www/vhosts/
      # Custom log file locations
      LogLevel warn
      ErrorLog  /var/www/vhosts/
      CustomLog /var/www/vhosts/ combined

  • Using a2ensite

Now we're pros at creating VirtualHost configurations, we need to enable it.

    sudo a2ensite

The result should be:

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

OK, this sounds good. The computer rarely lies to us so lets execute:

    /etc/init.d/apache2 reload

Now would be the time to cross your fingers.

Now navigate to your site:
  • Persevere!

To create and enable, simply go through the process again:

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

Then enable the site and restart Apache:

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


Wow, you really are a pro!

Was this answer helpful?

« Back