VirtualHosts in Apahce2

Introduction


Apache is the most widely used open source HTTP server. It is a cross-platform web server. A more detailed definition is found on http://en.wikipedia.org/wiki/Apache_HTTP_Server or http://apache.org/

Objective

I want to set up a VirtualHost on my laptop so that I can start working on a website that
I am working on.

Senario

Website client : mr.X
web address(URL): http://www.mrx.org
web server: Apache_HTTP_Server
Scripting Engine : PHP

NOTE

  • The main focus here is to set up the web server to serve the website for mr. X.
  • The website  using http://www.mrx.org

VirtualHost configuration

Assumptions and considerations

  1. All the VirtualHost configuration mentioned here are found in the Apache manual page
  2. I assume that you have already installed Apache web server and scripting engine.
  3. You are using a GNU/Linux System especially Debian derivatives. If you don’t know what
    I am talking about now. Please wait for a little while till I publish one or Google them up.
  4. All the website data is located under /var/sites/www.mrx.orgNOTE
    • I always follow this directory layout where all the sites are stored under /var/sites/ and individual sites will be named in the same way as their DNS i.e, http://www.mrx.org will be stored as follows /var/sites/www.mrx.org

Actual VirtualHost configuration

Filename : /etc/apache2/sites-available/www.mrx.org

<VirtualHost *:80>
  # Address of the server
  ServerName www.mrx.org
  # Email address for the web master
  ServerAdmin admin@mrx.org
  # Where the website data is located
  DocumentRoot /var/sites/www.mrx.org
  # Specify permissions on who can access this site
  <Directory /var/sites/www.mrx.org >
     Options Indexes FollowSymLinks MultiViews
     AllowOverride None
     Order allow,deny
     Allow from all
  </Directory >
</VirtualHost>

Now save the file http://www.mrx.org under /etc/apache2/sites-available/ and execute the following.

$ sudo mkdir -p /var/sites
$ sudo mkdir -p /var/sites/www.mrx.org
$ sudo a2ensite http://www.mrx.org
$ sudo service apache2 restart


Now open your hosts file found in /etc/hosts and enter the following details and save it.

 # this is a default content of the file /etc/hosts
 # DO NOT DELETE THE FOLLOWING ENTRY!
 127.0.0.1    localhost    localhost.localdomain

#Instead add your entries below
# Add entry for www.mrx.org this will be used as a DNS
127.0.0.1   www.mrx.org

Now you can access your web-site as http://www.mrx.org