Set up Automatic Virtual Hosts with Nginx and Apache

Originally published at:

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, look for its files automatically, so all we need to do is change our etc/hosts file only once ever? Whoa!

Mind blown

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

Continue reading this article on SitePoint

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.


1 Like

Damn handy to know, I was looking into this last week and was unaware that I could do this in nginx.

It is specifically handy for deploying feature branches( and has a lot of benefits over using virtual directories.

1 Like

Great idea!

In my case I have .com and that point to the same directory, how to set it in apache?

Superb stuff. Thanx for great info.

For my windows Xampp I did something like this

<VirtualHost *:80>
    ServerName vhosts.fqdn
    ServerAlias *
    VirtualDocumentRoot "C:/xampp/htdocs/php/laravel5/%1/public"

And worked just fine :smile:

1 Like

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.

Not if you use dnsmasq which can do this automatically for you.

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 :slight_smile:

1 Like

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?

It adds and remove sections. Check the bottom of my post for an example …

There are 2 issues with my solution if you’re using puphpet, which is why it isn’t perfect.

  • You need to set a unique hostname for each Config.yaml
  • You need to turn off the default vhost installation that Puphpet tries to use, as that can cause some conflicts.

Nothing insurmountable and for the limited time we have it’s easier to work around that rather than build our own Vagrant file.

While you’re at it, the vagrant-cachier plugin is also very cool, and I’m pretty certain the vagrant-vbguest has sped things up as well.

Now to find a way to make oh-my-zsh the default inside Vagrant …

Example hosts file.

## vagrant-hostmanager-start id: dbe4914c-9e77-47aa-b259-0e150aa15e70   gptq
## vagrant-hostmanager-end

## vagrant-hostmanager-start id: d55c4fcc-822d-42d7-a64f-99b71be55a6c   october-test
## vagrant-hostmanager-end
1 Like

Once again - thanks a lot, @baf. I’m rather a Vagrant beginner and your tips are really helpful. :smile:

Priceless, thanks for this!

@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 *
    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

Sorry, I don’t do XAMPP and wouldn’t be able to effectively debug it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.