Creating virtual hosts on Ubuntu

All virtual host for apache2 are saved in directory

/etc/apache2/sites-enabled/

and apache will automatically load all files in it

Create virtual host at your host file for ubuntu

sudo gedit /etc/hosts

Add new line, for example

127.0.0.1	mydomain.local

Create virtual host at your host file for apache

sudo gedit /etc/apache2/sites-enabled/001-mydomain

Add directives


	ServerAdmin webmaster@localhost
	ServerName mydomain.local
	DocumentRoot /home/user/www/public/

Restart apache

sudo /etc/init.d/apache2 restart

Check if your new domain is working at http://mydomain.local/


  1. lukoko says:

    Hello Andriau,
    As you stated here brief setup for adding a virtual host on ubuntu, and I believe it works fine, but i still want to notice that there is directory sites-available in /etc/apache2 – and its main purpose is to contain all vhost configs. so, instead to put my vhost config file directly to sites-enabled, i would rather put it to sites-available, say i have file /etc/apache2/sites-available/areal.dev (it may contain the same your /etc/apache2/sites-enabled/001-mydomain contains).
    and then i execute
    $ sudo a2ensite areal.dev

    this command will create symlink in sites-enabled pointing to sites-available/areal.dev, and you stil get subdomain working as its config were put to sites-enabled.

    Thats not a big deal, of course, but doing this way you can switch your vhosts on and off with commands a2ensite/a2dissite. And directory names has more sense in this setup,- thats the Ubuntu way ;-)

  2. Andrius says:

    Thanks for explanation ;) I will remember that.

Leave a Reply