Configuring Apache for virtual hosting

  • What is Apache Virtual Hosting?

    I think the Apache developers say it the most succinctly:

    ApacheDevelopers:
    The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com) on a single machine.

    (lifted from: http://httpd.apache.org/docs/2.2/vhosts/)

    So basically it allows you to host more than 1 domain on the same server, which is awesome.
  • Even though you may be tempted to skip this section (and you can), I'm going to highly recommend that you take the time to setup virtual hosting on your new Droplet - you may find that down the road you need a "test domain" separate from your main domain name to run some things on. This will help you do that.

    I just personally find the virtual hosting setup to be a "cleaner" way of running Apache as it is clear to see what users own which domains.
  • Setting up your domain in virtual hosting

    The good news about using Ubuntu is that it is already setup for Virtual Hosting "out of the box" - all we need to do is create the virtual hosting config file that we will be using for our domain.

    So as the root user, navigate to the /etc/apache2 directory - when you do a long listing (ls -l) you should see the following:
  • If you see this you're in the right directory
  • You need to replace:

    [YOUR_EMAIL_ADDRESS] - put your email address here.

    [YOUR_DOMAIN] - replace this with your domain - in this case for me I would have "jamroom.net" everywhere this is shown.

    [USER_NAME] - replace this with the user that was created when we setup the droplet.

    Once you have the config file created, save the file and exit the editor (control-X in pico - say yes to save the file).

    Next up, we need to create the symbolic link that is going to "activate" our config file:

    a2ensite jamroom.net
    

    This will create a "link" in the directory (the same thing as a "shortcut" in windows) that points to the "jamroom.net" config file in the sites-available directory.

    Remember - if you named your file differently than you need to use the correct file name here!

    finally, let's restart Apache so our changes take effect:

    service apache2 restart
    

  • So how the config files for virtual hosting are setup for Apache on Ubuntu is that you have 2 directories:

    "sites-available" - this is the directory that contains the actual virtual hosting config files (we're going to create our own in just a minute).

    "sites-enabled" - inside this directory you will see symbolic links that point to config files located in the sites-available directory - i.e. if there is a symbolic link in this directory pointing to a config file, then that config file is active.

    So change directory into the sites-available directory:

    cd sites-available
    

    and inside you should see 2 files - "default" and "default-ssl". We;'re going to create a new one. So using an editor - we will use "pico" here since it is easier to understand for most new users compared to vi, but by all means use vi if you can :) - we're going to create a new file for our domain.

    For our testing here I'm going to be using the domain "www.jamroom.net" - you should use your domain instead:

    pico jamroom.net
    

    This will drop you into the code editor. You will want to add the following to this file:
    <VirtualHost *:80>
    
        ServerAdmin [YOUR_EMAIL_ADDRESS]
        ServerName [YOUR_DOMAIN]
        ServerAlias www.[YOUR_DOMAIN]
    
        RMode stat
    
        DirectoryIndex index.php index.html index.htm
        DocumentRoot /home/[USER_NAME]/public_html
        <Directory /home/[USER_NAME]/public_html/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            allow from all
        </Directory>
    
        ErrorLog /home/[USER_NAME]/logs/error.log
        LogLevel error
    
    </VirtualHost>
  • Instead of "restart" you can use "graceful" - that tells Apache to "restart your processes, but finish serving any requests before reloading" - i.e. this is a nice way to reload Apache without disconnecting your current users.

Tags