Logo

dev-resources.site

for different kinds of informations.

Local machine as a web server without public IP using Cloudflare tunnel(part-1)

Published at
2/22/2024
Categories
cloudflare
webserver
publicwebserver
localmachinewebserver
Author
alamin899
Author
9 person written this
alamin899
open
Local machine as a web server without public IP using Cloudflare tunnel(part-1)

we can use our local machine as a public webserver using Cloudflare tunnel. you do not need any public IP.

Prerequisites

  1. PC or laptop
  2. you need a public domain(possible without a domain)
  3. Cloudflare account

Cloudflare Tunnel
Cloudflare Tunnel provides you with a secure way to connect your resources to Cloudflare without a publicly routable IP address. With Tunnel, you do not send traffic to an external IP — instead, a lightweight daemon in your infrastructure (‘cloudflared’) creates outbound-only connections to Cloudflare’s global network. Cloudflare Tunnel can connect HTTP web servers, SSH servers, remote desktops, and other protocols safely to Cloudflare

Step-1(Install OS)

I use Ubuntu OS in my daily work. So I installed an Ubuntu server on my laptop which I will use as a server machine. You can also use another OS (ubuntu-desktop, Mac OS, windows). after installation, you have to ensure that you connect to the internet, after setup internet your machine is ready to serve as web-server.

Step-2(Connect your domain with cloudflare)

Create a Cloudflare account. after sign up then login in Cloudflare account. After login, you will redirect to the dashboard .From dashboard click add a site button. then enter your domain and submit then you will see name-server ,this name-serve add to your domain registered dashboard.

Image description

Congratulations your domain and cloudflare are connected now.

Step-3(Install cloudflare in your linux machine)

  • Add Cloudflare’s package signing key:


sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null


Enter fullscreen mode Exit fullscreen mode
  • Add Cloudflare’s apt repo to your apt repositories:


echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list


Enter fullscreen mode Exit fullscreen mode
  • Update repositories and install cloudflared:


sudo apt-get update && sudo apt-get install cloudflared


Enter fullscreen mode Exit fullscreen mode

After successfully install Cloudflare check



cloudflared -v


Enter fullscreen mode Exit fullscreen mode

Installing process for other OS - Download And Install

Step-4(Create Tunnel and use)

1. Authenticate cloudflared



sudo cloudflared tunnel login


Enter fullscreen mode Exit fullscreen mode

After this command you will be redirected to your Cloudflare account otherwise you will get a link, then you will use this link after you will see list of domain which you attach on your cloudflare account . i selected my domain iamalamin.com .
after selected youru domain it will create cert.pem file to your machine /root/.cloudflared/ directorey

2. Create cloudflare tunnel



sudo cloudflared tunnel create <NAME>


Enter fullscreen mode Exit fullscreen mode

after this command you will get a tunnel.this tunnel has <Tunnel-UUID> which is store in /root/.cloudflared/ directory with <Tunnel-UUID>.json



sudo cloudflared tunnel list


Enter fullscreen mode Exit fullscreen mode

show tunnel list in your machine

3. Create configure file for my tunnel

In your /root/.cloudflared directory, create a config.yml file using any text editor. This file will configure the tunnel to route traffic from a given origin to the hostname of your choice.



tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json

ingress:
  - hostname: example-app.com
    service: http://localhost:3000
    originRequest:
      noTLSVerify: true

  - service: http_status:404


Enter fullscreen mode Exit fullscreen mode

Here service: http://localhost:3000 this is your local application running url.

4. Start routing traffic

Now assign a CNAME record that points traffic to your tunnel subdomain:



sudo cloudflared tunnel route dns <Tunnel-UUID> <your-domain-name>


Enter fullscreen mode Exit fullscreen mode

After this command a CNAME record will be created in your Cloudflare account domain dashboard.When I visited Cloudflare dashboard's Websites > your-domain.com > DNS > Records I saw that a new CNAME record is added in my domain's DNS record. This was added by the command I run.

Image description

5. Run the tunnel



sudo cloudflared tunnel run <Tunnel-UUID or NAME>


Enter fullscreen mode Exit fullscreen mode

You need to always run this command to access your application using this tunnel.

6. Run Cloudflared as a service

To always run your Cloudflare you can use Cloudflare service on your machine.



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


Enter fullscreen mode Exit fullscreen mode

Now your application will run from your local machine publicly using cloudflare tunnel

webserver Article's
30 articles in total
Favicon
Nginx Simplified: Technical Insights with Real-World Analogies
Favicon
Need assistance to solve an issue
Favicon
Installing Nginx Web Server on Linux: A Step-by-Step Guide
Favicon
Step-by-Step Guide: Setting Up Apache2 and Deploying an HTML Template on Ubuntu (Using Vagrant on a Local Machine)
Favicon
Path-Based Reverse Proxying with Caddy
Favicon
Transform Your Business with Our ERP CRM Software in Kolkata
Favicon
What is a web server ?
Favicon
Postmortem
Favicon
gondola: Why not use a lightweight and simple YAML-based reverse proxy?
Favicon
Uptime Kuma: Uptime von Services beobachten und tracken 🔎
Favicon
Nginx-Proxy-Manager: Leichtgewichtiger Reverse-Proxy 🌍
Favicon
Nginx Proxy Manager: Lightweight reverse proxy 🌍
Favicon
Uptime Kuma: Monitor and track the uptime of services 🔎
Favicon
OpenResty: The Overpowered Web Server Used by 40M Websites (that people rarely talk about)
Favicon
Installing a WebServer on Win Server 2019 VM
Favicon
HOW TO SECURE A WINDOWS SERVER WITH IIS AND AZURE NETWORK SECURITY FEATURES
Favicon
Web Sunucuları: İnternetin Temel Taşları
Favicon
Installing a WebServer on Win Server 2019 VM
Favicon
Advantages of Outsourcing Server Management
Favicon
Deploying a web server on the Linux virtual machine
Favicon
Should I use Nginx or Apache Server?
Favicon
Running a Secure Web Server on AWS EC2
Favicon
Trending In NGINX: Top Trends
Favicon
🚀How TCP Servers Are Designed to Handle Multiple Requests
Favicon
Why did Cloudflare Build its Own Reverse Proxy? - Pingora vs NGINX
Favicon
Planning my Linux Homelab
Favicon
Building a Web Server in Go
Favicon
🚀Unlocking the Power of NGINX - A Web Server Marvel! 🚀
Favicon
Local machine as a web server without public IP using Cloudflare tunnel(part-1)
Favicon
How web technology works? - Part 02

Featured ones: