Set up Automatic Virtual Hosts with Nginx and Apache

article

#1

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!

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

Continue reading this article on SitePoint


#2

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.

Scott


#3

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(feature-whateverChange.internal.com) and has a lot of benefits over using virtual directories.


#4

Great idea!


#5

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


#6

Superb stuff. Thanx for great info.

For my windows Xampp I did something like this

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

And worked just fine smile


#7

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.


#8

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.


#9

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


#10

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 smile


#11

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?


#12

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
192.168.43.83   gptq gptq.local.dev
## vagrant-hostmanager-end

## vagrant-hostmanager-start id: d55c4fcc-822d-42d7-a64f-99b71be55a6c
192.168.43.83   october-test october-test.local.dev
## vagrant-hostmanager-end

#13

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


#14

Priceless, thanks for this!


#15

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

#16

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


closed #17

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