Howto Create Name Based and IP Based Virtual hosts in Apache

Sponsored Link
We have already seen the apache2 installation if you want to check here

Virtual Host refers to the practice of running more than one web site (such as www.company1.com and www.company2.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.

Basics Of Virtual Hosts

Using virtual hosts, host several domains with a single web server. In this way, save the costs and administration workload for separate servers for each domain. There are several options regarding virtual hosts

Name-based virtual hosts

IP-based virtual hosts

Operation of multiple instances of Apache on one machine

Name-Based Virtual Hosts

With name-based virtual hosts, one instance of Apache hosts several domains. You do not need to set up multiple IPs for a machine. This is the easiest, preferred alternative. Reasons against the use of name-based virtual hosts are covered in the Apache documentation.

Configure it directly by way of the configuration file /etc/apache2/ sites-available/filename(by default we have default name file take this as reference and create new file). To activate name-based virtual hosts, specify a suitable directive. NameVirtualHost *. * is sufficient to prompt Apache to accept all incoming requests. Subsequently, configure the individual hosts:

NameVirtualHost *
ServerName www.example.com
DocumentRoot /home/www/htdocs/example.com
ServerAdmin [email protected]
ErrorLog /var/log/apache2/www.example.com-error_log
CustomLog /var/log/apache2/www.example.com-access_log common

ServerName www.myothercompany.com
DocumentRoot /home/www/htdocs/myothercompany.com
ServerAdmin [email protected]
ErrorLog /var/log/apache2/www.myothercompany.com-error_log
CustomLog /var/log/apache2/www.myothercompany.com-access_log common

A VirtualHost entry must also be configured for the domain originally hosted on the server (www.example.com). In this example, the original domain and one additional domain (www.myothercompany.com) are hosted on the same server.

Just as in NameVirtualHost, a * is used in the VirtualHost directives. Apache uses the host field in the HTTP header to connect the request to the virtual host. The request is forwarded to the virtual host whose ServerName matches the host name specified in this field.

For the directives ErrorLog and CustomLog, the log files do not need to contain the domain name. Here, use a name of your choice.

ServerAdmin designates the e-mail address of the responsible person that can be contacted if problems arise. In the event of errors, Apache gives this address in the error messages it sends to clients.

IP-Based Virtual Hosts

This alternative requires the setup of multiple IPs for a machine. In this case, one instance of Apache hosts several domains, each of which is assigned a different IP. The following example shows how Apache can be configured to host the original IP (192.168.1.10) plus two additional domains on additional IPs (192.168.1.20 and 192.168.1.21). This particular example only works on an intranet, because IPs ranging from 192.168.0.0 to 192.168.255.0 are not routed on the Internet.

Configuring IP Aliasing

For Apache to host multiple IPs, the underlying machine must accept requests for multiple IPs. This is called multi-IP hosting. For this purpose, IP aliasing must be activated in the kernel.

Once the kernel has been configured for IP aliasing, the commands ifconfig and route can be used to set up additional IPs on the host. These commands must be executed as root. For the following example, it is assumed that the host already has its own IP, such as 192.168.1.10, which is assigned to the network device eth0.

Enter the command ifconfig to find out the IP of the host. Further IPs can be added with commands like the following:

/sbin/ifconfig eth0:0 192.168.1.20
/sbin/ifconfig eth0:1 192.168.1.21

All these IPs are assigned to the same physical network device (eth0).

Virtual Hosts with IPs

Once IP aliasing has been set up on the system or the host has been configured with several network cards, Apache can be configured. Specify a separate VirtualHost block for every virtual server:
ServerName www.myothercompany.com
DocumentRoot /home/www/htdocs/myothercompany.com
ServerAdmin [email protected]
ErrorLog /var/log/apache2/www.myothercompany.com-error_log
CustomLog /var/log/apache2/www.myothercompany.com-access_log common

ServerName www.anothercompany.com
DocumentRoot /home/www/htdocs/anothercompany.com
ServerAdmin [email protected]
ErrorLog /var/log/apache2/www.anothercompany.com-error_log
CustomLog /var/log/apache2/www.anothercompany.com-access_log common
VirtualHost directives are only specified for the additional domains. The original domain (www.example.com) is configured through its own settings (under DocumentRoot, etc.) outside the VirtualHost blocks.

Multiple Instances of Apache

With the above methods for providing virtual hosts, administrators of one domain can read the data of other domains. To segregate the individual domains, start several instances of Apache, each with its own settings for User, Group, and other directives in the configuration file.

In the configuration file, use the Listen directive to specify the IP handled by the respective Apache instance. For the above example, the directive for the first Apache instance would be:

Listen 192.168.1.10:80

For the other two instances

Listen 192.168.1.20:80
Listen 192.168.1.21:80

In Apache2 to configure virtual hosts there are two important files i.e

/etc/apache2/sites-available and /etc/apache2/sites-enabled/

We need to create symlink between these two folders for each virtual host or you can use a2ensite and a2dissite

sudo a2ensite

This Will create the correct symlinks in sites-enabled to allow the site configured in sitefilename to be served

sudo a2dissite

This Will remove the symlinks from sites-enabled so that the site configured in sitefilename will not be served

Example for name based virtual hosts

Apache 2.0, the default site is instead the first file (in alphabetical order) in the /etc/apache2/sites-enabled directory. After initial installation, there will be a symlink from 000-default in this directory to /etc/apache2/sites-available/default. As you can see from this, Apache 2.0 offers another level of abstraction in the Virtual Hosts by recommending putting the actual files in /etc/apache2/sites-available and then symlinking from there to /etc/apache2/sites-enabled. I would recommend following this convention, but it’s not mandatory. In our example above, we would create two files, /etc/apache2/sites-available/default and /etc/apache2/sites-available/example.com. Our /etc/apache2/sites-available/default file would look like this

NameVirtualHost *
ServerName incorrect.com
DocumentRoot /home/www/html/default

And our /etc/apache2/sites-available/example.com would look like this

NameVirtualHost *
ServerName www.example.com
ServerAlias example.com
DocumentRoot /home/www/html/example.com/html
CustomLog logs/www.example.com-access_log common

We would then create symlinks to the /etc/apache2/sites-enabled directory with the ln -s

sudo ln -s /etc/apache2/sites-available/example.com /etc/apache2/sites-enabled/example.com

or you can use the following command

sudo a2ensite example.com

Site www.example.com installed.

Now you need to restart the apache web server using the following command

sudo /etc/init.d/apache2 restart

Now we have our Virtual Hosts configured, it’s time to test. Fire up a browser and head to www.example.com

Example for IP based virtual hosts

/etc/apache2/sites-available/example.com would look like this:

ServerName www.example.com
ServerAlias example.com
DocumentRoot /home/www/html/example.com/html
CustomLog logs/www.example.com-access_log common

We would then create symlinks to the /etc/apache2/sites-enabled directory with the ln -s

sudo ln -s /etc/apache2/sites-available/example.com /etc/apache2/sites-enabled/example.com

or you can use the following command

sudo a2ensite example.com

Site www.example.com installed.

Now you need to restart the apache web server using the following command

sudo /etc/init.d/apache2 restart

Now we have our Virtual Hosts configured, it’s time to test.Fire up a browser and head to www.example.com to see if your web site is working or not.Before testing you need to make sure your DNS server was configured with correct A record.Create all the required directories and files for configuring websites.

Sponsored Link

You may also like...

8 Responses

  1. Frank says:

    LOL I’m going to use this to create honeypots.

  2. newmannn says:

    If you’re using guest os Linux with VMWARE and try to connect throw your windows because you are sure that you add in etc/hosts:

    192.168.0.1 mysite.com

    it will fail, you have to know that windows has also a etc/hosts file:
    “C:\WINDOWS\system32\drivers\etc”

    add there the same lines you added in your linux etc/hosts:
    192.168.0.1 mysite.com

    magic! ;D
    took me 3 days to find this out (-.-)

  3. dude for the dns set up do you mind posting an article on dns set after apche settings have been configured.

    just wondering.
    thanks.

  4. abdussamad says:

    Hello aren’t you supposed to put the virtual host entries inside containers?

  5. Sango says:

    This has been getting to me for hte past few months. I can do just about anything else with Linux except get virtual hosts to work. Can someone tell me what it is that I am doing wrong??

    NamevirtualHost *:80

    ServerAdmin [email protected]
    DocumentRoot /home/styehimba/public_html/
    ServerAdmin [email protected]
    DocumentRoot /home/styehimba/public_html/
    ServerAdmin [email protected]
    ErrorLog /var/log/apache2/www.tyehimbaenterprises.net-err_log
    CustomLog /var/log/apache2/www.tyehimbaenterprises.net-access_log common

    Options FollowSymLinks
    AllowOverride None

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ “/usr/share/doc/”

    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

  6. Homer Kendziora says:

    Nice post dude Thanks

  7. Joshua says:

    Hello,

    I have followed this tutorial, and I am getting a mysterious error. Requests for http://www.mydomain.com work fine, but requests for http://mydomain.com do not work properly. In Firefox, they redirect to http://www.mydomain.com, and in Chrome they result in an “Unable to contact server” message from the browser.

    What’s going on? I have created master DNS entries for all the websites I am hosting, but I have basically left the default settings Linode gives us alone.

    Thanks in advance!

    Joshua

  8. Nithin says:

    hey guys ,
    i need to do https virtual hosting can any one help
    thanks in advance!
    Nithin

Leave a Reply

Your email address will not be published. Required fields are marked *