Laravel multi-tenancy via subdomain DNS setting (Manjaro Linux, dnsmasq)

Recently I was setting up my development environment for my Laravel project. This time I wanted to try out Laravel Homestead. This project implements a multi-tenant, in which each user will receive their own subdomain. I did not face this issue with Laravel Valet or artisan serve. Note that my knowledge regarding networking is very limited.

# vim /etc/dnsmasq.conf

Find “#server=“, uncomment the line and set your preferred DNS server (Google, OpenDNS, 1111 etc). This step is necessary (in my case) or else you would not be able to resolve any other address other than the ones defined in /etc/hosts or /etc/dnsmasq.conf

server=8.8.8.8

Add the following line:

address=/.your-app-url.test/your-app-ip
# example
address=/.myproject.test/192.168.10.10

If you’re using Homestead like I do, the IP can be obtained from Homestead.yaml. Save the file.

# vim /etc/NetworkManager/NetworkManager.conf

Add the following lines:

[main]
dns=dnsmasq

Save the file and restart the NetworkManager service.

Ping the address

$ ping myproject.test
PING myproject.test (192.168.10.10) 56(84) bytes of data.
64 bytes from myproject.test (192.168.10.10): icmp_seq=1 ttl=64 time=0.613 ms
64 bytes from myproject.test (192.168.10.10): icmp_seq=2 ttl=64 time=1.18 ms
^C
--- myproject.test ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.613/0.894/1.175/0.281 ms

$ ping hello.myproject.test
PING hello.myproject.test (192.168.10.10) 56(84) bytes of data.
64 bytes from myproject.test (192.168.10.10): icmp_seq=1 ttl=64 time=0.990 ms
64 bytes from myproject.test (192.168.10.10): icmp_seq=2 ttl=64 time=0.963 ms
^C
--- hello.myproject.test ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.963/0.976/0.990/0.013 ms

Source:

  1. https://askubuntu.com/questions/1029882/how-can-i-set-up-local-wildcard-127-0-0-1-domain-resolution-on-18-04
  2. https://serverfault.com/questions/118378/in-my-etc-hosts-file-on-linux-osx-how-do-i-do-a-wildcard-subdomain/118589#118589
  3. https://voboghure.com/2020/01/02/enable-wildcard-sub-domain-for-localhost-on-ubuntu-18-04/

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.