A tool WorkFlow has a new functionality “construct-container” that sets up a new container, so you can spin(i.e. drush spin-it) new Drupal instances automatically. Besides creating database, directories in file system, the construct container task also sets up and configures virtual host for the new Drupal instance. In this post, we cover the approach that was taken to provide multiple virtual hosts solution for WorkFlow tool users.
The solution utilizes Apache module virtual_alias. To be able use the Wildcard domain names on the local machine, the DNS server – dnsmasq is used
Install virtual_alias Module
First, check to see if virtual_alias module is already enable and loaded with apache:
apachectl -M | grep "virtual_alias"
If this doesn’t return anything, then the virtual_alias module is not enabled. Lets do so:
sudo a2enmod virtual_alias sude service apache2 restart
This should enable the virtual_alias module and load in apache. Go ahead and verify with “apachectl -M” to see if its loading
Set Up Universal Virtual Host Alias
In this example, we configure one universal virtual host for DEV Environment(for WorkFlow Tool there is additional for TEST environment) that is going to service all of the new Drupal instances. To do so, lets add new file – dev-virtual.conf in the /path/to/apache/sites-available(by default /etc/apache2/sites-available) as following:
<VirtualHost *:80> ServerAlias *.dev VirtualDocumentRoot /path/to/dev/%1 ServerAlias localhost *.dev <Directory "/path/to/dev"> Options +SymLinksIfOwnerMatch AllowOverride All Require all granted </Directory> </VirtualHost>
Afterwards, lets enable the universal virtual host
sudo service a2ensite dev-virtual sudo service apache2 reload
Here, we specify root dir with ‘%1’ at the end to service each Drupal instance at its own directory. The ‘%1’ refers to the first part of domain name separated by dot. For example, metronic.dev will result the ‘%1’ refer to ‘metronic'(for more info see section ‘Directory Name Interpolation’).
As you see from the universal virtual host configurations above, the domain name we have set up to be *.dev, so next we configure all request *.dev point to the right machine.
Configure DNS or Set Up Own Local DNS
For public server, you will point all “*.dev” requests to the public server. In that case, all you need is to add the new DNS A entry in the DNS service as following:
*.dev IN A 177.77.177.77
For local machines, we cannot use a wildcard in /etc/hosts, so we have to set up local DNS. Let’s use the light weight DNS solution – dnsmasq
sudo apt-get install dnsmasq
Once installed, we configure by editing /etc/dnsmasq.conf and adding line:
address=/dev/127.0.0.1
Here, we set *.dev requests point to 127.0.0.1 which is the local machine
Afterwards, restart the dnsmasq:
sudo service dnsmasq restart
Now, all request with the wild card (i.e. “*.dev”) will be pointed to local machine where the universal virtual host should service them from the directory specified.
Troubleshooting
1. client denied by server configuration: /path/to/dev/some
This error was caused by adding %1 at the end of path for the Directory configurations. Here is correct way:
VirtualDocumentRoot /path/to/dev/%1 ... //<Directory "/path/to/dev/%1"> <Directory "/path/to/dev"> ... Order allow,deny Allow from all ... </Directory>
By removing ‘%1’, the actual directory is found and no more errors. In short, when you have VirtualDocumentRoot, then the Directory root is the path to the virtual aliases instead the full path as it is with static virtual hosts configurations
References
- http://brunodbo.ca/blog/2013/04/26/setting-up-wildcard-apache-virtual-host-wildcard-dns
- http://float64.uk/blog/2014/08/22/dynamic-development-area-apache-php-fpm/
- http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html
- https://httpd.apache.org/docs/2.4/rewrite/vhosts.html
- https://httpd.apache.org/docs/2.4/vhosts/mass.html
- https://code.google.com/p/mod-myvhost/wiki/Install
- http://eosrei.net/articles/2012/08/create-dynamic-virtual-hosts-apache-http-vhostalias