Apache Virtual Host and Setting it up on Ubuntu Linux
The term 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 an IP address for each web site, or “name-based“, meaning that you have more than one web site per IP address. The fact that they are running on the same physical server is not visible to the end user.
Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless you are using equipment that explicitly demands IP-based hosting.
Using Name-based Virtual Hosts
To use name-based virtual hosting, you must designate the IP address (and possibly port) on the server that will be accepting requests for the hosts. This is configured using the NameVirtualHost directive. In the normal case where any and all IP addresses on the server should be used, you can use * as the argument to NameVirtualHost.
The next step is to create a
block for each different host that you would like to serve. The argument to the VirtualHost directive must match a defined NameVirtualHost directive. (In this usual case, this will be “*:80“). Inside each VirtualHost block, you will need at minimum a ServerName directive to designate which host is served and a DocumentRoot directive to show where in the filesystem the content for that host lives.
For example, suppose that you are serving the domain www.domain.tld and you wish to add the virtual host www.otherdomain.tld, which points at the same IP address. Then you simply add the following to httpd.conf:
NameVirtualHost *:80 <VirtualHost *:80> ServerName www.domain.tld ServerAlias domain.tld *.domain.tld DocumentRoot /www/domain </VirtualHost> <VirtualHost *:80> ServerName www.otherdomain.tld DocumentRoot /www/otherdomain </VirtualHost>
More about the directives
NameVirtualHost Directive designates an IP address for name-virtual hosting. The NameVirtualHost directive is a required directive if you want to configure name-based virtual hosts.
are used to enclose a group of directives that will apply only to a particular virtual host.
The ServerName directive sets the request scheme, hostname and port that the server uses to identify itself. This is used when creating redirection URLs.
The ServerAlias directive sets the alternate names for a host used when matching requests to name-virtual hosts.
The DocumentRoot directive sets the directory that forms the main document tree visible from the web. Example:
then an access to http://www.my.host.com/index.html refers to /usr/web/index.html. If the directory-path is not absolute then it is assumed to be relative to the ServerRoot.
The DocumentRoot should be specified without a trailing slash.
In my local machine:-
– I am using Ubuntu Linux Operating System.
– I have already setup Apache, PHP and MySQL in my machine.
– My documentroot directory is /var/www
– I have a directory named ‘test’ inside ‘www’ directory
– I want to create virtual host for this ‘test’ directory
Here is how to do it:-
– Create a plain text document named ‘test‘ in /etc/apache2/sites-available
– Write the following in it.
NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot /var/www/test ServerName dev.test </VirtualHost>
– Change directory to sites-enabled with the following command in terminal
– Create Symbolic link (note that we had created new file named ‘test’ in sites-available directory)
sudo a2ensite test
The /etc/apache2/sites-available directory is not parsed by Apache2. Symbolic links are present in /etc/apache2/sites-enabled directory. They point to “available” sites.
‘a2ensite‘ (Apache2 Enable Site) utility is used to create those symbolic links.
Note: To disable sites, ‘a2dissite’ utility is used.
– Open /etc/hosts and add the new host names at the end of the line beginning by 127.0.0.1, which is localhost. So, the line will be like:-
127.0.0.1 localhost.localdomain localhost dev.test
– Restart Apache
sudo service apache2 restart
– Now, when you browse http://dev.test in your browser, you will be directed to index file present inside /var/www/test directory.
References & Further Reading:-