VVV multiple projects with vagrant share

The vagrant share command is totally underused and working in a remote company it’s also totally necessary. Nearly everyday I’m asked to troubleshoot a piece of code or help figure out a bug. Usually I need some context in order to help so I say “can you share your screen” or “would you mind pushing that up to staging and I’ll have a look at it later”. That’s just no good because it creates a time gap where I can’t help them. And oftentimes by the times I get around to taking a look they’ve already given up or went another route. That just sucks and there has to be a better way right? Well, there is and it’s called vagrant share. But there is one issue and that is by default it shares the default configuration for the VM which working in VVV is the default screen which isn’t very useful in debugging a site. But there is hope yet.

Luckily with the vagrant share command you can specify a port as one of the flags. So this got me thinking I wonder if… You setup a site with it’s own port you could theoretically access each site like it was it’s own box right? I just blew my own mind in the process. Oh the possibilities.

So I added this to my Vagrantfile

1
2
3
4
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.network "forwarded_port", guest: 85, host: 8085
end

And then I changed my nginx port to listen on port 85 like so

1
listen 85;

So now I’m thinking cool let’s fire this up and see if it works so I type

1
vagrant share --http 85

And that works just fine if you take WordPress out of the situation but that defeats the purpose here since I work with primarily with WordPress. So, what do I do now? We’re getting passed all of the checkpoints on this except for WordPress and the reason is it’s determining the home and site url either by the DB or the wp-config file. What we need to do here is make the install URL agnostic so that it will work with any domain.

We we need to either change tht in the DB or update the config. Updating the config is much easier so let’s just do that.

1
2
define('WP_HOME', "http://{$_SERVER['HTTP_HOST']}/");
define('WP_SITEURL', "http://{$_SERVER['HTTP_HOST']}/");

Bam! we are in business you type up vagrant share --http 85 and everything runs cool as ice.

But what about images? And other things? Just throw in a proxy pass like in the example below and everything works wonderfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 85;
listen 443 ssl;
server_name site.dev *.site.dev;
root /srv/www/site;
include /etc/nginx/nginx-wp-common.conf;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|mp3|mov|tif|tiff|swf|txt|html)$ {
expires 24h;
log_not_found off;
try_files $uri $uri/ @production;
}
location @production {
resolver 8.8.8.8;
proxy_pass http://www.site.com/$uri;
}
}

Now anyone you share that with will be able to help checkout your local machine without any trouble they can even login and poke around the admin.