David Stilson

  Linux System Administration

Virtual Web Hosting

Creating Virtual Web Hosts Using Apache

In the How-To on creating a web site we changed the Apache config file so it would point to the home directory. In this How-To we use Apache's default settings. To complete this How-To you will need to use Apache's default settings.

1) For this demonstration create 3 sites: site1.com, site2.com and site3.com (change the name of the site*.com to match your site). Create the directories for your new sites. By default, Apache in Ubuntu uses the /var/www directory. Open a terminal and type the following commands:

     cd /var/www

      mkdir site1

      mkdir site2

      mkdir site3

Verify that the directories were created with the ls command.

2) Copy the default 'sites available' file found in /etc/apache2/sites-available/default and rename it the same as your new sites. You may need become a sudo user to be granted the correct permissions.

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

      sudo cp/etc/apache2/sites-available/default /etc/apache2/sites-available/site2

      sudo cp/etc/apache2/sites-available/default /etc/apache2/sites-available/site3

3) Edit the new config files for each site using a text editor. Add the line ServerName server1 right below the ServerAdmin line. Change both DocumentRoot and Directory lines to point to your new sites. Do the same procedure for each new site you want to create.

    gedit /etc/apache2/sites-available/site1

Note: You need to modify the line containing AllowOverride None to read AllowOverride All. This tells Apache that it's okay to allow .htaccess files to over-ride previous directives. When a .htaccess file is placed in a directory it is loaded when the Apache server is loaded. .htaccess files can be used to alter the default configuration of the Apache Web Server software to enable/disable additional functionality and other features. This includes the ability to redirect the web page. For example, the web page can be redirected if a, 404 file not found error, occurs.

4) After you have edited the config files for each of the 3 new sites you need to tell Apache to start serving the new domains and stop serving the default. From a terminal window run the following commands:

      sudo a2ensite site1

      sudo a2ensite site2

      sudo a2ensite site3

5) Reload apache to make all of your changes effective. You should be able to see all of your new sites now.

     sudo /etc/init.d/apache2 restart

6) Verify that your sites work. Enter, localhost/site1, and the other sites in your browser.