Virtual Hosting with Apache
Virtual hosting is the practice to share resources for hosting multiple domains without creating separate services for each. Virtual hosting helps to use resources more effectively.
There are three types of virtual hosting
1.IP based virtual hosting
In this type of virtual hosting different IPs areusingto host domains.IP based virtual hosting needs different IP for each domainhostedinit.Hosting server may add the different IP addresses to same interface or add different network cards.
2.Name based virtual hosting
Name based virtual hosting webserver uses host names to identify the resources requested (In HTTP Header) and servers based on hostname.Over IP based virtual hosting, name based virtual hosting has an advantage,it doesn’t need multiple IPs or interface cards.Name based virtual hosting is normally referred as “Shared Hosting “ by hosting providers.
3.Port based virtual hosting
It is also possible to use different ports (ie,ports other the 80) to serve the different resources. But this type is not user friendly and not a common practice for normal web hosting.
Virtual Hosting with Apache
Apache supports all of this virtual hosting techniques. Let’s go to some of the Apache virtual host configurations. Uncomment the following line in /etc/httpd/conf/httpd.conf
Include /etc/httpd/conf. d/vhosts/*. conf Then create filevhost. conf in /etc/httpd/conf.d/ . Open that file and append the following configurations for each type. The Apache directive used this configuration is explained below.
- <VirtualHost > < VirtualHost /> means VirtualHost configuration.
- ServerAdmin: Administrator email ID
- DocumentRoot: Documentrootforthesite
- ServerName: Domain name
- ServerAlias: Domain Alias
- ErrorLog: Error log
- IP based virtual hosting
<VirtualHost 1.2.3.4>
ServerAdmin admin@yourdomain.com
DocumentRoot “/var/www/html/example_1”
ServerNameexample1.com
ServerAlias www. example1.com
ErrorLog “logs example1/error_log”
CustomLog “logs/example1/access_log” common
</VirtualHost>
<VirtualHost 1.2.3.5>
ServerAdmin admin@yourdomain.com
DocumentRoot “/var/www/html/example_2”
ServerNameexample2.com
ServerAlias www. example1.com
ErrorLog “logs/ example2/error_log”
CustomLog “logs/ example2/access_log” common
</VirtualHost>
- Name Based Virtual hosting
NameVirtualHost *:80
<VirtualHost*:80>
ServerAdmin admin@yourdomain.com
Document Root “/var/www/html/example_1”
ServerNameexample1.com
ServerAlias www. example1.com
ErrorLog “logs example1/error_log”
CustomLog “logs/example1/access_log” common
</VirtualHost>
<VirtualHost*:80>
ServerAdmin admin@yourdomain.com
DocumentRoot “/var/www/html/example_2”
ServerNameexample2.com
ServerAlias www. example2.com
ErrorLog “logs/ example2/error_log”
CustomLog “logs/ example2/access_log” common
</VirtualHost>
- Port based Virtual hosting
Listen 80
Listen 8080
NameVirtualHost 1.2.3.4:80
NameVirtualHost 1.2.3.4:8080
<VirtualHost172.20.30.40:80>
ServerName www.example.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost172.20.30.40:8080>
ServerName www.example.com
DocumentRoot /www/domain-8080
</VirtualHost>
You can verify the virtual configuration syntax by executing the following command. If everything is set up properly, it will display the following output.
/etc/httpd/bin/httpd -S Virtual Host configuration: Syntax OK
If something is not configured properly, it will display warning messages.
At last, restart the httpd service in order to reflect the recent changes that you have madeinhttpdconfigurations
/etc/init.d/httpd restart
Redirection Using mod_alias
Redirect one URL to another with the help of Apache configuration. Edit the virtual host configuration section of Apache configuration file. The following example redirecttestdomain.com to www.testdomain.com .For using this, make sure that the mod_alias enabled in Apache configurations. You can check if mod_alias is enabled by using the following command:
httpd-M |grepalias
alias_module (static)
Then editthehttpd.confas follows:
<VirtualHost*:80>
ServerName web1.testdomain.com
ServerAlias testdomain.com we.testdomain.com
Redirect / http://www.testdomain.com/
</VirtualHost>
<VirtualHost*:80>
ServerName www.testdomain.com
</VirtualHost>
Redirecting using mod_rewrite
Enable mod_rewrite in Apache configuration .The following example redirects a your domainold.html to your domainnew.html
You can check if mod_rewrite is enabled by using the following command:
httpd-M |greprewrite
rewrite_module (static)
RewriteEngine On
RewriteRule ^/yourdomainold\.html$ yourdomainnew.html
Using Directory Aliasing
Apache Directory aliasing use to redirect one directory to another one. The following example shows how to directory alias /usr/local/mail to /webmail
Alias /webmail /usr/local/mail
Leave A Comment
You must be logged in to post a comment.