Originally from: http://www.novell.com/coolsolutions/feature/19856.html

In this guide I tried to explain how to implement Virtual Hosting using Apache2 on SUSE Linux Enterprise Server 10. This guide is ESPECIALLY written for those WEB-ADMINS who are used to configuring Virtual Hosting on Redhat Enterprise Linux(RHEL 3/4). Since on a rhel Server, apache is configured using a single file i.e /etc/httpd/httpd.conf, i.e all of the virtual hosting configurations goes in /etc/httpd/httpd.conf ONLY, while in SUSE Linux Enterprise 10, we don’t edit the httpd.conf for virtual hosting.

What is Virtual Hosting:

Virtual Hosting is the art/method to run multiple websites on a single machine.

Benefits Of Virtual Hosting:

There are millions of websites on the Internet. Without Virtual Hosting it is impossible to run millions of website, because each website requires a dedicated Machine, IP and/or Port as an address to be accessed from across the world/Internet.

Types Of Virtual Hosting:

There are three types of virtual hosting.

  1. IP Based Virtual Hosting (not commonly used)
  2. Port Based Virtual Hosting (not commonly used)
  3. Name Based Virtual Hosting (commonly used)

a. IP Based Virtual Hosting:

    IP based virtual Hosting is the method to run multiple websites on a single machine, but each website must be configured to run on a different/unique IP. To configure the IP base virtual hosting we have to assign multiple IPs to the machine(server).

    Say we have a “Dell Precision 650” machine, to host 3 different websites we must assign 3 unique IP addresses to the machine, and then configure each website to listen on a dedicated IP.

    Advantage:

    Just a single physical machine will host multiple websites, otherwise we need a separate machine to host each website.

    Disadvantage:

    Requires a dedicated IP for each website.

b. Port Based Virtual Hosting:

    • To access the www.test.com, in the url user/surfer has to type the port too, as www.test.com:81
    • To access the www.wxyz.com, in the url user/surfer has to type the port too, as www.wxyz.com:82
    • To access the www.ijkl.com, in the url user/surfer has to type the port too, as www.test.com:83
  • Port base virtual Hosting is the method to run multiple websites on a single machine and even on a single/same IP, but each website must be configured to run on a different/unique Port.

    Say we have a “Dell Precision 650” machine, to host 3 different websites we just need a single dedicated/unique IP-add.

    e.g we have to host www.test.com, www.wxyz.com, and www.ijkl.comon a single machine using single/same IP-addr… we will configure www.test.comto listen on port 81, www.wxyz.comto listen on port 82, and www.ijkl.comto listen on port 83.

    Advantage:

    Just a single physical machine and IP will host multiple websites. No need to purchase multiple machines and IP-add to host multiple websites.

    Disadvantages:

    – It is quite impossible to inform the users/surfers that on which particular port the website is hosted.
    – And user/surfer must has to type the particular port in the url e.g

c. Name Based Virtual Hosting:

    To run multiple websites on a single machine and even on a single/same IP and port, we have to configure the Name-Based Virtual Hosting.

    Advantage:

    A single machine, single IP, and the default http port(80) is used to host several websites.

    Disadvantage:

    Proper DNS configuration will be required to host and access the websites hosted via Name Base Virtual Hosting.

    Note: Since other types of Virtual Hosting(IP and Port base) are not commonly used, thats why we just discuss the Name-Based virtual hosting and used the term “Virtual Hosting” for Name-Based Virtual Hosting.

Step-By-Step Configuration

Configuring Virtual-Hosting using apache2 on SLES 10

  1. First create the Directories and html files for the two websites we host using Virtual Hosting.say e.g
    a1, mkdir /srv/www/example1
    b1, echo "This is the EXAMPLE 1 website" >/srv/www/example1/index.html
    
    a2, mkdir /srv/www/example2
    b2, echo "This is the EXAMPLE 2 website" >/srv/www/example2/index.html
    ********

    After creating directories and html files for the two websites, Now we configure the apache2 to host/run the two websites using virtual hosting.

  2. in /etc/apache2/listen.conf append the following line:
    NameVirtualHost IP_OF_Server:PORT
    i.e
    
    NameVirtualHost 192.168.0.101:80
  3. then change the directory ascd /etc/apache2/vhosts.d

    cp vhost.template example1.conf
    cp vhost.template example2.conf

    NOTE: the virtual host configuration files(in our case example1.conf, and example2.conf) MUST have “.conf” in the end of their names.

  4. Have to change some required parameters as per the environment
    1. Minimal Required Configuration for example1 Websitein /etc/apache2/vhosts.d/example1.conf
      <VirtualHost 192.168.0.101:80> *
      
         ServerName www.example1.org
         DocumentRoot /srv/www/example1
      
         <Directory "/srv/www/example1"> **
            Order allow,deny
            Allow from all
         </Directory>
      
      </VirtualHost>
    2. Minimal Required Configuration for example2 Websitein /etc/apache2/vhosts.d/example2.conf
      <VirtualHost 192.168.0.101:80> 
      
         ServerName www.example2.org
         DocumentRoot /srv/www/example2
      
         <Directory "/srv/www/example2">
            Order allow,deny
            Allow from all
         </Directory>
      
      </VirtualHost>
  5. Restart Apache
    # /etc/init.d/apache2 restart
    or
    # rcapache2 restart

    Now access the websites via your favorite web-browser
    DONE 😉

* If the website is host on the default http port i.e port 80 then its optional to specify the port on rhel, but on a SLES, even the default http port(80) must have to be explicitly specify/define.

** On rhel the Directory Block( <Directory “dir_name”> …</Directory> ) declaration is optional, but on a SLES it is must required within Virtual host Configuration( <VirtualHost IP:Port>…</VirtualHost> ).