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
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.