Blog

Thoughts from my daily grind

Custom Domain Name - Web Development - macOS

Posted by Ziyan Junaideen |Published: 16 May 2021 |Category: Apple
Default Upload |

There is no place like localhost for a web developer. We are more familiar with localhost than we are with our homes. While localhost is very convenient, it doesn't work well for SaaS (multi-tenancy) projects that use subdomains. I generally configure custom local domains (.test) to all my projects. For example, my iMac has a *.jdeen.test custom domain pointed towards a Vagrant VirtualBox VM.

This article discusses how you can configure a custom domain using dnsmasq. Dnsmasq is a DHCP and DNS caching application available for macOS and Linux.

Installation

There are a variety of ways you can use to install dnsmasq on your Macintosh. I recommend Homebrew. Since I am relatively new to macOS and I am coming from Ubuntu I find Homebrew very convenient and familiar.

brew up
brew install dnsmasq

Once installed I used the following commands to configure dnsmasq to automatically launch on system startup. You can choose to follow this step. In that case, you may need to manually put up dnsmasq after each restart.

# Copy the default configuration file.
cp $(brew list dnsmasq | grep /dnsmasq.conf.example$) /usr/local/etc/dnsmasq.conf

# Copy the daemon configuration file into place.
sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/

# Start Dnsmasq automatically.
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Configuration

Once you have dnsmasq installed, the next step is to configure the custom domain. I use DOT-test as the top-level domain name for my projects. That way www.jdeen.com will be www.jdeen.test in local development.

I like to place these configurations in a separate file.

cat /usr/local/etc/dnsmasq.d/dot-test.conf
address=/test/127.0.0.1

address=/jdeen.test/192.168.10.10
address=/laundry.test/192.168.10.11

We are instructing dnsmasq to point all DOT-test domains to localhost (127.0.0.1). We then go and override it saying "however point jdeen.test to 192.168.10.10". If you are wondering, 192.168.10.10 is a Vagrant VirtualBox VM configured to run the Rails application.

While we have a configuration file, dnsmasq doesn't know about it. This is how we tell dnsmasq to find any of our .conf files. On the bottom of the dnsmasq.conf add conf-dir=/usr/local/etc/dnsmasq.d,*.conf.

cat /usr/local/etc/dnsmasq.conf
# .
# ..
# ...
# lot of other config...

conf-dir=/usr/local/etc/dnsmasq.d,*.conf

Once you update the configuration files related to dnsmasq, you should always restart the application. The instructions I provided will not make a restart method so you will have to stop it and then start it.

sudo launchctl stop Homebrew.mxcl.dnsmasq
sudo launchctl start Homebrew.mxcl.dnsmasq

Now if you have Nignx configured, visiting any .test site will land you on the Nginx home page. You may configure the dot-test.conf file to fit your needs either pointing to virtual machines or localhost itself.

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments