The lead developer at Mosaic, Brighton with a passion for web application development and motorcycles.
When working in a team it is very useful to have a central web server with multiple environments and a configuration as close to the live server as possible. This can be a bit of a nightmare though if you need to setup a new VirtualHost container in Apache every time a new project is brought on or when a developer wants to work on a version of the site in their own environment.
The good news is that this can all be handled automatically and new sites can be setup by simply adding a new directory to the file system. There are at least two ways of getting this going; the first of which is the mod_vhost_alias module for Apache and the second is enabled via mod_rewrite. I prefer to use the second method as it is more flexible and it allows you tap into the ability of mod_rewrite to introduce environment variables and redirect requests (this is particularly useful for robots.txt - you’ll see).
The Apache2 Manual does have a very good page dedicated to overcoming this problem, but I will be sharing with you all the settings I am using which you will need to stop Google et. al. from crawling your sites served from the staging environment for example.

Sun VirtualBox Logo
Often Linux just does it better! Often I find myself developing a Windows machine without access to a Linux development server, but I still need to access to some of the Linux binaries and features such as cron jobs, the at command and binaries such as imagemagick, pdftotext, etc. Some things can be emulated with ported binaries or through Cygwin, but I feel a lot more comfortable developing on a platform that is representative of the live server the web site will run on.
I have not removed XAMPP as it is very useful for developing small scripts or sites without the overhead of running a virtual machine. Therein lies the problem with this solution – it is virtual machine based and that will mean your local development machine will suffer when the virtual machine steals computing time from the CPU or more memory. So whilst I can use XAMPP on my netbook I am not so sure a VM will run smoothly.
The idea behind this step is to minimise the time it takes to setup a new host on the server. Basically all you have to do is create a new directory an away you go. (Don’t forget it will still need to appear in your hosts file).
VirtualDocumentRoot sets the directory that the VHosts public directory is contained in. This will basically convert http://subdomain.localhost/ to /mnt/htdocs/subdomain and pull the relevant files through.
A side note when using this method that you should be aware of. This will affect your rewrite rules if they are placed into a .htaccess file. To avoid any problems always declare the RewriteBase rule in your .htaccess.
For example:
Nathan brought a glaring omission from my post to the fore – thanks! Currently you maybe having permissions issues with your shared folders because they might be being mounted as root:root. To get them to load with a specified user and group you will need to edit your /etc/fstab file again and change ‘defaults‘ to be ‘uid=username,gid=groupname‘ – an example would be ‘uid=simon,gid=www-data‘.
A full line example would be from:htdocs /mnt/htdocs vboxsf defaults 0 0
tohtdocs /mnt/htdocs vboxsf uid=simon,gid=www-data 0 0

Apache HTTP Server
I really like the way the Apache modules and virtualhosts are seperated out on Debian into folders containing those, which are available and those which are enabled. There is one small problem with this – it is more work than before! Luckily there are some helper scripts.
The Apache configuration files are layed out in the following way:
mods-available – the actual text files containing the modules configuration
sites-available – the vhosts text file for the site
mods-enabled – a symlink to the actual text file in mods-available
sites-enabled – a symlink to the actual text file in sites-available
Anything listed in the enabled directories will be loaded when Apache is therefore enabling the respective site or module. Manually symlinking these up can be a right pain so I use the following scripts to assist me:
“a2ensite sitename” – will create the symlink for you
“a2dissite sitename” – will remove the symlink for you
“a2enmod modulename” – will create the symlink for you
“a2dismod modulename” – will remove the symlink for you
When the symlink is place the module or site will be loaded and vice versa.
Do not forget that you still need to reload the configurations into Apache by running “/etc/init.d/apache2 reload”.
Here are some hints for those of you that use a XAMPP install for testing your developments on your local machine.
I am using a Windows machine running XP Pro and this is how I setup my VirtualHosts. The conf file you need to amend is located at c:\xampp\apache\conf\extra\httpd-vhosts.conf Open it up in your favourite editor and un-comment the following line near the top of the file:
NameVirtualHost *:80
This will enable the creation of VirtualHosts in your XAMMP installation.
Firstly you need a VirtualHost setup for localhost so that you can access the XAMPP scripts and demo files and any projects you might already have in the default htdocs location. This will look something similar to this:
Now for your custom VirtualHost. This can be placed anywhere on your computer where there are read permissions setup. It does not have to fall under the standard XAMPP htdocs folder. It will look something like this:
You will notice that in the above example there is a directory directive applied to the VirtualHost where there wasn’t one in the standard localhost VirtualHost we setup first. This is because the default install of XAMMP does not allow us to have folders outside of the DocumentRoot (c:\xampp\htdocs). The VirtualHost I setup was in c:\xampp however so I had to allow Apache access in the VirtualHost container.
Don’t forget to add your new ServerName to your hosts file. Usually found in c:\WINDOWS\System32\drivers\etc\hosts and editable with any text editor. Of course it goes without saying that Apache must be restarted after any configuration changes and you may need to restart your browser after some changes to the hosts file.
To begin with I am using Apache2 so this will not apply to Apache1.3. Apache2 uses an interesting technique for setting up Virtual Hosts, they are no longer stored in the huge httpd.conf file. They are stored in two folders, sites-available and sites-enabled.
I am basing this setup on the following structure:
/home/www/ -- www.sitename1.com -- htdocs -- stats -- logs -- www.sitename2.com -- htdocs -- stats -- logs
To make a new Virtual Host you make a new file in the sites-available directory. Sitename = your new site. Don’t forget to sudo if your not logged in as root
cd /etc/apache2/sites-available vi sitename
Then enter the following into your new file:
<VirtualHost *>
ServerAdmin email@address.com
ServerName sitename.com
ServerAlias *.sitename.com
DocumentRoot /var/www/sitename/htdocs
ErrorLog /var/www/sitename/logs/error.log
TransferLog /var/www/sitename/logs/access.log
</VirtualHost>
The asterisk in the initial VirtualHost tag can be replaced by a particular IP address if your server has more than one.
You need to add a symlink to the sites-enabled directory for apache2 to add your new VirtualHost to its serving directories.
cd ../sites-enabled ln -s ../sites-available/sitename
Now you need to create the folders mentioned in the structure above:
mkdir /var/www/sitename mkdir /var/www/sitename/htdocs mkdir /var/www/sitename/htdocs/stats mkdir /var/www/sitename/logs /etc/init.d/apache2 restart
So you have your nice little virtual servers setup, but how do you know who is accessing them? Well the following article is good starting point: http://www.debian-administration.org/articles/85
However there are a few helpful extras that you need to know.
When installing and Debian asks you for the location of the log files you need to modify the path from /var/logs/apache/ to /var/logs/apache2 because you are running Apache2.
When creating that cron job in /etc/cron.daily you must set permissions for it:
chmod +x mycmds.sh
If you wish to run the script now to test it then you need to type:
./mycmds.sh
Also when you run webalizer it may chuck an error saying something like it could not find /var/logs/apache2/access.log.1 This is because the log has not been rotated yet so if you don’t want to wait for the cron job to do it for you you can:
logrotate --force apache2