Skip to content
Go back

Expose Your Localhost app to the Internet with Custom domain using Cloudflare Tunnel

Published:

Since you can’t use a custom domain with the free ngrok plan, but your domain is hosted on Cloudflare, here’s a fully free, reliable alternative:


Use Cloudflare Tunnel — 100% FREE, allows custom domains

You’ll expose your localhost app directly through your own domain using cloudflared.


Goal:

To expose your local app (e.g. running on http://localhost:3000) to:

https://subdomain.yourdomain.com

e.g.

https://local.janakkumarshrestha0.com.np

Step-by-Step: Use Cloudflare Tunnel with Custom Domain

1. Make sure your domain is on Cloudflare


2. Install cloudflared on your machine

For Debian/Ubuntu-based systems:

wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared-linux-amd64.deb

3. Authenticate cloudflared with Cloudflare

cloudflared tunnel login

This opens a browser. Log in to your Cloudflare account and authorize.


4. CCreate a new tunnel. You can name it whatever you like

cloudflared tunnel create mytunnel

This will generate a credentials-file and tunnel ID inside /home/USERNAME/.cloudflared/.


5. Create a config file

Create the file:

sudo mkdir -p /etc/cloudflared
sudo nano /etc/cloudflared/config.yml

Paste this, replacing the values:

tunnel: YOUR_TUNNEL_ID
credentials-file: /home/YOUR_USERNAME/.cloudflared/YOUR_TUNNEL_ID.json

ingress:
  - hostname: local.janakkumarshrestha0.com.np
    service: http://localhost:3000
  - service: http_status:404

Replace:


6. Route DNS through the tunnel

cloudflared tunnel route dns mytunnel local.janakkumarshrestha0.com.np

Cloudflare will automatically add the correct DNS record (CNAME).


7. Start the tunnel

cloudflared tunnel run mytunnel

DONE! Your local site is now live at:

https://local.janakkumarshrestha0.com.np

Optional: Auto-start the tunnel at boot

You can enable the tunnel as a system service:

sudo cloudflared service install
sudo systemctl start cloudflared
sudo systemctl enable cloudflared
sudo systemctl status cloudflared

Secure with Access Rules (Optional)

You can protect your tunnel with Cloudflare Access (Google login, OTP, etc.) — all free.



Suggest Changes

Previous Post
Linux Command Line and Shell Scripting Cheat Sheet for Beginners and Practitioner
Next Post
Make your localhost app live on the internet using ngrok.