Your Ad Here
 

Reply to comment

devans's picture

Apache Name Based Virtual Hosting - How To

Your rating: None Average: 5 (1 vote)

Over the years I have had many people ask about how to do Virtual Hosting.

Despite many demonstrations, much coaching and plenty of examples, it seems that where I work at least, nobody can grasp it or as I suspect maybe they simply don't want to! I find this a little amazing considering how simple it really is.

Anyway, what I am going to show today is a basic how-to on Virtual Hosting for the 1.x and 2.x Apache Web Server. The configuration for both these variants is controlled almost exclusively by a single file, httpd.conf. Version 2.2.x varies in that they have exploded the configuration into several files that refer back to the httpd.conf. I will be explaining more on the Apache 2.2.x virtual hosting configuration for this site specifically in the near future as I continue my series regarding the reconfiguration of Poscribes.com along with the integration of load-balancing and replication also. But enough of that for now, let's get stuck in and cover the basic elements necessary for configuring Apache Virtual Hosting.

So what is virtual hosting? Quite simply it is the ability through a chosen method of typically either a name or address (IP) based configuration that allows hosting multiple websites on the same web server. Doesn't sound like it should be too difficult does it? Well, it really isn't but it can be a little confusing at first.

At this point, I am of course assuming that you have a working, basic install of Apache and that your website is functioning. To test, simply click the following link:

http://localhost

If you can see the default Apache web page then all is good, if not then something is broken and you will need to fix it before continuing down the path of virtual hosting. Check your error.log file for clues as to where the problem may lie.

Okay, so let us begin with a basic name-based virtual host configuration.

To establish name-based virtual hosting along with aliasing you will need to amend the httpd.conf file. The very first thing we must do is to enable the virtual hosting aliasing module. Inside of the httpd.conf file, search for the following line:

#LoadModule vhost_alias_module modules/mod_vhost_alias.so
To enable this, simply remove the '#' from the beginning of the line and save the file.

Dependant upon your host operating system, it may appear to have a different relating module, in this case mod_vhost_alias.so but the principle still stands. This module allows us the flexibility of using aliasing as part of our virtual hosting directive. It will become clearer as you read on.

While you have the httpd.conf file open scroll down to the very bottom of this file and add the following:

NameVirtualHost *:80

# This directive tells Apache to operate a name-based virtual host running on all IP addresses available to the system but exclusively on the default port for web servers, being port-80.

Now, below what you have just added, add the following code too:

# Start Virtual Host Container
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot "C:/apache/htdocs/example"
</VirtualHost>
# End Virtual Host Container

# What is shown above is a typical Virtual Host container, in this case for the site www.example.com. Notice that the VirtualHost directive matches the NameVirtualHost directive in that they both state *:80. Also note that the main ServerName points to www.example.com but we have also included example.com as an Alias should the client (browser) requesting access choose this format. This aliasing is what required the enabling of the module earlier in this how-to. The DocumentRoot in this case is a natural extension of the default directory structure so much so that if you were to open a browser you could use the following URL to access the same site:

http://localhost/example

Because we want to test the name-based configuration locally using our own web server we must redirect system lookups for the example.com domain to the local system. This can be accomplished by either adding this site to your local DNS system, if applicable or more simply by editing your hosts file. On Microsoft Windows systems, this is found at c:\windows\system32\drivers\etc\hosts. The file may be set to read-only so you will need to change this in the file properties before saving to it. When you open the file you will likely see the following:

127.0.0.1 localhost

To this we are going to add our example of example.com. Amend the file as follows and then save.

127.0.0.1 localhost
127.0.0.1 www.example.com

That's it! Now restart the Apache service and go to the following URL:

http://www.example.com

To see if the aliasing is working correctly we can also try the following URL:

http://example.com

And last but not least that the localhost reference is still applicable:

http://localhost/example

Pretty simple, eh! Just a couple of line changes/additions, a restart of the Apache Web Service and you're in business.

Now if you wanted to add a second, third or any successive number of name-based virtual hosts you can copy and paste the same virtual host container information, amending the ServerName, ServerAlias (if any) and the DocumentRoot to account for each respective site individually.

Done deal!

Next time around we'll look at the changes necessary to the above information as relates to address or IP-based Virtual Hosting.

Hope this was helpful.

Dave

Reply

The content of this field is kept private and will not be shown publicly.
 
  • Allowed HTML tags: <a><em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Testing for human visitors.