Originally published at: http://www.sitepoint.com/set-automatic-virtual-hosts-nginx-apache/
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 once ever? 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
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.