Enable wildcard sub-domain for localhost on Ubuntu 18.04

Happy New Year my friends. Hope you all had a wonderful year and you are confident to face 2020! This year I am taking couple of challenges unlike previous years where I made “NEW YEAR” resolutions. Keep me in your prayers so that I win those challenges.

Currently I am developing a SaaS application where user can publish their site in sub-domain. So, for test this feature in development mode, I need to enable wildcard sub-domain facility in my localhost. I am using Ubuntu 18.04 and a virtual host name [mylocalhost.tapan] for the application. Here is what I need to do for enable the sub-domain for that [mylocalhost.tapan] host name.

It’s a little bit long since systemd-resolved does not play very well with NetworkManager when configured with dnsmasq.

Yet I still recommend starting dnsmasq from NetworkManager, because network connectivity changes (WIFI, wired, …) will be handled transparently.

Enable dnsmasq in NetworkManager

Edit the file /etc/NetworkManager/NetworkManager.conf, and add the line dns=dnsmasq to the [main] section, it will look like this :

[main] plugins=ifupdown,keyfile dns=dnsmasq [ifupdown] managed=false [device] wifi.scan-rand-mac-address=no

Let NetworkManager manage /etc/resolv.conf

sudo rm /etc/resolv.conf ; sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf

Configure mylocalhost.tapan

echo 'address=/.mylocalhost.tapan/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/mylocalhost.tapan-wildcard.conf

Reload NetworkManager and testing

NetworkManager should be reloaded for the changes to take effect.

sudo systemctl reload NetworkManager

Then we can verify that we can reach some usual site :

dig google.com +short 74.125.68.113
74.125.68.138
74.125.68.101
74.125.68.102
74.125.68.139
74.125.68.100

And lastly verify that the mylocalhost.tapan and subdomains are resolved as 127.0.0.1:

dig mylocalhost.tapan google.mylocalhost.tapan a.b.c.d.mylocalhost.tapan +short 127.0.0.1 127.0.0.1 127.0.0.1

I hope this will help you many like me who are searching for a local way of testing sub-domain facility of any application.

Thanks and happy coding!

(Visited 1,501 times, 1 visits today)

Leave a comment

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