auto vhost alias and php switching for localhost

For study and test purposes I often setup new installations on the local server of my computer (osx). I changed my local setup to make this proces easier.

The points I improved:

  • Switching between php versions (easy when possible)
  • Dynamic path for my docroot in stead of creating them all manually
  • Dnsmasq in stead of manually entering urls in /etc/hosts

It are three separate solutions for different problems but when you want to implement one of them I think it's word considering to implement them together. And since there is enough documentation for each topic it's easy to look it up and make it work together. My setup is on a macbook pro so I use osx (El Capitan). Below I will give you the steps and the documentation I've used.

Setting up apache mysql php homebrew Macos Sierra
I used this terrific step by step guide from Luke Armstrong to install my apache installation with homebrew in stead of the default installation. He explains how to set this up in a way that you can switch easy from php 5.6 to 7.0 or an other php version. I modified his setup a little to get the Dynamic path for my docroot so I can create as many installations as I want without setting up every vhost by hand. I used the apache documenation for this: Apache mod_vhost_alias.

My apache .conf file looks like this:
-----------------------------------------------------------------------
UseCanonicalName Off

<Directory "/usr/local/var/www/vhosts/_wildcard>
    Allow From All
    AllowOverride All
    Options Indexes FollowSymlinks MultiViews
    Require all granted
</Directory>

<VirtualHost localhost:80>
    VirtualDocumentRoot /usr/local/var/www/vhosts/_wildcard/%2/%3/%4+
    ServerAlias php70.*
    <FilesMatch "\.php$">
        SetHandler proxy:fcgi://127.0.0.1:9070
    </FilesMatch>
</VirtualHost>

<VirtualHost localhost:80>
    VirtualDocumentRoot /usr/local/var/www/vhosts/_wildcard/%2/%3/%4+
    ServerAlias php56.*
    <FilesMatch "\.php$">
        SetHandler proxy:fcgi://127.0.0.1:9056
    </FilesMatch>
</VirtualHost>
-----------------------------------------------------------------------

Some example url's and paths are:

The first part of the url php56 or php70 doesn't change the location of the docroot but only results in using an other PHP version. So it's very easy and fast to switch from php version and test your application. Keep in mind that it doesn't switch the php version on the commandline. The article of Luke Armstrong explains how you can switch your php version for the cli with brew unlink and brew link.

As you see the url's are ending in .localhost. I configured Dnsmasq to map everything with that extension to my localhost. For setting up Dnsmasq I used these instructions:

Furthermore I read this article on which I made the decision to use .localhost instead of .dev for my Dnsmasq: Don't use .dev for development

If you came across my article. I hope it helped you to find the documentation, connect the dot's and improving your own local development environment.

=============================================================
Aditional: change setup to commandline phpswitching.
A couple of days after I made the above setup I came across this tutorial where kind of the same brew installation is explained as by Luke Armstrong but with a smart tool to switch the php version on your commandline and for your browser at the same time. When setup you don't switch by using a specific url but by executing the commend sphp 70 or sphp 65 to switch. macOS 10.12 Sierra Apache Setup: Multiple PHP Versions

Tags: