Hello friends. Today I am going to tell you about configuring Virtual Hosts in Apache. Firstly, you should know that what are Virtual Hosts? If not, below is definition from Wikipedia:
Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. This allows one machine to share its resources, such as memory and processor cycles, to use its resources more efficiently.
In simple words, If you want to host multiple websites (domains) on a single server then Virtual Hosting is what you need !!. Lets have some discussion about Virtual hosts, if you don’t like theory you can skip directly to configuration part.
Types of Virtual Hosts:
Virtual hosts can be of two types:
- Name-based Virtual Hosts
- IP based Virtual Hosts.
Name-based Virtual Hosts:
Name-based virtual hosts use multiple host names for the same webserver IP address. Using this method you can host multiple domains (different host names), even if your server has only one Network Interface Card (NIC) i.e one IP. Web server is configured to provide different website based on host name requested by user. If the user requests directly using IP address of server, then default website is provided.
IP-based Virtual Hosts:
In IP-based Virtual Hosts, each site points to a unique IP address. IP-based Virtual Hosting is useful if your server has more than one NIC /IP’s. In that case you can configure your web server to host different site based on the IP address for which request is coming. This type is Virtual hosting is very rarely used.
Name-based Virtual Hosts Configuration:
Here, I am explaining configuration for Apache 2.x. If you are not using Apache as web server then rest of the post is not useful to you. To configure Name-based Virtual Hosts open your server’s httpd.conf file. Search and uncomment the following line is not already uncommented.
If you are unable to find above line in your configuration then it may be possible that Virtual Hosts settings are defined in separate file. Take a look at include directives defined in the httpd.conf file and try to find the right file. For example- In my case, I found the following line in my httpd.conf file:
Add or edit rest of configuration at appropriate place according to your httpd.conf. Note that Virtual Hosts in Ubuntu are handled differently, if you install Apache using Ubuntu repositories. I will explain configuration for Ubuntu later in this topic.
For default website, place its contents under Document Root of your web server. You do not need to configure Virtual host for default website. For websites other that default, enter the following lines in appropriate file.
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/var/www/drupal" ServerName example.com ErrorLog "logs/example.com-error_log" CustomLog "logs/example.com-access_log" common </VirtualHost>
Here, I am using example.com as host name. Change it to your host name. Also, change the value of DocumentRoot to the path of your Virtual Host web directory i.e directory which you want to server when request comes from the name defined as ServerName. Restart Apache for the configuration to take effect.
IP-based Virtual Hosts Configuration:
For IP-based Virtual Hosts, add following lines in the appropriate file (see above section for selecting file).
<VirtualHost example1.com> ServerAdmin email@example.com DocumentRoot "/var/www/example1" ServerName example1.com ErrorLog "logs/example1.com-error_log" CustomLog "logs/example1.com-access_log" common </Virtualhost>
Do not forget to replace host name and DocumentRoot with your own host name. You can also use IP address in <VirtualHost > directive here. Then restart Apache.
Virtual Hosts configuration in Ubuntu:
If you are using Ubuntu Linux distribution and Apache is installed using Ubuntu repositories. Then to configure Virtual Hosts you have to follow following steps:
- Open /etc/apache2/apache2.conf file. Be sure that following line is uncommented under Virtual Hosts section.
- Then create a separate file for each Virtual Host you want to configure in the /etc/apache2/sites-enabled/ folder.
- Write Virtual Host configurations for each Virtual Hosts in their respective files. Configuration is same as discussed in above sections.
- Restart your webserver. Following command can do it.
$sudo /etc/init.d/apache2 restart