Virtual Hosting is a process in which we can host multiple sites on a single server. or we can also say single Apache to have multiple sites.
Types of Virtual Hosting
Virtual hosting is of two types-
1. Name Based
2. IP Based
Name Based Virtual Hosting
In Name Based virtual hosting the host name is provided by the client, so many host names points to a single IP address
For Example: There could be two domains
1- www.firstexample.com
2- www.secondexample.com
Both the above domains are mapped to the same IP address say "111.1.110.28".
Steps to create virtual hosts for Debian based Linux distributions
Step 1: First check for a default virtual host file on the below mentioned path
/etc/apache2/sites-enabled/000-default.conf
copy and paste 000-default.conf file for the number of domain names you want to create, (in our case we have two domains). Rename these file, as per Ubuntu file name should end with suffix ".conf"
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/firstexample.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/secondexample.com.conf
Now open the file in any editor
sudo nano /etc/apache2/sites-available/firstexample.com.conf
we configure the virtual host file as below
Note: request are taken on port 80 by default, we can also change this port to another for this make sure no other porcess is using this port.
Listen 80
<VirtualHost *:80>
ServerAdmin admin@firstexample.com (This will help server admin receive email on admin@firstexample.com)
ServerName firstexample.com
ServerAlias www.example.com
DocumentRoot /var/www/firstexample/public_html
# Other directives here
</VirtualHost>
Similarly for secondexample.com
sudo nano /etc/apache2/sites-available/secondexample.com.conf
<VirtualHost *:80>
ServerAdmin admin@secondexample.com (This will help server admin receive email on admin@secondexample.com)
ServerName secondexample.com
ServerAlias www.example.com
DocumentRoot /var/www/secondexample/public_html
# Other directives here
</VirtualHost>
Step 2: Now enable the above virtual files
sudo a2ensite firstexample.conf
sudo a2ensite secondexample.conf
Steps to create virtual hosts for CentOS based Linux distributions
Step1: Now check for a default virtual host file on the below mentioned path
/etc/httpd/conf
create the /etc/httpd/conf/vhosts directory
a.) cd /etc/httpd/conf
b.) mkdir vhosts
Creating a virtual host conf file firstexample.com.80.conf under directory vhosts
c.) cd vhosts
d.) cat > firstexample.com.80.conf (creating file).
e.) nano firstexample.com.80.conf (open firstexample.com.80.conf in terminal base editor nano).
Step2: Now type the following lines in your virtual host file firstexample.com.80.conf
Listen 80
<VirtualHost 111.1.110.28:80>
ServerAdmin admin@ firstexample.com
ServerName firstexample.com
ServerAlias www. firstexample.com
DocumentRoot /www/firstexample.com/html
# other directives
</VirtualHost
Save your changes (CTRL-O) and exit nano (CTRL-X).
Steps to follow for Debian and CentOS based operating system
the below steps have to follow for Debian and CentOS based operating system once when step 1 and step 2 are done.
---> Restart apache2
----> For testing the virtual host on our local machine, open a "host" file on below mentioned path
----> sudo nano /etc/hosts
----> add the following lines in file
127.0.0.1 localhost<br>
111.1.110.28 firstexample.com<br>
111.1.110.28 secondexample.com<br>
This directs any request for firstexample.com and secondexample.com from the clients computer and send the requests to the server at 111.1.110.28. Save the changes.
IP Based Virtual Hosting
In IP Based virtual hosting each host name is mapped to a unique IP address.
Therefore for virtual hosting here you have to make changes in 000-default.conf as below and rename the file with adding ".conf" to the name.
Listen 80
<VirtualHost 111.1.110.28>
ServerAdmin admin@firstexample.com (This will help server admin receive email on admin@firstexample.com)
ServerName firstexample.com
ServerAlias www.example.com
DocumentRoot /www/firstexample
</VirtualHost>
<VirtualHost 111.1.110.82>
ServerAdmin admin@secondexample.com (This will help server admin receive email on admin@secondexample.com)
ServerName secondexample.com
ServerAlias www.secondexample.com
DocumentRoot /www/secondexample
</VirtualHost>
0 Comment(s)