When starting work on a new web project, one of the steps involved is setting up a virtual host on the computer you use for development. Doing this manually becomes boring very quickly: creating a new virtual host file, enabling it, adding an entry to your hosts file, and, depending on your setup, symlinking a few things together to make it all work.
Instead of doing this every time, you can set up a 'catchall' virtual host, also called a 'wild card' virtual host. This virtual host makes it possible to use one top level domain on your local, and automatically have its subdomains map to a given documentroot on your local, following a certain pattern. I'm using .dev as a top level domain in my setup.
Additionally, a local DNS server (I'm using Dnsmasq) can send all requests for a certain domain to your localhost, so you don't have to worry about adding entries to your hosts file ever again.
Below are the steps to accomplish this. Note that I'm on Ubuntu 13.04, but this should work equally well (possibly/probably with some slight changes here and there) on other Ubuntu versions or Linux flavors, and on OSX.
(This post is based on the very helpful write ups/comments by Randy Fay, Giles Smith, Daniel Hahler and Grumot. Since I had to make slight changes to make it work for me, I decided to do a quick write up, for future reference.)
1. Set up catchall vhost
- Enable Apache's vhost_alias module:
sudo a2enmod vhost_alias
- Create /etc/apache2/sites-available/catchall.conf
ServerAlias localhost *.dev #wildcard catch all
Allow from all
Require all granted
- Enable the virtual host configuration:
sudo a2ensite /etc/apache2/sites-available/catchall.conf
- Reload the Apache configuration:
sudo service apache2 reload
- To make the VirtualDocumentRoot work, you also need to uncomment the following line in Drupal's .htaccess:
# RewriteBase /
2. Configure local wildcard DNS server
- Install Dnsmasq:
sudo apt-get install dnsmasq
- Since Ubuntu's NetworkManager uses dnsmasq, and since that messes things up a little for us, open up
/etc/NetworkManager/NetworkManager.confand comment out the line that reads
dns=dnsmasq. Restart NetworkManager afterwards:
sudo restart network-manager.
- Make sure Dnsmasq listens to local DNS queries by editing
/etc/dnsmasq.conf, and adding the line
- Create a new file in
/etc/dnsmasq.d/dev, and add the line
address=/dev/127.0.0.1to have dnsmasq resolve requests for *.dev domains. Restart Dnsmasq:
sudo /etc/init.d/dnsmasq restart.
3. Have your localhost resolve domain names
The final step needed to make this all work, is to let your localhost resolve domain names you request, so it can send requests for the .dev domain (or whatever domain you choose to work with), to your localhost. To do this, you open the file
/etc/dhcp/dhclient.conf, and uncomment the line
#prepend domain-name-servers 127.0.0.1;. Next, refresh your local DNS handling by typing
4. Enjoy doing no more manual virtual host configuration
To add a new site on your local machine, you can simply add your codebase in your workspace, e.g. /path/to/your/workspace/drupal/public, will be accessible on your local at the url drupal.dev.