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 (domain.com). 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/domain.com/htdocs (this is where you will put your html.)
    mkdir -p vhosts/domain.com/log (this will be for your log files)
    chmod -R 755 vhosts/domain.com
  • 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/domain.com/htdocs
    nano index.html

add the following into the file:

    <html>
      <head>
        <title>domain.com</title>
      </head>
      <body>
            Look at me. Setting up Apache2 like a Pro! 
      </body>
    </html>

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

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

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 domain.com:

    nano /etc/apache2/sites-available/domain.com

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: domain1.com
    # public: /var/www/vhosts/domain.com/domain.com/
     
    <virtualhost *:80>
     
      # Admin email, Server Name (domain name) and any aliases
      ServerAdmin webmaster@domain.com
      ServerName  domain.com
      ServerAlias www.domain.com
     
     
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html
      DocumentRoot /var/www/vhosts/domain.com/htdocs
     
     
      # Custom log file locations
      LogLevel warn
      ErrorLog  /var/www/vhosts/domain.com/log/error.log
      CustomLog /var/www/vhosts/domain.com/log/access.log combined
     
    </virtualhost>

  • Using a2ensite

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

    sudo a2ensite domain.com

The result should be:

    Site domain.com 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:

    http://domain.com
  • Persevere!

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

    nano /etc/apache2/sites-available/domain2.com
    ...
  1. 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

 

Wow, you really are a pro!


Was this answer helpful?

« Back