If you are developing PHP application on your own PC, you probably have some vhost based configuration of your Apache. With some tricks, you can turn your Linux box into powerful development server without configuring vhost for every app . In the minimum configuration effort, you need to pass those steps:
- checkout repository to disk
- create vhost configuration with some ServerName
- create entry in /etc/hosts which reflects server name used in vhost to 127.0.0.1
I’m pretty sure, that above path is used by many developers. We can ommit two lasts steps, limiting preparation of the environment only to place source code somewhere in filesystem. To achieve this we need mod_vhost_alias module for Apache2 and simple DNS server which will resolve local domain names for us.
Mod_vhost_alias installation is pretty straightforward. It’s probably already bundled with your Apache2 and only thing needed to be done is enabling it with command:
sudo a2enmod vhost_alias
With mod vhost alias we can dynamically point Document Root based on server name. Just put below config in the /etc/apache2/sites-enabled/000-default
Now all calls will be pointed to our /var/www/FIRST_PART_OF_DOMAIN/web because of %1 modifier. You can use your own convention by using other modifiers in VirtualDocumentRoot directive (see documentation). Above configuration is very minimalistic, you should add some logging configurations and so on.
Last thing we need to handle is dynamically resolving domain names. In easiest way, you can add every name to /etc/hosts, but it requires you to put them there for every host. For example entry in /etc/hosts:
will lunch your application located in /var/www/myapp/ (by pointing to web/ directory, like for example symfony does). If you want to automate this, install dnsmasq, simple DNS server.
sudo apt-get install dnsmasq
Then edit /etc/dnsmasq.conf file (it maybe located somewhere else, I’m showing Ubuntu way):
Last thing we need to do is update /etc/resolv.conf file and add ours DNS resolver addres to the top of it:
Now you need to restart dnsmasq and Apache2:
sudo /etc/init.d/apache2 restart sudo /etc/init.d/dnsmasq restart
Now you can enjoy new LAMP configuration. Every new project needs only to be putted on disk in the /var/www/PROJECT_NAME and it can be accessed immediately from browser with url PROJECT_NAME.local.dev