One development virtualhost config to rule them all (for apache2)

A colleague gave me a virtualhost config file that has made my life a bit easier.

I've run virtualized Linux machines as primary development platforms for a long time, and every time I needed to setup a new site for development I had to, among a lot of stuff, create a new virtualhost config for Apache, enabled it, and restart the server.

This in it self doesn't take all too long, maybe a few minutes at most, but the fact that there's a lot of other stuff that needs to be done before the dev-site is up and running, any shortcuts are appreciated.

So without further ado, here's the config (note that you'll need to enable the apache module vhost_alias):

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  # Match domain name against a folder
  VirtualDocumentRoot "/var/www/%0"

  # Default servername is localhost, set alias to wildcard
  ServerName localhost
  ServerAlias *

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

  LogLevel debug

  <Directory /var/www>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

What the config basically does is that it maps the incoming domain request against a folder with the same name in /var/www.

In my case I generally use .dev as the TLD for my projects. This means when I set up a new site, I create a new folder for the project, let's pretend the project is called example, the folder that I'd create would be called example.dev, I then clone the site to that destination (or symlink, depending on). Apache will now automatically map any requests for http://example.dev to the folder /var/www/example.dev/.

Here's how you webroot would look like with a couple of projects:

beije@debian:/var/www$ ls -al
total 24
drwxr-xr-x 6 beije beije 4096 Jul 16 13:09 .
drwxr-xr-x 3 beije beije 4096 Jul 16 13:09 ..
drwxr-xr-x 2 beije beije 4096 Jul 16 13:09 benjaminhorn.dev
drwxr-xr-x 2 beije beije 4096 Jul 16 13:09 example.dev
drwxr-xr-x 2 beije beije 4096 Jul 16 13:09 project.dev
drwxr-xr-x 2 beije beije 4096 Jul 16 13:09 wordpress-test.dev

Clean right?

The only thing I still have to do is manually update my hosts file (for Windows it's located in %windir%\System32\drivers\etc\hosts, Linux has it located in /etc/hosts). I really do wish operating system would allow wildcard entries in the hosts-file.

Benjamin Horn
Benjamin Horn
Developer at Bazooka
A Finnish-German full-stack developer who's worked with a multitude of different technologies throughout the years.
Vaasa, Finland