Localhost / HTTP Tunnels: How Can You Make Localhost Public Using a Custom Domain?

Imagine working on a groundbreaking project on your local machine and then... voilà! With just a few clicks, your project is accessible to the entire world. The possibilities are endless and the barriers between local and global are blurring faster than ever. This is the power of a "localhost tunnel".

Whether you're an experienced tech professional developing a sophisticated web service or a newbie experimenting with web development, a localhost tunnel can prove to be an invaluable tool. Think it sounds complex? Think again! By the end of this blog post, you will have a comprehensive understanding of localhost tunnels and how to create them, all explained in an easy-to-understand, jargon-free manner.

Let's embark on this exciting journey of turning your localhost public!  

What we’ll cover:

The Magic of Localhost / HTTP Tunnels

Working on a Node.js Express web server or integrating webhooks for Stripe or PayPal? There are moments when you need to make your locally running project accessible over the internet, and this is precisely when a localhost tunnel comes to the rescue.

There are numerous scenarios where a localhost tunnel can be a real time-saver: testing IoT devices not connected to your network, providing access to the dev version of an API running on your local machine, or sharing a URL with stakeholders for feedback and reviews.

While various SaaS providers offer tunneling solutions (like ngrok and many others listed here), this blog post will guide you through the process of creating your very own low-latency tunnels over a VPN with custom domains, using virtually any cloud server. You don't need to be a DevOps expert for this; all you need is a few minutes of your time and a server running Ubuntu 22.04.

Prerequisites for This Guide

  • A server with a static public IP and SSH access, running Ubuntu 22.04. This could be a VPS, a cloud server, a dedicated server, or even a Raspberry Pi. You can acquire such servers from providers like DigitalOcean, Scaleway, OVH, or others. However, we do not recommend using AWS, GCP, or Azure, unless you’re well-versed in cloud technology and are willing to pay for the “big names”.
  • A custom domain name that you own and can control the DNS records for.

Note that the term "local machine" refers to the computer where you are developing your project (like a laptop, MacBook, or a desktop computer). "Cloud server" refers to a server without a graphical interface, obtained from a cloud provider (like a VPS, dedicated server, cloud instance, etc.)

With the prerequisites in place, let's proceed!

Server Provisioning: A Step-by-Step Guide

We'll be using the LocalCloud platform for this guide. LocalCloud is an open-source multi-cloud PaaS that deploys web projects directly from Git repositories while creating a VPN between cloud servers and local machines. Think of it as an open-source alternative to Heroku or Render, but with additional time-saving features.

Here's how to install LocalCloud:

  • Create a new server: Create a new server using your cloud provider's dashboard. The server should have a public IP and Ubuntu 22.04 installed. Let's assume that the IP address of your server is 160.170.172.74. You will replace this with the actual IP address of your cloud server in all the following steps.
  • Add a DNS A Record: Navigate to the dashboard of your domain name manager. Here, add an A record to link your server's IP address with your domain. Let's assume you have a domain "saasproject.io" and you want to create tunnels with URLs like "project1.saasproject.io", "project2.saasproject.io", and so on. In this case, add a wildcard A record as follows: .saasproject.io -> 160.170.172.74
*.saasproject.io -> 160.170.172.74
  • SSH into your server: From your local machine, SSH into your server using the command:
ssh root@160.170.172.74
  • Install the LocalCloud Node Agent: Run the following command on your server to install the LocalCloud node agent. Replace "your_domain" with your actual subdomain. This subdomain will handle requests to create/delete localhost tunnels from local machines. For instance, based on the previous step, your_domain could be "agent.saasproject.io".
curl https://localcloud.dev/install | sh -s your_domain
  • Wait for the LocalCloud agent to complete the server provisioning, and then and run a command on the server
localcloud
  • Select "Servers/Local Machines" to connect a local machine (laptops, desktop computers, etc)
  • Follow the steps to install CLI on your local machine
  • Create Tunnels:  After LocalCloud CLI is installed on your local machine, launch it and select “Tunnels” in the menu
localcloud
  • You now have a public URL with HTTPS for a web project running on your local machine.

LocalCloud vs Cloudflare / Ngrok HTTP tunnels

I guess you have a question about how LocalCloud localhost tunnels differ from Cloudflare / Ngrok tunnels:

  • LocalCloud doesn't limit what type of data you can send over localhost tunnels. For example, you cannot stream video, download big files, or send large amounts of data with Cloudflare tunnels. With LocalCloud tunnels you can send anything you wish and are not limited by the amount of data.
  • The speed of sending data over LocalCloud tunnels is limited only by your internet possibility and the bandwidth of your cloud provider. For example, if you have 200 Mb/s at your home or office, you will have the same maximum speed for your tunnels. Cloudflare, Ngrok, and other localhost tunnels share resources between many users and of course performance of tunnels depends on how many users are currently connected to the same hardware
  • LocalCloud provides end-to-end encryption for all data that you send over localhost tunnels while Cloudflare sends data over HTTPS, but not end-to-end encryption and can inspect traffic.
  • With LocalCloud you can limit who can access websites and web apps over localhost tunnels and you shouldn't implement additional authorization in your project for that. For example, you can give access to the internal knowledge base and admin panel to only limited users by installing LocalCloud CLI on their laptops/computers
  • With LocalCloud tunnels you have a dedicated IP address and haven't any TLS certificate issues like you can see with Ngrok
  • With LocalCloud tunnels you pay only for a server, all costs are predictable. Of course, you can also use the same server for other tasks like hosting web projects, building containers, etc.

What's Next on the Agenda?

  • Try deploying a web project from GitHub or Bitbucket. Simply run the "localcloud" CLI and select "New service" from the menu.
  • Try updating a deployed project by pushing code to the repository.

Congratulations! You have successfully created localhost tunnels with a custom domain. We hope this guide was useful, and we encourage you to share your experiences, suggestions, or any questions you may have in the comments section below. Keep experimenting and happy coding!