Complete Guide to Virtual Hosting with Apache

Complete Guide to Virtual Hosting with Apache

Virtual hosting allows you to host multiple websites or domains without having the need to create separate services for every domain. With this approach, you can manage your resources easily and efficiently. If you’ve been using Apache web server, you can set up three types of virtual hosts, which are:

IP-based virtual hosting

In this type of virtual hosting, each domain is assigned a different IP. They can be attached to a single NIC card or multiple NICs.

Name-based virtual hosting

In this type, Apache is configured to recognize the resources and servers using user hostnames. All of the domains use the same IP address. It’s a common hosting type adopted by web hosting companies.

Port-based virtual hosting

Port-based virtual hosting type relies of ports for serving the requested domain. Each domain is assigned to a particular port other than 80. While useful, this method of virtual hosting is not very popular because of the complexity involved while managing.

Setting up Virtual Hosting in Apache

To set up any of the virtual hosting, you need to follow a simple process. First navigate into /etc/httpd/conf/ and uncomment the following line:

Code:
Include /etc/httpd/conf. D/vhosts/*.conf
Then in the /etc/httpd/conf.d/ path, create a file and name it “filevhost.conf

Open the file and add the syntax depending on the type of virtual hosting you want to set up:
  • For IP-based virtual hosting
Code:
<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

Code:
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

Code:
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>
To verify the virtual configuration syntax, you need to execute the below code:

Code:
/etc/httpd/bin/httpd -S
You should get this output if everything is working as intended:

Virtual Host configuration:
Syntax OK

Or else, you’ll see a warning message.

Next, restart the httpd service by executing this command:

Code:
/etc/init.d/httpd restart
Redirecting URLs using MOD Alias

Apache configuration allows you to redirect one URL to another. For this, you have to open the Apache configuration file and edit the virtual host configuration section.

If the section reads something like this:

Code:
httpd-M |grepalias
alias_module (static)
The insert the following lines in httpd.confas:

Code:
<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>
You can also redirect URLs using MOD rewrite. You’d need to enable mod_rewrite. You can check if its enabled by executing the following command:

Code:
httpd-M |greprewrite
rewrite_module (static)
RewriteEngine On
RewriteRule ^/yourdomainold\.html$ yourdomainnew.html
If so, then it will redirect yourdomainold.html to yourdomainnew.html.

Finally, you can use Apache’s Directory aliasing for redirecting one directory to another. The following example will redirect /usr/local/email to /webmal

Alias /webmail /usr/local/mail
Author
kumkumsharma
Views
2,002
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top