Most of the time a developer will need to test his programs against the production setup, to closely monitor the performance of the developed programs or if it will perform as expected when put into production. The best thing to do it is to enable Apache to mimic the production server setup so that the only thing a developer need to do is to transfer the files to the production server.
This is easily accomplished in Apache server using the virtual host configuration, wherein we can setup a server instance which exactly matched our production server. We just need to enable all the modules that we need and proceed in configuring the virtual host file.
Why do we need to enable virtual host?, you would say but apparently virtual host is needed if:
* you´re on multiple domains
* you need to test your projects with same configuration with the server
* test your projects without touching your public server
* you´re just organizing your projects into groups
* or setup a local copy of your blog or website
What do we need?, we need a server or a local computer running Apache either in Windows or Linux. Since most of us are developing using Windows we might as well use Windows using Apache Xampp for this setup.
1. In Apache we usually edit the httpd.conf to configure the virtual host file, but in Xampp it is a separate file being called inside the apache httpd.conf. You will find your httpd.conf in your drive where you installed Xampp in my case it’s in my D: drive under the xampp folder navigate through D:\xampp\apache\conf you will find there your httpd.conf.
2. Inside our D:\xampp\apache\conf directory we will see a folder named extra. These folder holds additional configuration for our apache server. Before the functions can be configurable inside the httpd.conf but now it is just being called inside the httpd.conf when we start the server. Configuration in this manner is much simple since we will immediately know in which part of the configuration, we have committed an error if our server suddenly doesn’t start. It is always a good idea to create a backup of the original file for reference or to restore it to original state if we commit an error.
3. In the folder extra look for the file named httpd-vhost.conf this is what we need to configure to enable our virtual host environment. We will used name-based virtual hosting for these. There are two configurations for virtual host name-based and ip-based.
When you open the httpd-vhost.conf you will see something like the following code below as a sample code:
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any
# ServerAdmin webmaster@local
# DocumentRoot D:/xampp/htdocs/www
# ServerName local
# ServerAlias local
# ErrorLog @rel_logfiledir@/cabsites-error_log
# CustomLog @rel_logfiledir@/cabsites-access_log common
# Options Indexes FollowSymLinks
# AllowOverride All
# Order allow,deny
# Allow from all
Options Indexes FollowSymLinks
Allow from all
I just configured my development server to use D:/xeroxss/app/webroot as my webroot folder wherein, I will put my webfiles. Localhost configuration reflects my default Xampp configuration folder for localhost so that my server will still be accessible through localhost with my default configuration, useful in accessing our PhpMyadmin installation.
My virtual host www.xeroxss.com will actually use my drive D:/xeroxss/app/webroot and all my web documents will be served in that directory.
4. We are not done yet since we need to configure our computer to recognize the domain that we add remember that this domain is not registered and therefore will not be accessible in any DNS servers. So with all computers we need to configure our own computer so that it will search its own host file first before querying any DNS servers.
In Windows the host file is located under C:\WINDOWS\system32\drivers\etc\host so we need to edit this file and add the domain that we created to it for it to be recognized as valid domain by our computer. When you open it you will see:
# Copyright (c) 1993-1999 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
# For example:
# 220.127.116.11 rhino.acme.com # source server
# 18.104.22.168 x.acme.com # x client host
Put our configuration there save and restart the Apache server. If you encounter an error make sure that you follow the exact code above and check your syntax. To test just save an index.html in your created virtual host folder and it will be accessible in your virtual host server. Or try to ping your created domain in your command prompt first to make sure that your computer recognized your host file. You will be able to ping your domain and see something like this.