When starting new applications, a developer often needs to set up a new virtual host. This includes setting up new configuration files for Apache or new Nginx site entries. Sometimes you need to do this manually. Other times, it’s handled automatically if you just define a site-to-folder mapping. But wouldn’t it be practical if we could skip that step, too, and just have any URL ending in, for example, .local.com look for its files automatically, so all we need to do is change our etc/hosts file only onceever? Whoa!
In this tutorial, we’ll set up Nginx and Apache to automatically look inside certain folders when it detects a given URL format. For example, the URL mynewsite.local.com will automatically look inside the DOC_ROOT/nynewsite/public folder for the index.php file. You will then be able to define other patterns suitable for generic scripts, standard MVC apps (public/index.php) and Symfony/Silex based apps (web/app.php) – all depending on the URL format (e.g. mynewsite.sf2local.com could look inside web/ for app.php, as is typical of those frameworks).
This tutorial will be Unix-centric, but is Windows-friendly if you use our Homestead Improved box.
Before we begin, make sure you have something.local.com in your /etc/hosts file, so that we can test this URL as we go along. If desired, modify it to your liking. I’ll be focusing on the local.com suffix for triggering Vhost lookups inside the something folder in our document root, but this can be literally anything that’s valid in a URL charset.
That is a pretty good little trick. Adding it to my nginx.conf now. Thanks for the tip!
I went looking for the reason why Nginx doesn’t allow the variable in the error log file name and it done so by design, so that Nginx can still write out errors, even when it can’t expand variables. Using SED is suggested as possible workaround.
You definitely shouldn’t redirect all .com addresses to your vhost, else you’ll prevent yourself from visiting 90% of the internet’s web sites. If you’re only dealing with a single instance, just make a separate virtual host block for each.
Not as useful when working with virtual machines and such tools as Vagrant. You can use this as template config for all VMs, but still need to update hosts file at host OS with VM IP.
With vagrant, the plugin vagrant-hostmanager works wonders. Automatically updates your host file so you don’t have to worry about it.
Works particularly well when you have a separate Vagrant install for each project too, just start up the box and your hosts file is automatically updated
Thanks, @baf. I’m definitely gonna check this one out. Perhaps you know if it’s overriding whole file, along with all custom records? Or is it only adding and removing appropriate lines?
@swader While in my previous comment I said it worked for me, for some reason it is half working on a different machine with Xampp. Dynamic is working but it is not getting/reading the .htaccess file, so the rewrite engine is not working and cannot view any other links. Could you help me out?
# Slim Framework v2 (Using Auto Virtual Hosts)
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias *.slim.dev
VirtualDocumentRoot "D:/abid/php/slim/%1/www"
<Directory ~ "D:/abid/php/slim/.*/www">
DirectoryIndex index.php
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>