Laravel’s Homestead is a great tool to spin an Ubuntu-based machine for local Laravel development but one of the drawbacks of the default setup is that you need to interact with
/etc/hosts file a lot in order to map local test domains to your Homestead server. The scenario gets worser when you have to handle dynamic sub-domains on a Laravel project like I had to do. If you are bored editing the hosts file again and again like me, then there is yet another solution that lets you handle the DNS dynamically on your Mac machine.
Personally I am using a Macbook Pro Retina 15″ dating back to late 2013 but this guide should help you even if you are using a newer machine. So let’s get started.
Installing & Configuring dnsmasq
dnsmasq is a great tool written in C that lets you handle the DNS dynamically on your Mac machine. This means that with the help of dnsmasq, you can map
*.test or any other development domain to your Homestead server’s IP address (192.168.10.10 in most cases). The end result will be that you will only need to map the development domain to a project in
Homestead.yaml and you will not need to interact with
/etc/hosts file anymore.
Here are the steps involved in dnsmasq installation:
- In your terminal, run
to install dnsmasq
brew install dnsmasq
- To create config directory, run
mkdir -pv $(brew --prefix)/etc/
- Now to map all
*.testdomains to your Homestead machine, create the configuration file by running
. Double check to ensure that your Homestead server’s IP address is 192.168.10.10. If not, use the correct IP address here. The IP address can be found in
echo 'address=/.test/192.168.10.10' > $(brew --prefix)/etc/dnsmasq.conf
- To enable auto-start of dnsmasq, run
sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
- And then launch dnsmasq by running
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
The next step is to add the localhost nameserver to your resolvers. Here are the steps to do that.
- Create resolver directory if it isn’t there by running
sudo mkdir -v /etc/resolver
- And then add the nameserver by running
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'
Add localhost to DNS Servers
As the last step of dnsmasq configuration, got to
System Preferences > Network (WiFi or whatever you are connected to) > Advance > DNS and add
127.0.0.1 to the top of the DNS servers list. Remember that you need to move 127.0.0.1 to the top of the list so it will be queried at first. After adding the host there, apply the changes.
Now dnsmasq configuration is done. You can start using subdomains on
.test TLD in your Homestead.yaml without adding the domains to
If you are going to use dynamic sub-domains for a Laravel project, i.e.
third.foo.test, then the configuration is a little different in your Homestead.yaml file. Let’s say you are developing a CRM and want to assign dynamic domains on it. You will need to configure Homestead.yaml file like this:
sites: - map: '*.mycrm.test' to: /home/vagrant/mycrm/public
Notice the commas around your domain here. Now anything typed in the browser before
mycrm.test will point to your CRM project.
I hope this guide was simple enough. The dnsmasq installation guide was consulted from this gist and the remaining minor adjustments to make it work with Homestead are my own discoveries. You can try this guide and let me know in comments in case you are having any issues. I hope this guide saves you a lot of time for your future Laravel projects.