Use BIND (named) to create a multi-VirtualHost configuration – no more VirtualHostX

[Deprecated: for OSX 10.9 and up, please use rba.sh.]

It has been a while since I posted my guides for setting up a local development environment and how to add SSL to this set-up. This setup, among other things, required the use of a hostname-to-ip-management type of software. My choice was VirtualHostX.

Recent events have brought a new temporary colleague to our workforce and he brought in some fresh blood – so to speak (post in Dutch).

Today I have successfully eliminated the need for VirtualHostX – at least, in my development environment.

Using only the sentiment, the essence as it were, of his post, I have revised my own setup by doing the following. You can safely follow these steps after having setup the environment as described in my posts here and here.

There is one known issue; the SSL certificate generated no longer matches the domain, but who cares, it works :) It’s not like we’re creating a real-world-publicly-reached environment, right?

Reminder; This post is the fourth post in a series to build the “ultimate” setup. See ‘The “Ultimate” Guide …’ for a complete list.

Configuring BIND

  1. Fire-up the terminal and switch to super-user mode.
    Shell commandsudo -s
  2. Then edit the /etc/named.conf file to add the local zone;
    Add to /etc/named.conf (download file)zone "local" IN {
            type master;
            file "local.zone";
            allow-update { none; };
    };
  3. And create the zone-file:
    File-content for /var/named/local.zone (download file)$TTL    86400
    $ORIGIN local.
    @       IN      SOA     localhost.      user.domain.com. (
                                            42              ; serial
                                            3H              ; refresh
                                            15M             ; retry
                                            1W              ; expiry
                                            1D )            ; minimum

                            1D IN NS        @
                            1D IN A         127.0.0.1

    * IN A 127.0.0.1
  4. Now execute these commands;
    Shell-Scriptrndc-confgen -b 256 -p 54 > /etc/rndc.conf
    head -n5 /etc/rndc.conf | tail -n4 > /etc/rndc.key
  5. Finally, we need to configure named to start on boot and start it.
    Shell-Scriptlaunchctl load -w /System/Library/LaunchDaemons/org.isc.named.plist
    launchctl start org.isc.named
  6. Now we can test the name server
    Shell commandnslookup anything.local 127.0.0.1
    should return
    Shell output# nslookup anything.local 127.0.0.1
    Server: 127.0.0.1
    Address: 127.0.0.1#53
    Name: anything.local
    Address: 127.0.0.1

If this works as planned, add 127.0.0.1 to your networks DNS tab.

Reconfiguring Apache for the use of Wildcard Virtual Hosts.

IMPORTANT NOTE;

I have discovered that using a virtual document root as described below will cause problems with redirects/rewrites using .htaccess rewrite rules other than the root of the website. See my post on how to overcome this by rebuilding the Apache2 VirtualHost Configuration automatically. If you still want to use a virtual document root, follow instructions that follow now, otherwise, skip the rest.

Edit the file /etc/apache2/extra/httpd-vhost.conf (still as SuperUser)

Make it look like this;

File-content for httpd-vhost.conf (download file)NameVirtualHost *:80
<VirtualHost *:80>
        ServerAlias *.local
        UseCanonicalName Off
        VirtualDocumentRoot "/Development/%0.0/public_html"
        php_admin_value auto_prepend_file /Development/auto_document_root.php
</VirtualHost>

NameVirtualHost *:443
<VirtualHost *:443>
        ServerAlias *.local
        UseCanonicalName Off
        VirtualDocumentRoot "/Development/%0.0/public_html"
        php_admin_value auto_prepend_file /Development/auto_document_root.php
        SSLEngine On
        SSLCertificateKeyFile /etc/apache2/ssl.key/server.key
        SSLCertificateFile /etc/apache2/ssl.key/server.crt
</VirtualHost>

Please notice the reference to  /Development/auto_document_root.php

create a file with this name and place this inside;

File-content for /Development/auto_document_root.php (download file)<?php
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) ."/{$_SERVER['HTTP_HOST']}/public_html";

This will make sure that the DOCUMENT_ROOT property of the environment is set correctly.

As per hint of Jorijn, check to make sure that the line

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
is not commented out in /etc/apache2/httpd.conf

Final notes

Note that the directory structure is now always: /Development/domain.local/public_html/(your website files here). Need 3 domain-names to the same web-space? use symbolic links!

And with that, I say, Happy coding, thank you Jorijn and as usual; comments, questions, remarks; PLEASE :)

 

Author: Remon Pel

WebDeveloper though not WebDesigner

2 thoughts on “Use BIND (named) to create a multi-VirtualHost configuration – no more VirtualHostX”

    1. Hey. Thanks for the hint.

      LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

      This line is enabled by default on OSX 10.6 and newer, but it never hurts to check :)

Leave a Reply

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