I would like to map my directories/subdomains in the following way:
/home/sam/sites/* to
I don't want to have to add a line to my hosts file every time I spool up a new site.
I'm a beginner when it comes to configuring and installing apache, what's the best way to achieve this?
51 Answer
It turns out there are quite a few steps involved in getting this working. Here is how I solved it:
- Edit /etc/resolve.conf to set your domain name resolution to your local machine by commenting out any existing lines and adding
nameserver 127.0.0.1. After this line add another line containing a secondary dns server for resolving other sites, egnameserver 8.8.8.8. Some program also overwrites this file every time you boot, so you can usechmod 0444 /etc/resolv.confto prevent this from happening (there might be a better way to achieve this). - Install dnsmasq, and at the end of /etc/dnsmasq.conf add a line
address=/your-custom-local-domain/127.0.0.1 - Create a new apache site config by placing a file called localdomain into
/etc/apache2/sites-availablewith the text at the bottom of this answer. - Run the following command
sudo a2ensite config localdomain. - Reboot apache, and create a new folder in ~/ called Sites.
<VirtualHost *:80> ServerName 127.0.0.1 ServerAlias *.your-custom-local-domain VirtualDocumentRoot /home/%2/Sites/%1/ #ErrorLog /home/%2/logs/error.%1.log </VirtualHost>
Now any new folder you create in the sites directory should be mapped to foldername.your-custom-local-domain.