June 28, 2016Matthieu Auger3 min read
Have you ever wanted to expose a local server to the entire internet? It may sound scary but can be really useful. Use-cases are infinite, here are a few:
Multiple solutions exist, open-source or not but my favorite is ngrok and you can learn to use it in just 2 minutes. Installation is quite easy, head over to https://ngrok.com/download and follow instructions.
In two words: it takes a local port number in input, and offers you a unique URL, accessible by anyone. For example, let's assume you are building a very basic web application in php
echo "Hello World" > index.php php -S localhost:8000
Now a web server is running your computer's port 8000. When you hit http://localhost:8000 on a browser, you can see your application.
How to expose it to the world? It's simple, just type:
ngrok http 8000
After a few seconds, ngrok will output a few informations, the most important one being "Forwarding": You now have a unique public URL bound to your local server. Paste it, share it, anyone can use it.
Ever had this weird bug on this specific device that you can't reproduce anywhere else? Your responsive website is displaying badly on your blackberry's boss? Sometimes you just don't know what is the issue and need to code in the dark, test something, see what is happening and repeat. ngrok eases this kind of workflow by preventing you to deploy your modifications again and again.
Webhooks are everywhere, GitHub, Slack, Mandrill, Trello, many applications use them. They allow you to build tools that will be notified when an event occurs. But these applications need public URL to send the payloads to. Tunneling your local environment with ngrok will help you during development process. The GitHub documentation is a good start if you want to get into it.
Your coworker has downloaded a large file and you want to get it? It can be anything, latest dump of your production database, a base virtual machine, heavy CSV. With ngrok, you can simply run a server (for example PHP built-in server) and expose your file:
php -S localhost:8000on that folder
ngrok http 8000(let's assume generated URL is https://bc153101.ngrok.io)
Two weeks ago a coworker was out of the office and needed to quickly access one of his client's production server which is IP restricted. How do we managed to do that?
ngrok tcp 22. The result was: tcp://0.tcp.ngrok.io:15602
ssh -A -p 15602 firstname.lastname@example.org. The
-Aoption ensured its SSH public key was forwarded in order to log into the production server.
”I want to expose a local server behind a NAT or firewall to the internet”. The ngrok promise is simple yet powerful. You should definitively think about it next time you struggle with public/private networks, firewalls or just want to get your local environment available to someone. And of course please remain careful with what you expose, and to whom.
Any other use-case comes to your mind? Please feel free to tell us in the comment section.
Web Developer at Theodo