Apache Virtual Hosts in Ubuntu

An Apache virtual host, as explained by Apache, refers to the practice of running more than one web site (such as http://www.company1.com and http://www.company2.com) on a single machine. Virtual hosts can be “IP-based” or “name-based”. By default, Ubuntu already has this capability enabled, so things are much easier to configure.

Steps to create virtual hosts:

  • Create the folders that will host your new sites. By default, Apache in Ubuntu serves from /var/www. So create these:

mkdir /var/www/site1

  • Copy the current default setting found in /etc/apache2/sites-available/default and name it the same as your new site. 

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1

  • Edit the new config file for site1 using your preferred text editor. Add the line ServerName server1 right below the ServerAdmin line and change both DocumentRoot and Directory to point to your new sites.

    vi /etc/apache2/sites-available/site1

  • It will look as:

  • In hosts file , add one line 127.0.0.1 site1

sudo vi /etc/hosts

Add line:

127.0.0.1  site1

  • After you have edited the config files for site1, just tell Apache to start serving the new domain and stop serving the default:

sudo a2ensite site1

  • Now reload apache and you should be able to get your new domain:

sudo /etc/init.d/apache2 reload

  • Now type in terminal:

localhost/site1

  • Congratulations, you have done it.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Apache Virtual Hosts in Ubuntu

  1. Very soon this site will be famous amid all blogging users,
    due to it’s good articles

  2. Daouane says:

    great and educative content, thanks for being so important for the education.http://www.mybigg.com

Leave a comment