linux poison RSS
linux poison Email

Adding Virtual Hosts in Apache2 under Ubuntu

The term Virtual Host refers to the practice of running more than one web site (such as www.foo.com and www.goo.com) on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

Apache was one of the first servers to support IP-based virtual hosts right out of the box. Versions 1.1 and later of Apache support both IP-based and name-based virtual hosts (vhosts). The latter variant of virtual hosts is sometimes also called host-based or non-IP virtual hosts.

Following are the simple steps for creating/adding virtual hosts in Apache2 under Ubuntu:

Steps #1:
First create the directory structure for holding the webpages and logs for your virtual hosts:
sudo mkdir /var/www/foo
sudo mkdir /var/www/goo
sudo mkdir /var/log/apache2/foo
sudo mkdir /var/log/apache2/goo
Steps #2:Add a proper host entry for site foo and site goo into your /etc/hosts file: vi /etc/hosts
192.168.1.1    foo
192.168.1.1    goo
Steps #3:
Now, go and create the vhosts file for site foo and goo: vi /etc/apache2/sites-available/foo and add the required contents as shown below:
<VirtualHost *:80>
        ServerAdmin foo-webmaster@localhost
        ServerName foo
        DocumentRoot /var/www/foo
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/foo/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog /var/log/apache2/foo/error.log
        LogLevel warn
        CustomLog /var/log/apache2/foo/access.log combined
</VirtualHost>

Similarly  create the /etc/apache2/sites-available/foo file for goo site

Steps #4:
Type following command to enable site foo and goo:
sudo a2ensite foo
sudo a2ensite goo
After this, restart/reload your apache server using command
sudo service apache reload
Finally, after successful apache reload/restart you should able to navigate to site http://foo and http://goo using any browser.




2 comments:

Biapy said...

Hi,

For even simpler Vhost management, i've written a little script named "a2tools". It is basicaly a Vhost file templater.

For example, the code you give in your article is basically this command line with a2tools:

a2tools "foo" "/var/www/foo"

More info is available at :

http://howto.biapy.com/en/debian-gnu-linux/servers/http/install-and-setup-apache-2-on-debian

Thank you for your work.
Pierre-Yves Landuré
Biapy

Unknown said...

Basically,virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

Post a Comment

Related Posts with Thumbnails