Ubuntu: How to set up an Apache Virtual Host (vhost) CodeUnit 28 SEP 2011

To set up a virtual host (vhost) under Apache on your Ubuntu server is relatively simple. First you need to create a config file under the /etc/apache2/sites-available directory. A good practice is to name this file the same as the folder you want to use to hold this virtual host’s web pages.

For this example we want to serve up traffic coming in for www.mydomain.com, so we create a file called mydomain:

sudo nano /etc/apache2/sites-available/mydomain

The contents of which looks like this:

<VirtualHost *:80>
DocumentRoot /home/www/mydomain
<Directory />
Options FollowSymLinks
AllowOverride None
<Directory /home/www/mydomain>
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
ServerName mydomain.com
ServerAlias www.mydomain.com

ErrorLog /var/log/apache2/mydomain-error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/mydomain-access.log combined

ErrorDocument 404 /error_documents/404.php
ErrorDocument 403 /error_documents/403.php


With the file saved, next we enable the site by running:

sudo a2ensite mydomain

Finally, reload the Apache configuration with:

sudo /etc/init.d/apache2 reload


(And if you are in the process of looking for a hosting server, you may want to check out different web hosting companies first to help you compare and find the most appropriate hosting account for you.)

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.