Dynamic apache vhosts
As a developer you most likely run dozens of projects in your webroot directory.
Setting up a new apache host and meddling through the apache conf files for every single project is just a waste of time. Not mentioning that conf file(s) can go huge over time. So, for the past six months or so I’ve been using a dynamic vhost configuration.
The concept is very simple. Let’s assume that you want to access each project via some dynamic domain like “$project.local”. “$project” would be a dynamic project name, and “.local” would actually be a localhost server.
First thing first, make sure that vhost module is enabled:
$ sudo cp /etc/apache2/mods-available/mod_vhost_alias.load /etc/apache2/mods-enabled/mod_vhost_alias.load
Now edit the default apache conf file:
$ sudo nano /etc/apache2/sites-enabled/000-default
Add following configuration to it:
<VirtualHost *:80> ServerName local ServerAlias *.local VirtualDocumentRoot /var/www/%1/public_html UseCanonicalName Off </VirtualHost>
Retart the apache service
$ sudo service apache2 restart
Now the only “hard” part about this dynamic apache vhost configuration is that you have to set a new line ih hosts file for each project, which will point to local ip address. I’ve heard that this can also be dynamically ser over the bind DNS server, but I’ll explore that some other time
$ sudo nano /etc/hosts 127.0.0.1 local 127.0.0.1 foo.local
Comments Off