Automatic vhost configuration
Created 7 years ago by keevitaja

Hello !!!

just the other day we had a discussion on vhosts configurations and Laraval's Valet. And Valet is only for Mac. But actually, this Valet is not needed at all. I have used this on linux, but quick google showed that it is possible on Mac as well. Not sure about windows... windows sucks bigtime!

So the end goal is, that when you brows http://something.app the request will be automatically routed to correct folder and no vhost or system configuration what so ever would not be needed. Please note, that the location for configuration files may vary for different systems and Linux distribution. I use Arch.

First create a nginx vhost configuration file and enable the vhost, where the domain name is a wildcard:

/etc/nginx/sites-available/wild (wild is config file)

server {
    listen 80;
    server_name ~^(www\.)?(?<domain>.+)\.app$;

    root /usr/share/www/$domain/public;

    index index.html index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Next install dnsmasq and start/enable it. On arch

sudo pacman -S dnsmasq
sudo systemctl enable dnsmasq (on Arch it will be automatically started)

Then locate your resolve.conf (on Arch /etc/resolve.conf) and add to this folder file resolve.conf.head with following contents:

nameserver 127.0.0.1

Next open /etc/dnsmasq.conf and add following line:

address=/app/127.0.0.1

Restart your nginx and perhaps dnsmasq.

When all done, you should be able to browse any domain ending with .app . No vhost or /etc/hosts touching needed

Please do note, that this is only for the local development environment. DO NOT USE IN PRODUCTION

ryanthompson  —  7 years ago

Awesome! Thanks for sharing! Does valet just wrap it into a single service or something?

keevitaja  —  7 years ago

I do not know, but as you have to install everything manually by Valet, it must be doing something similar.

adnan  —  7 years ago

Thanks for the tip, .. Admittedly, I'm using valet, and it saved my life (exaggeration), .. Valet is largely useful because it does what Ryan was hinting at, wrapping this setup into a thing, (has, valet restart) .. icing on the cake, serving individual folders and even exposing local apps to the whole world wide web hh, .. something I haven't used, but I keep in the back of my mind,

huglester  —  7 years ago

keevitaja, thanks for this. I have already setup to just do vhost.sh new.app, and it does all the thing. Now I won't need to create nginx configs😄 thank you. was googling for this, but actually did not find the 'right' way to build those variables like:

server_name ~^(www.)?(?.+).app$; root /usr/share/www/$domain/public;