HOWTO: Set up Apache Virtual Hosts on Ubuntu Gutsy Gibbon

Assuming you have a fresh installation of apache2.

First we need to tell apache we will be using name based virtual hosts.

Edit the apache2.conf file:
sudo vi /etc/apache2/apache2.conf
Add the following line, replacing xxx.xxx.xxx.xxx with your IP address.
NameVirtualHost xxx.xxx.xxx.xxx:80
Now we can start adding our Virtual Hosts. Let’s say you want to set up a virtual host for the domain myfunkysite.com.

Create the folder where you will be hosting your site together with a test page.
sudo mkdir /var/www/myfunkysite.com
sudo mkdir /var/www/myfunkysite.com/httpdocs
sudo echo "My Funky Site is Working" > /var/www/myfunkysite.com/httpdocs/index.html
sudo chown -R www-data:www-data /var/www/myfunkysite.com/httpdocs
Now we create a configuration file to tell apache all about out new site. If you have a look under /etc/apache2/ you will see 2 folders - sites-available and sites-enabled. We will configure our host under sites-available.
cd etc/apache2/sites-available
ls

You will see there is a file called ‘default’. This is the out of the box apache2 default site configuration. Let’s create a new file for our site.

sudo vi myfunkysite.com

Add the following to the file replacing xxx.xxx.xxx.xxx with your external IP address.

<VirtualHost xxx.xxx.xxx.xxx>
ServerName myfunkysite.com
ServerAlias www.myfunkysite.com
DocumentRoot /var/www/myfunkysite.com/httpdocs
</VirtualHost>

Ok - now we remove the default site entry and add our new virtual host.
sudo a2dissite default
sudo a2ensite myfunkysite.com
sudo /etc/init.d/apache2 reload

That’s it! Set up your DNS for the domain to point to your server IP and you should see the new site. If you want to test before your new dns has propogated then edit your hosts file:

vi /etc/hosts

Add the line below replacing xxx.xxx.xxx.xxx with your IP address.

xxx.xxx.xxx.xxx www.myfunkysite.com

Now open a browser (or wget) www.myfunkysite.com and you should see something like this.

To add additional sites simply create the website folders (in /var/www) and matching config files (in /etc/apache2/sites-available) and then for each site run:

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

5 Responses to “HOWTO: Set up Apache Virtual Hosts on Ubuntu Gutsy Gibbon”


  1. 1 mihai Feb 1st, 2008 at 4:34 pm

    You forgot to enter the IP in the tag. Good tutorial :)

  2. 2 daryl Feb 3rd, 2008 at 4:55 am

    Thanks Mihai, will fix that tag now!

  3. 3 Andreas Feb 28th, 2008 at 12:35 pm

    Would be great if you described what everything did too. I know I could look everything up in the apache documentation but it would be nice to have everything on one place!

    For example this line.
    NameVirtualHost xxx.xxx.xxx.xxx:80

    Thanks for the great guide though. Helped me get started :D

  4. 4 James Nov 25th, 2008 at 2:27 am

    G8 how to…
    i have used this for my server but an having issues with how to setup the dns on my domain suppliers website, do i have to setup dns on the the domain suppliers control panel of do i have to setup my ubuntu server to be a name server. Any help would be great.

    J

  1. 1 Using Apache and Ubuntu for local websites with SSL « Thoughts by Techxplorer Pingback on Apr 27th, 2009 at 2:09 am

Leave a Reply






Add to Technorati Favorites
Zen Handbook

Latest Posts

RSS