Virtual Hosts on Apache R R


Virtual Hosts on Apache

You will love this one! I will start by giving you sample url's that you can compare, then we will talk about what virtual hosts really mean.
http://www.sabyo.com/finance/
http://www.sabyo.com/admin/
http://www.sabyo.com/personnel/

http://finance.sabyo.com/
http://admin.sabyo.com/
http://personnel.sabyo.com/


Let's say our company is called sabyo.com, and it has three departments: finance, admin, and personnel. Sabyo.com has a separate folder for each department on the webserver. All web files for admin go into the admin folder, those for finance go into the finance folder, and personnel files go into personnel folder. Since all three folders are running under the same domain name, each department is identified by appending folder names to the domain name http://www.sabyo.com. So what we get for admin is something like http://www.sabyo.com/admin/.

But what if you don't want to append the folder name to the domain name, but instead you want the url to be something like http://admin.sabyo.com/? You can do this by setting up virtual hosts for each of the departments at http://www.sabyo.com/.

Better still, what if you have clients or friends who want you to host their websites for them on your server, but they don't want to use your domain name http://www.sabyo.com/ as part of their url, instead they have their own registered domain names?

Assuming we have two clients, one has the domain name http://www.clientone.com/, and the other http://www.clientwo.com/. You will create folders for each client on your server, just as you did for the three departments (finance, admin, and personnel) at http://www.sabyo.com. Next you will setup virtual hosts for each of the clients with their own domain name.

Let's say you normally serve your webpages out of the folder /www/html/, when you create folders for the two clients, you will get /www/html/clientone, and /www/html/clientwo (clientone and clientwo are just arbitrary names I've chosen). Also, if we add your own three departments, you will get /www/html/finance, /www/html/admin, /www/html/personnel.

Once you have the virtual hosts set up, any requests for http://admin.sabyo.com/ will be served from the folder /www/html/admin, any requests for http://www.clientone.com/ will be served from /www/html/clientone, and so forth. I hope all this is making sense to you.

Because all you need to create a new section on your website is to create a new folder, you can create an unlimited number of folders, each one with its own domain name, made possible by virtual hosting. So another way of looking at virtual hosts is to say that it enables you to run unlimited number of servers on one machine, even though you're actually running just a single copy of the server.

Now that you have an appreciation for what you can accomplish by configuring virtual hosts, let's look at the actual process of creating virtual hosts on Apache server. I must say that since virtual hosting is extremely useful to a server administrator, the configuration must be very difficult, right? Wrong! Configuring virtual hosts in Apache is incredibly easy. All you need to do is add some lines to httpd.conf (the configuration file for apache server) for every virtual host you're creating.

Let's see how to do it for our examples above.

Using Notepad or any other text editor, open httpd.conf, search for NameVirtualHost. Under that add the following


NameVirtualHost *

<VirtualHost *>
ServerName www.admin.sabyo.com/
DocumentRoot /www/html/admin
</VirtualHost>

Change ServerName to match yours. Do the same with DocumentRoot. That's it! The above five lines is all you need to set up a name based virtual host.

The first line is a directive, NameVirtualHost *. This is the line that tells apache that you're going to define name based virtual hosts. You will notice that I've used an asterik * in front of this directive. That defines the IP address on which the webserver will service clients. If you have a static ip, you may want to use it instead of an asterik.

So for example, if you have the static ip address 11.19.23.4, the virtual host definition will be


NameVirtualHost 11.19.23.4

<VirtualHost 11.19.23.4>
ServerName www.admin.sabyo.com/
DocumentRoot /www/html/admin
</VirtualHost>

If you're running Apache on a dynamic IP, leave it as NameVirtualHost *. That way, apache will use any available ip address on your machine for serving your webpages.

Anytime there's a request for http://admin.sabyo.com/, your apache server will only access files under the directory /www/html/admin.

Now, to add more virtual hosts, all you need to do is leave out the first line, ie. NameVirtualHost *, and define the remaining lines. Let's add personnel.


NameVirtualHost *

<VirtualHost *>
ServerName www.admin.sabyo.com/
DocumentRoot /www/html/admin
</VirtualHost>

<VirtualHost *>
ServerName www.personnel.sabyo.com/
DocumentRoot /www/html/personnel
</VirtualHost>

Our virtual host definition for all five will be


NameVirtualHost *

<VirtualHost *>
ServerName www.admin.sabyo.com/
DocumentRoot /www/html/admin
</VirtualHost>

<VirtualHost *>
ServerName www.personnel.sabyo.com/
DocumentRoot /www/html/personnel
</VirtualHost>

<VirtualHost *>
ServerName www.finance.sabyo.com/
DocumentRoot /www/html/finance
</VirtualHost>

<VirtualHost *>
ServerName www.clientone.com/
DocumentRoot /www/html/clientone
</VirtualHost>

<VirtualHost *>
ServerName www.clientwo.com/
DocumentRoot /www/html/clientwo
</VirtualHost>

All you need to do now to make your virtual hosts work is to
  • save httd.conf.
  • Shutdown and restart your apache server. This forces apache to read httpd.conf again to reflect the changes you made.

Within each virtual host, you can also have sub levels. Eg. you can have something like

http://records.admin.sabyo.com/

http://benefits.records.admin.sabyo.com/, etc.





Send me a feedback on this tutorial, so I can improve on it. Use the form below. Thanks.
  
Your Email If you want a reply           
 

Comments or question

Home




Copyright © 2000 Richie's Tutorials All rights reserved.