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




You forgot to enter the IP in the tag. Good tutorial
Thanks Mihai, will fix that tag now!
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
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