How to setup a Wildcard DNS Setup for your local development on MacOS with Apache

If you have a lot of web projects, then you quickly accumulate something like a “websites” folder with all the projects. Until recently, I created a corresponding vhost config in Apache for each individual project. This process can be annoying, which is why you can optimize here.

One of my colleagues is already using a similar setup where the name of the CMS is its TLD and the name matches the folder names. He uses a wildcard to then access his new WordPress project, for example, as follows
name-of-projectfolder.wp. If you want something similar, go through the tutorial. I tested this on my Mac Studio running macOS Monterey (12.3.1)

Install and Setup Dnsmasq

First of all you need to install Dnsmasq, which provides a local DNS server that we need for our task.

brew install dnsmasq

After that go to the config of dnsmasq. In my case the config can be found here: /opt/homebrew/etc/dnsmasq.conf. In this file we can add our TLD Suffix that we would like to use. I would recommend to check if the TLD that you would like to use is not something that already exists. You can check the this List by the IANA for this. In my case I would like to use .wp and .neos.

# Append this to the end of the config file
# Add a new ‘address’ line for each TLD you want to add.
address=/.wp/127.0.0.1
address=/.neos/127.0.0.1

Now we need to create a resolver config for every TLD we want to use. macOS has a built-in function for that. We can use it by creating configuration files in the /etc/resolver/ directory. This directory probably won’t exist on your system, so your first step should be to create it:

sudo mkdir -p /etc/resolver

# Now create a file for every TLD
sudo touch /etc/resolver/wp
sudo touch /etc/resolver/neos

After that, open the files and add following to the files.

nameserver 127.0.0.1

Now, start the dns service.

sudo brew services start dnsmasq

Now you can ping any domain that ends with your created TLD and it should resolve to your localhost at 127.0.0.1.

Create a Wildcard VirtualHost Configuration for your Apache Server

After dnsmasq is ready to go, we need to edit the vhost Config of your Apache Server. First of all make sure that the modules mod_vhost_alias and mod_log_config are activate. Then go to your vhost config and add following.

# Every TLD get a vhost Block
# Dont forget to change the path
<VirtualHost *:80>
    ServerName neos.local
    ServerAlias *.neos
    VirtualDocumentRoot /Users/marco/Sites/Web/%1
    ErrorLog "/opt/homebrew/var/log/httpd/neos_error.log"
</VirtualHost>

<VirtualHost *:80>
    ServerName wp.local
    ServerAlias *.wp
    VirtualDocumentRoot /Users/marco/Sites/%1
    ErrorLog "/opt/homebrew/var/log/httpd/wp_error.log"
</VirtualHost>

After that test your Apache Config with sudo apachectl configtest. If the Syntax is OK you are ready to go. Restart your Apache and test your wildcard.

Leave a Reply

Your email address will not be published.