1. Skip to Menu
  2. Skip to Content
  3. Skip to Footer>
Thursday May 2nd     6:25 PM PDT                                  

Apache Part 2 - Understanding Virtual Hosts

Written by Greg King Saturday, 04 July 2009 16:11

The Apache Webserver is one of the most popular applications being run under the FreeBSD operating system. Setting it up is not to complicated.  Getting it to work the way you want it to work can be a trick though as this software package is very configurable and the documentation does not always have the novice administrator in mind.


In part 1, we installed and did basic configuration of the Apache Web Server v2.2.x.  This article will explain how to use VIRTUAL HOSTS  which allows us to run more than one website off a single IP address.  We will not be attempting to set up a secure socket layer (SSL) server or html authentication yet. Those are subjects of later articles!

NOTE:The directory structure seems to change from release to release, so some research may be necessary to completely configure the system inspite of this documentation. In our example, our freebsd system is server.ourdomain.com.  Our IP is 192.168.1.100. Our new website will be called www.ourdomain.com which we should have configured in the dns how-to.
 

Understanding Virtual Host Entries

 
Here is a sample virtual host file entry with line numbers to use as reference.  We will explore what each line does to better understand how we need to set our webserver up.
 
  1. <VirtualHost *:80>
  2.      <Directory /home/my_login_name/www/>
  3.          Order allow,deny
  4.          allow from all
  5.      </Directory>
  6.         ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  7.         DocumentRoot /home/my_login_name/www/
  8.         ServerName www.ourdomain.com
  9.         ServerAlias ourdomain.com
  10.         Alias /icons/ /home/my_login_name/www/icons/
  11.         CustomLog /var/log/ourdomain-access.log common
  12.         ErrorLog /var/log/ourdomain-error.log
  13.         RewriteEngine on
  14.         RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
  15.         RewriteRule .* - [F]
  16. </VirtualHost>
Lines 1 and 16 simply define the start and end of our virtual host entry.  As we will learn, you can have more than one virtual host, so this syntax is needed to seperate them.
 
Lines 2 through 5 define the directory limits of this website.  Note the /home/my_login_name/www/ on line 2. If my account login was greg, the directory would be /home/greg/www/ .  The system automatically creates a /home/login_name for the user when you create the account.  Obviously, I can create user accounts at will, so I could create a different user account for each website I want, and put that user account name in line 2.  The user  has to create the www directory under their login as it does not exist by default.
 
Line 3 and 4 are for security.  The order is simply to tell it if all should be allowed or denied first.  I mean, if not otherwise specified, the user is allowed.  Line 4 works with line 3.  we are allowing from all ip's or users, anything in the /home/my_login_user/www directory.  Make sure you include those in your virtual host entry or attempts to view the site will be forbidden.
 
Line 6 is the email address of the server administrator.  I'm thinking that is YOU, so put your email address there.  I don't know that it is necessary, so you could just omit that line all together if you wanted?!
 
Line 7 is quite important!  When we did our configuration on the httpd.conf earlier, we told it to look for index.html and index.php files in the document root.   This is the directory it will look in for the initial file.  Your website will start with one file which might well have links to other files, etc.  If the user hits your site (as defined by the servername and serveralias files described below, the initial index.html file will be displayed to them.  If it is not there, then it will display the index.php file.  If neither is there, it will display a tree with directory structure of the <DIRECTORY> or will show an error depending upon how you further configure things.
 
Lines 8 and 9 are the heart of virtual hosts.  The ServerName directive is what this virtual host will respond to.  If a user hits port 80 on your webserver looking for www.ourdomain.com, apache searches the virtual hosts to see which one represents that request.  Line 9 is an alias to the server name.  In our case, a user coming in requesting www.ourdomain.com OR ourdomain.com would be fed the index.html file in the document root listed for this virtual host.  Sounds complicated, but it really isn't as we will see soon.
 
Lines 10 and 11 define were to log information relative to this virtual host.  You want seperate logs for each virtual host because they aid in troubleshooting problems and having multiple sites in one log makes them extremely difficult to search.
 
Finally, lines 13-15 are for security issues.  The issue is much to complicated to focus on in this basic article.  Just put them in your virtual host entries to be safer.
 
Ok, now that we have a better understanding of the virtual host, lets put it to use!  See you in the next article!
 
 
 
  FreeBSD Configuration
eXTReMe Tracker
Content View Hits : 405008