Sometimes when you are locally building a Laravel application and you're using Homestead for example, you'll need to expose your project at some point somewhere across the internet. And this could be for a variety of reasons.
00:13
It could be that you want to just share the URL with someone to preview your project or usually you'll find that you want to use something like a webhook and test a webhook hitting your application. Now the only other way to do this is to start to expose ports on your local machine which
00:30
obviously is not a good idea, it's not a secure way to do this. So we're going to be looking at using ngrok to expose our local server but via Homestead. So usually ngrok works really nicely out of the box but for Homestead there's a little bit more setup.
00:47
So if you're already using ngrok, that's great. If you're not, go ahead and sign up and download this and we're going to start to walk through how we get this exposed. So I have ngrok just stored within my home directory just here and I can go ahead and
01:03
run this just to make sure everything is good. So usually what we would expect to do is something like ngrok HTTP and then we would say, well, we're working on port 8000 so we would just use the port 8000. Now this would typically work if you're working with say a local Apache server or a local
01:23
Nginx server but in this case it doesn't work and that's because with Homestead we have names for each of our projects and it's just not going to work this way. So instead what we need to do is make a few changes to our laravel Homestead configuration. So to do this I'm just going to hop over to the .homestead directory and I'm going to
01:45
open up Homestead.yaml in Sublime. So this is the mapping here for the someproject.app that I currently have open just here and you can see that this just points to a laravel installation. So what we actually need to do is create another mapping just here and it's pretty much the
02:04
same but it's someproject.ngrok.io. So now we want to map this to the same directory. So we can just go and pull this and we want to map it to this directory here, perfect. So this is all we need to do but obviously we need to go ahead and provision our machine.
02:21
So I'm going to go back a directory and into the normal Homestead directory and I'm going to run a vagrant provision on this and wait for this to finish. So now that that's done then I'm just going to come back to my local directory, my home directory and I'm going to run ngrok again but this time if we just run it I want to
02:42
use the subdomain option. So we run ngrok HTTP, we go ahead and provide a subdomain just here and that subdomain is going to be the name of your project. So in this case it's someproject and then we give the port.
02:57
So in our case again it's still port 8000. So when I run this now, again this opens up a secure tunnel for us and I can go ahead and paste this into here and you'll see that I get that project. And just to prove that this is all working, we can maybe head over to the roots file and
03:13
maybe just return something like oh hi and we can go back, preview this and everything is working nicely. So now you can very quickly boot this up when you want to test a webhook, maybe you're working with Laravel Cashier and you're working with Braintree and you want to test a Braintree
03:31
webhook to make sure say a user's subscription is cancelled, this is perfect. You can now do this and when you don't want it, you just go ahead and close this off. And that is it. That is how we set up a secure tunnel to our Homestead project using ngrok.
1 episode• 3 mins•8 years ago
Overview
Need to share a local project you're working on? Or test a webhook from a server? If you're using Laravel Homestead, we have you covered with this quick tip.