Skip to content

Instantly share code, notes, and snippets.

@cam8001
Last active October 12, 2021 12:44
Show Gist options
  • Save cam8001/e3a63acda17e28f27c53bf03d6dd951f to your computer and use it in GitHub Desktop.
Save cam8001/e3a63acda17e28f27c53bf03d6dd951f to your computer and use it in GitHub Desktop.
Set up dnsmasq for dynamic hosts on your local machine.

DNS

Install dnsmasq:

$ sudo apt-get install dnsmasq

Edit /etc/dnsmasq.conf to resolve all domains ending in ".lh" to your own machine:

# Resolve all addresses ending in .lh to this machine.
address=/lh/127.0.0.1

You need to configure systemd-resolve so it doesn't conflict with dnsmasq:

$ sudo vi /etc/systemd/resolved.conf

Add these lines to the end of the file:

# See https://askubuntu.com/a/909620
DNSStubListener=no

Now restart systemd-resolve:

$ service systemd-resolved restart

Make sure dnsmasq starts on boot:

$ systemctl enable dnsmasq

Now check it is all working!

$ nslookup foo.lh
Server:            127.0.0.1
Address:        127.0.0.1#53

Name:   foo.lh
Address: 127.0.0.1

Apache

Enable mod_vhost_alias:

sudo a2enmod vhost_alias

Create a new vhost config

vim /etc/apache2/sites-available/dynamic-vhosts_vhost_alias.conf

Add something like this following:

# See https://www.sitepoint.com/set-automatic-virtual-hosts-nginx-apache/
UseCanonicalName Off

<VirtualHost *:80>
  ServerName vhosts.fqdn
  ServerAlias *.lh

  VirtualDocumentRoot /home/camerontod/sites/%1

  <Directory ~ "/home/camerontod/.*">
   Options Indexes FollowSymlinks MultiViews
   AllowOverride All
   Require all granted
  </Directory>
</VirtualHost>

Enable your new config:

a2ensite dynamic-vhosts_vhost_alias

Restart/reload Apache:

sudo service apache2 restart

Now, request to domain.lh will resolve to /home/camerontod/sites/domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment