Host Your Local Server Without a Static IP Using Cloudflare Tunnels
Host Your Local Server Without a Static IP Using Cloudflare Tunnels
Self-hosting your applications or websites from home offers great control and flexibility, but it also presents challenges like exposing your IP address, maintaining security, and managing dynamic IPs. Cloudflare Tunnels offer a solution to these problems by securely exposing your local web server to the internet without requiring a static IP or port forwarding on your router.
In this blog, we’ll walk you through the step-by-step process of setting up a free, secure, self-hosting environment using Cloudflare Tunnels.
Prerequisites:
Before getting started, ensure you have the following:
- A domain name.
- Domain nameservers configured to point to Cloudflare.
- Cloudflare account access.
- An application or website running on your local server (e.g., using XAMPP, WAMP, or another server stack).
Step 1: Download and Install Cloudflared
Cloudflared is a lightweight command-line tool that you use to establish secure tunnels.
- Download Cloudflared:
- Visit the Cloudflare Downloads page and choose the appropriate version for your operating system.
- Install Cloudflared:
- Rename the downloaded executable to
cloudflared.exe
. - Open PowerShell or Command Prompt and navigate to the location where
cloudflared.exe
is located. - Run the following command to verify the installation:
\cloudflared.exe --version
- Rename the downloaded executable to
Step 2: Authenticate Cloudflared
- Log in to Your Cloudflare Account:
- Run the following command:
cloudflared tunnel login
- This command will open a browser window prompting you to log in to your Cloudflare account and authorize Cloudflare Tunnel for your domain.
- Run the following command:
Generate the Certificate:
- After successful authentication, Cloudflare will generate an account certificate (
cert.pem
) and save it in the default cloudflared directory. ( C:\users\Rishav-PC\.cloudflared\cert.pem)
Step 3: Create a Tunnel
- Create the Tunnel:
- Run the following command to create a new tunnel and give it a name (e.g., hosttesting):
- This creates a tunnel with a unique ID (UUID) and generates a tunnel credentials file (JSON).
- We can check the C:\users\Rishav-PC\.cloudflared directory that the json file is created.
- Confirm the Tunnel Creation:
Run the following command to verify:
cloudflared tunnel list
- Also verify the creation of Tunnel in the Cloudflare zero Trust. Just login to Cloudflare and from the left menu select Cloudflare Zero trust . Then select Tunnels within the Network from the Menu.
Step 4: Create a Configuration File
- Navigate to the Cloudflared Directory:
- Go to the
.cloudflared
directory (e.g.,C:\Users\Rishav-PC\.cloudflared
).
- Go to the
- Create a
config.yml
File:
Step 5: Assign a DNS Record
- Route Traffic to Your Tunnel:
2 .Route Traffic to a Private Network
If you want to connect a private network (e.g., an internal IP address or a range of IP addresses), you can route traffic using an IP address or CIDR.
cloudflared tunnel route ip add <IP/CIDR> <UUID or NAME>. ( Note: Check the Private ip of your host server network. Here mine was: 192.168.2.0/24)
3. Confirm the Route
To confirm that the route has been successfully established, use the following command:
cloudflared tunnel route ip show
This command will display all the IP routes configured for your tunnel, confirming whether the traffic routing is set up correctly.
-
You can also check the from the Cloudflare Zero Trust Menu in the Routes that the Route is added in the list.
Step 6: Run the Tunnel
- Run the Tunnel:
- Start the tunnel to proxy incoming traffic to your local server:
cloudflared tunnel run hosttesting or Tunnel ID
- Start the tunnel to proxy incoming traffic to your local server:
Step 7: Check the tunnel
Your tunnel configuration is complete! If you want to get information on the tunnel you just created, you can run: