Drush: Virtualhost and Hosts file management

David G - DrupalI recently purchased a new computer for my house. In setting up a development environment for projects I wanted to find a utility tool to manage Apache virtualhosts and my systems local Hosts file. In the past I’ve used tools like MAMP(pro), virtual machines, this small GUI tool called Rapache for Linux, or VirtualhostX for OSX. … Listing off all these tools kinda makes me feel old :/ haha.

I’ve talked about Drush alot in the past as a very useful Drupal shell. In doing research over the weekend I stumbled across the Drush Packages site. Therein I found the Drush-Virtualhost package created by David Reid. Drush-virtualhost is a set of Drush commands that help to you List, Add and Remove Apache virtualhost entries, start and stop the Apache webserver, and maintain your systems Hosts file so the locally defined websites resolve in your web browser.

Note: A recent version of Drush (something like version 6+, which is considered the current stable version of Drush) is required to use this command set. You basically need a version of Drush which uses Composer to install and use this command set easily. I have a bad habit of installing Drush and basically letting it get out of date on my development boxes.

With a recent version of Drush, to install the virtualhost commandset you make use of Composer:

cd ~/.drush && composer require davereid/drush-virtualhost:dev-master

After installation and clearing the drush cache and invoking drush help we can see the Virtual package commands:

~$ drush --version
  Drush Version  :  6.5.0

~$ drush cc drush
'drush' cache was cleared.

~$ drush help --filter=virtualhost
All commands in virtualhost: (virtualhost)
 apache-restart Restarts the Apache server. 
 apache-site-add Add an Apache site configuration file. 
 apache-site-disable Disables an Apache site configuration file. 
 apache-site-enable Enables an Apache site configuration file. 
 apache-site-list Lists Apache site configuration files. 
 apache-site-remove Removes an Apache site configuration file. 
 host-add Add localhost to /etc/hosts file. 
 host-remove Removes localhost record from /etc/hosts file.

Then as you develop your projects in a Drush makefile and Bash scripts. Or simply want to create LAMP based projects, you’ll find this commandset useful. These virtualhost commands need not be limited to Drupal based websites!

Here is an example of the options supported by adding a new virtualhost and hosts entry:

~$ drush help apache-site-add
Add an Apache site configuration file.

 hostname                                  The hostname to use. 
 docroot                                   The docroot to use.

 --aliases                                 A space-separated list of additional 
                                           ServerAlias directives to use.       
                                           Defaults to a wildcard *.$hostname   
                                           to support subdomains.

~$ drush help host-add
Add localhost to /etc/hosts file.

 hostname                                  The hostname to add.

 --hosts-file                              The location of the hosts file. 
                                           Default is /etc/hosts.          
 --local-ipv4                              The local IPv4 address to use.  
                                           Default is           
 --local-ipv6                              The local IPv6 address to use.  
                                           Default is ::1.

Here is actually making a demo site at example-project.example domain.

/var/www/2015$ drush apache-site-add example-project.example/home/dgurba/apps/projects/webdev/2015/example.project/ 
# Created on 2015-02-14 by dgurba
<VirtualHost *:80>
  ServerName example-project.example
  ServerAlias *.example-project.example
  DocumentRoot /var/www/example-project.example

  <Directory /var/www/example-project.example>
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all

  ErrorLog /var/log/apache2/example-project.example.error.log
  CustomLog /var/log/apache2/example-project.example.access.log combined
rm: cannot remove ‘/var/www/example-project.example’: No such file or directory
‘/var/www/example-project.example’ -> ‘/home/dgurba/apps/projects/webdev/2015/example.project/’
Added hostname record ' example-project.example' to [ok]
Added hostname record '::1 example-project.example' to /etc/hosts. [ok]
Enabling site example-project.example.
To activate the new configuration, you need to run:
 service apache2 reload
 * Restarting web server apache2

As you can see the virtualhost file is created and a Hosts entry is added to the system. The webserver is then restarted. This is alot nicer workflow then attempting to create these files by hand. All this plumbing is being done by a tool we’re growing more and more used too — Drush.

Note: You should to create your website docroot folder prior to issueing this command. Also note that this tool creates symlinks from the global Apache root as <hostname> and points the symlink to <docroot>. Here for example I have a projects directory within my $home folder. In examining this I kinda like it — It’s keeping the basic structure of the default OS largely unchanged and uncluttered, nice!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *